• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

courtney-miles / slurp / 28483816155

01 Jul 2026 12:00AM UTC coverage: 91.188%. Remained the same
28483816155

push

github

courtney-miles
fix: Replaced call to deprecated league/csv method

0 of 1 new or added line in 1 file covered. (0.0%)

714 of 783 relevant lines covered (91.19%)

3.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

58.49
/src/SlurpFactory.php
1
<?php
2

3
/**
4
 * @author Courtney Miles
5
 *
6
 * @see https://github.com/courtney-miles/slurp
7
 *
8
 * @license MIT
9
 */
10

11
declare(strict_types=1);
12

13
namespace MilesAsylum\Slurp;
14

15
use frictionlessdata\tableschema\Schema;
16
use League\Csv\Reader;
17
use League\Pipeline\PipelineInterface;
18
use MilesAsylum\Slurp\Exception\FactoryException;
19
use MilesAsylum\Slurp\Extract\CsvFileExtractor\CsvFileExtractor;
20
use MilesAsylum\Slurp\Extract\CsvFileExtractor\CsvMultiFileExtractor;
21
use MilesAsylum\Slurp\Filter\ConstraintFiltration\ConstraintFilter;
22
use MilesAsylum\Slurp\Filter\FilterInterface;
23
use MilesAsylum\Slurp\InnerPipeline\FiltrationStage;
24
use MilesAsylum\Slurp\InnerPipeline\InnerProcessor;
25
use MilesAsylum\Slurp\InnerPipeline\LoadStage;
26
use MilesAsylum\Slurp\InnerPipeline\TransformationStage;
27
use MilesAsylum\Slurp\InnerPipeline\ValidationStage;
28
use MilesAsylum\Slurp\Load\DatabaseLoader\DatabaseLoader;
29
use MilesAsylum\Slurp\Load\DatabaseLoader\DmlStmtInterface;
30
use MilesAsylum\Slurp\Load\DatabaseLoader\LoaderFactory;
31
use MilesAsylum\Slurp\Load\DatabaseLoader\SimpleDeleteStmt;
32
use MilesAsylum\Slurp\Load\LoaderInterface;
33
use MilesAsylum\Slurp\OuterPipeline\ExtractionStage;
34
use MilesAsylum\Slurp\OuterPipeline\FinaliseStage;
35
use MilesAsylum\Slurp\OuterPipeline\OuterProcessor;
36
use MilesAsylum\Slurp\Transform\SchemaTransformer\SchemaTransformer;
37
use MilesAsylum\Slurp\Transform\SlurpTransformer\Transformer;
38
use MilesAsylum\Slurp\Transform\TransformerInterface;
39
use MilesAsylum\Slurp\Validate\ConstraintValidation\ConstraintValidator;
40
use MilesAsylum\Slurp\Validate\SchemaValidation\SchemaValidator;
41
use MilesAsylum\Slurp\Validate\ValidatorInterface;
42
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
43
use Symfony\Component\Validator\Validation;
44

45
class SlurpFactory
46
{
47
    /**
48
     * @var ConstraintValidatorFactoryInterface|null
49
     */
50
    private $constraintValidatorFactory;
51

52
    public function __construct(?ConstraintValidatorFactoryInterface $constraintValidatorFactory = null)
53
    {
54
        $this->constraintValidatorFactory = $constraintValidatorFactory;
19✔
55
    }
56

57
    public function createCsvFileExtractor(string $path, Schema $schema): CsvFileExtractor
58
    {
59
        $primaryKeys = $schema->primaryKey();
×
60
        $uniqueFields = $this->getUniqueFieldNamesFromSchema($schema);
×
61

62
        return new CsvFileExtractor(
×
NEW
63
            Reader::from($path),
×
64
            $primaryKeys,
×
65
            $uniqueFields
×
66
        );
×
67
    }
68

69
    /**
70
     * @param string[] $paths
71
     */
72
    public function createCsvMultiFileExtractor(array $paths, Schema $schema): CsvMultiFileExtractor
73
    {
74
        $extractors = [];
×
75

76
        foreach ($paths as $path) {
×
77
            $extractors[] = $this->createCsvFileExtractor($path, $schema);
×
78
        }
79

80
        return new CsvMultiFileExtractor($extractors);
×
81
    }
82

83
    public function createValidationStage(ValidatorInterface $validator): ValidationStage
84
    {
85
        return new ValidationStage($validator);
1✔
86
    }
87

88
    public function createTransformationStage(TransformerInterface $transformer): TransformationStage
89
    {
90
        return new TransformationStage($transformer);
1✔
91
    }
92

93
    public function createFiltrationStage(FilterInterface $filter): FiltrationStage
94
    {
95
        return new FiltrationStage($filter);
×
96
    }
97

98
    public function createLoadStage(LoaderInterface $loader): LoadStage
99
    {
100
        return new LoadStage($loader);
1✔
101
    }
102

103
    public function createEltFinaliseStage(LoaderInterface $loader): FinaliseStage
104
    {
105
        return new FinaliseStage($loader);
1✔
106
    }
107

108
    /**
109
     * @throws FactoryException
110
     */
111
    public function createTableSchemaFromPath(string $path): Schema
112
    {
113
        try {
114
            return new Schema($path);
1✔
115
        } catch (\Throwable $e) {
×
116
            throw new FactoryException('Error creating table schema from file path: ' . $e->getMessage(), 0, $e);
×
117
        }
118
    }
119

120
    /**
121
     * @throws FactoryException
122
     */
123
    public function createTableSchemaFromArray(array $arr): Schema
124
    {
125
        try {
126
            return new Schema($arr);
1✔
127
        } catch (\Throwable $e) {
×
128
            throw new FactoryException('Error creating table schema from array: ' . $e->getMessage(), 0, $e);
×
129
        }
130
    }
131

132
    public function createConstraintValidator(): ConstraintValidator
133
    {
134
        $validationBuilder = Validation::createValidatorBuilder();
1✔
135

136
        if (null !== $this->constraintValidatorFactory) {
1✔
137
            $validationBuilder->setConstraintValidatorFactory($this->constraintValidatorFactory);
×
138
        }
139

140
        return new ConstraintValidator(
1✔
141
            $validationBuilder->getValidator()
1✔
142
        );
1✔
143
    }
144

145
    public function createConstraintFilter(): ConstraintFilter
146
    {
147
        return new ConstraintFilter(
1✔
148
            Validation::createValidator()
1✔
149
        );
1✔
150
    }
151

152
    public function createSchemaValidator(Schema $tableSchema): SchemaValidator
153
    {
154
        return new SchemaValidator($tableSchema);
1✔
155
    }
156

157
    public function createTransformer(): Transformer
158
    {
159
        return Transformer::createTransformer();
1✔
160
    }
161

162
    public function createSchemaTransformer(Schema $tableSchema): SchemaTransformer
163
    {
164
        return new SchemaTransformer($tableSchema);
1✔
165
    }
166

167
    /**
168
     * @param array $fieldMappings array key is the destination column and the array value is the source column
169
     */
170
    public function createDatabaseLoader(
171
        \PDO $pdo,
172
        string $table,
173
        array $fieldMappings,
174
        int $batchSize = 100,
175
        ?DmlStmtInterface $preCommitStmt = null,
176
        ?string $database = null
177
    ): DatabaseLoader {
178
        return new DatabaseLoader(
1✔
179
            $table,
1✔
180
            $fieldMappings,
1✔
181
            new LoaderFactory($pdo),
1✔
182
            $batchSize,
1✔
183
            $preCommitStmt,
1✔
184
            $database
1✔
185
        );
1✔
186
    }
187

188
    public function createExtractionStage(
189
        PipelineInterface $innerPipeline,
190
        ?callable $interrupt = null
191
    ): ExtractionStage {
192
        return new ExtractionStage($innerPipeline, $interrupt);
2✔
193
    }
194

195
    public function createSlurp(PipelineInterface $pipeline): Slurp
196
    {
197
        return new Slurp($pipeline);
1✔
198
    }
199

200
    public function createInnerProcessor(): InnerProcessor
201
    {
202
        return new InnerProcessor();
1✔
203
    }
204

205
    public function createOuterProcessor(): OuterProcessor
206
    {
207
        return new OuterProcessor();
1✔
208
    }
209

210
    public function createSimpleDeleteStmt(
211
        \PDO $pdo,
212
        string $table,
213
        array $conditions = [],
214
        ?string $database = null
215
    ): SimpleDeleteStmt {
216
        return new SimpleDeleteStmt($pdo, $table, $conditions, $database);
1✔
217
    }
218

219
    /**
220
     * @return string[]
221
     */
222
    private function getUniqueFieldNamesFromSchema(Schema $schema): array
223
    {
224
        $uniqueFields = [];
×
225

226
        foreach ($schema->fields() as $field) {
×
227
            if ($field->unique()) {
×
228
                $uniqueFields[] = $field->name();
×
229
            }
230
        }
231

232
        return $uniqueFields;
×
233
    }
234
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc