Completed
Push — master ( bafd7c...9fa85a )
by Markus
09:12 queued 03:47
created

MathielenImportEngineExtension   C

Complexity

Total Complexity 57

Size/Duplication

Total Lines 377
Duplicated Lines 2.65 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 67.59%

Importance

Changes 22
Bugs 3 Features 2
Metric Value
wmc 57
c 22
b 3
f 2
lcom 1
cbo 8
dl 10
loc 377
ccs 171
cts 253
cp 0.6759
rs 6.4331

14 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 11 2
B parseConfig() 0 26 5
A generateObjectFactoryDef() 0 14 3
D generateFinderDef() 10 32 9
D generateImporterDef() 0 34 9
A generateFiltersDef() 0 14 2
B generateTransformerDef() 0 44 5
A generateValidatorDef() 0 11 1
B generateValidationDef() 0 55 7
A setSourceStorageDef() 0 7 1
B addStorageProviderDef() 0 55 6
A getStorageFileDefinitionFromUri() 0 10 2
B getStorageDef() 0 34 4
A getAlias() 0 4 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like MathielenImportEngineExtension often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use MathielenImportEngineExtension, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Mathielen\ImportEngineBundle\DependencyInjection;
3
4
use Mathielen\ImportEngine\Exception\InvalidConfigurationException;
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
6
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
7
use Symfony\Component\DependencyInjection\Extension\Extension;
8
use Symfony\Component\Config\FileLocator;
9
use Symfony\Component\DependencyInjection\Reference;
10
use Symfony\Component\DependencyInjection\Definition;
11
use Symfony\Component\ExpressionLanguage\Expression;
12
13
class MathielenImportEngineExtension extends Extension
14
{
15
16 11
    public function load(array $configs, ContainerBuilder $container)
17
    {
18 11
        $config = $this->processConfiguration(new Configuration(), $configs);
19
20 11
        if (!empty($config['importers'])) {
21 9
            $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
22 9
            $loader->load('services.xml');
23
24 9
            $this->parseConfig($config, $container);
25 4
        }
26 6
    }
27
28 9
    private function parseConfig(array $config, ContainerBuilder $container)
29
    {
30 9
        $storageLocatorDef = $container->findDefinition('mathielen_importengine.import.storagelocator');
31 9
        foreach ($config['storageprovider'] as $name => $sourceConfig) {
32 5
            $this->addStorageProviderDef($storageLocatorDef, $sourceConfig, $name);
33 9
        }
34
35 9
        $importerRepositoryDef = $container->findDefinition('mathielen_importengine.importer.repository');
36 9
        foreach ($config['importers'] as $name => $importConfig) {
37 9
            $finderDef = null;
38 9
            if (isset($importConfig['preconditions'])) {
39 5
                $finderDef = $this->generateFinderDef($importConfig['preconditions']);
40 5
            }
41
42 9
            $objectFactoryDef = null;
43 9
            if (isset($importConfig['object_factory'])) {
44 5
                $objectFactoryDef = $this->generateObjectFactoryDef($importConfig['object_factory']);
45
            }
46
47 4
            $importerRepositoryDef->addMethodCall('register', array(
48 4
                $name,
49 4
                $this->generateImporterDef($importConfig, $objectFactoryDef),
50
                $finderDef
51 4
            ));
52 4
        }
53 4
    }
54
55 5
    private function generateObjectFactoryDef(array $config)
56
    {
57 5
        if (!class_exists($config['class'])) {
58 5
            throw new InvalidConfigurationException("Object-Factory target-class '".$config['class']."' does not exist.");
59
        }
60
61
        if ($config['type'] == 'jms_serializer') {
62
            return new Definition('Mathielen\DataImport\Writer\ObjectWriter\JmsSerializerObjectFactory', array(
63
                $config['class'],
64
                new Reference('jms_serializer')));
65
        }
66
67
        return new Definition('Mathielen\DataImport\Writer\ObjectWriter\DefaultObjectFactory', array($config['class']));
68
    }
69
70
    /**
71
     * @return \Symfony\Component\DependencyInjection\Definition
72
     */
73 5
    private function generateFinderDef(array $finderConfig)
74
    {
75 5
        $finderDef = new Definition('Mathielen\ImportEngine\Importer\ImporterPrecondition');
76
77 5
        if (isset($finderConfig['filename'])) {
78 5
            foreach ($finderConfig['filename'] as $conf) {
79 5
                $finderDef->addMethodCall('filename', array($conf));
80 5
            }
81 5
        }
82
83 5 View Code Duplication
        if (isset($finderConfig['format'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84 5
            foreach ($finderConfig['format'] as $conf) {
85 5
                $finderDef->addMethodCall('format', array($conf));
86 5
            }
87 5
        }
88
89 5
        if (isset($finderConfig['fieldcount'])) {
90 5
            $finderDef->addMethodCall('fieldcount', array($finderConfig['fieldcount']));
91 5
        }
92
93 5 View Code Duplication
        if (isset($finderConfig['fields'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94 5
            foreach ($finderConfig['fields'] as $conf) {
95 5
                $finderDef->addMethodCall('field', array($conf));
96 5
            }
97 5
        }
98
99 5
        if (isset($finderConfig['fieldset'])) {
100 5
            $finderDef->addMethodCall('fieldset', array($finderConfig['fieldset']));
101 5
        }
102
103 5
        return $finderDef;
104
    }
105
106
    /**
107
     * @return \Symfony\Component\DependencyInjection\Definition
108
     */
109 4
    private function generateImporterDef(array $importConfig, Definition $objectFactoryDef=null)
110
    {
111 4
        $importerDef = new Definition('Mathielen\ImportEngine\Importer\Importer', array(
112 4
            $this->getStorageDef($importConfig['target'], $objectFactoryDef)
113 4
        ));
114
115 4
        if (isset($importConfig['source'])) {
116
            $this->setSourceStorageDef($importConfig['source'], $importerDef);
117
        }
118
119
        //enable validation?
120 4
        if (isset($importConfig['validation']) && !empty($importConfig['validation'])) {
121 2
            $this->generateValidationDef($importConfig['validation'], $importerDef, $objectFactoryDef);
122 2
        }
123
124
        //add converters?
125 4
        if (isset($importConfig['mappings']) && !empty($importConfig['mappings'])) {
126
            $this->generateTransformerDef($importConfig['mappings'], $importerDef);
127
        }
128
129
        //add filters?
130 4
        if (isset($importConfig['filters']) && !empty($importConfig['filters'])) {
131
            $this->generateFiltersDef($importConfig['filters'], $importerDef);
132
        }
133
134
        //has static context?
135 4
        if (isset($importConfig['context'])) {
136 4
            $importerDef->addMethodCall('setContext', array(
137 4
                $importConfig['context']
138 4
            ));
139 4
        }
140
141 4
        return $importerDef;
142
    }
143
144
    private function generateFiltersDef(array $filtersOptions, Definition $importerDef)
145
    {
146
        $filtersDef = new Definition('Mathielen\ImportEngine\Filter\Filters');
147
148
        foreach ($filtersOptions as $filtersOption) {
149
            $filtersDef->addMethodCall('add', array(
150
                new Reference($filtersOption)
151
            ));
152
        }
153
154
        $importerDef->addMethodCall('filters', array(
155
            $filtersDef
156
        ));
157
    }
158
159
    private function generateTransformerDef(array $mappingOptions, Definition $importerDef)
160
    {
161
        $mappingsDef = new Definition('Mathielen\ImportEngine\Mapping\Mappings');
162
163
        //set converters
164
        foreach ($mappingOptions as $field=>$fieldMapping) {
165
            $converter = null;
166
            if (isset($fieldMapping['converter'])) {
167
                $converter = $fieldMapping['converter'];
168
            }
169
170
            if (isset($fieldMapping['to'])) {
171
                $mappingsDef->addMethodCall('add', array(
172
                    $field,
173
                    $fieldMapping['to'],
174
                    $converter
175
            ));
176
            } elseif ($converter) {
177
                $mappingsDef->addMethodCall('setConverter', array(
178
                    $converter,
179
                    $field
180
                ));
181
            }
182
        }
183
184
        $mappingFactoryDef = new Definition('Mathielen\ImportEngine\Mapping\DefaultMappingFactory', array(
185
            $mappingsDef
186
        ));
187
        $converterProviderDef = new Definition('Mathielen\ImportEngine\Mapping\Converter\Provider\ContainerAwareConverterProvider', array(
188
            new Reference('service_container')
189
        ));
190
191
        $transformerDef = new Definition('Mathielen\ImportEngine\Transformation\Transformation');
192
        $transformerDef->addMethodCall('setMappingFactory', array(
193
            $mappingFactoryDef
194
        ));
195
        $transformerDef->addMethodCall('setConverterProvider', array(
196
            $converterProviderDef
197
        ));
198
199
        $importerDef->addMethodCall('transformation', array(
200
            $transformerDef
201
        ));
202
    }
203
204 2
    private function generateValidatorDef(array $options)
205
    {
206
        //eventdispatcher aware source validatorfilter
207 2
        $validatorFilterDef = new Definition('Mathielen\DataImport\Filter\ValidatorFilter', array(
208 2
            new Reference('validator'),
209 2
            $options,
210 2
            new Reference('event_dispatcher')
211 2
        ));
212
213 2
        return $validatorFilterDef;
214
    }
215
216 2
    private function generateValidationDef(array $validationConfig, Definition $importerDef, Definition $objectFactoryDef=null)
217
    {
218 2
        $validationDef = new Definition('Mathielen\ImportEngine\Validation\ValidatorValidation', array(
219 2
            new Reference('validator')
220 2
        ));
221 2
        $importerDef->addMethodCall('validation', array(
222
            $validationDef
223 2
        ));
224
225 2
        $validatorFilterDef = $this->generateValidatorDef(
226 2
            isset($validationConfig['options'])?$validationConfig['options']:array()
227 2
        );
228
229 2
        if (isset($validationConfig['source'])) {
230 2
            $validationDef->addMethodCall('setSourceValidatorFilter', array(
231
                $validatorFilterDef
232 2
            ));
233
234 2
            foreach ($validationConfig['source']['constraints'] as $field=>$constraint) {
235 2
                $validationDef->addMethodCall('addSourceConstraint', array(
236 2
                    $field,
237 2
                    new Definition($constraint)
238 2
                ));
239 2
            }
240 2
        }
241
242
        //automatically apply class validation
243 2
        if (isset($validationConfig['target'])) {
244
245
            //using objects as result
246 2
            if ($objectFactoryDef) {
247
248
                //set eventdispatcher aware target CLASS-validatorfilter
249
                $validatorFilterDef = new Definition('Mathielen\DataImport\Filter\ClassValidatorFilter', array(
250
                    new Reference('validator'),
251
                    $objectFactoryDef,
252
                    new Reference('event_dispatcher')
253
                ));
254
255
            } else {
256 2
                foreach ($validationConfig['target']['constraints'] as $field=>$constraint) {
257 2
                    $validationDef->addMethodCall('addTargetConstraint', array(
258 2
                        $field,
259 2
                        new Definition($constraint)
260 2
                    ));
261 2
                }
262
            }
263
264 2
            $validationDef->addMethodCall('setTargetValidatorFilter', array(
265
                $validatorFilterDef
266 2
            ));
267 2
        }
268
269 2
        return $validationDef;
270
    }
271
272
    private function setSourceStorageDef(array $sourceConfig, Definition $importerDef)
273
    {
274
        $sDef = $this->getStorageDef($sourceConfig, $importerDef);
275
        $importerDef->addMethodCall('setSourceStorage', array(
276
            $sDef
277
        ));
278
    }
279
280 5
    private function addStorageProviderDef(Definition $storageLocatorDef, $config, $id = 'default')
281
    {
282 5
        $formatDiscoverLocalFileStorageFactoryDef = new Definition('Mathielen\ImportEngine\Storage\Factory\FormatDiscoverLocalFileStorageFactory', array(
283 5
            new Definition('Mathielen\ImportEngine\Storage\Format\Discovery\MimeTypeDiscoverStrategy', array(
284
                array(
285 5
                    'text/plain'=>new Definition('Mathielen\ImportEngine\Storage\Format\Factory\CsvAutoDelimiterFormatFactory'),
286 5
                    'text/csv'=>new Definition('Mathielen\ImportEngine\Storage\Format\Factory\CsvAutoDelimiterFormatFactory'),
287
                )
288 5
            )),
289 5
            new Reference('logger')
290 5
        ));
291
292 5
        switch ($config['type']) {
293 5
            case 'directory':
294 5
                $spFinderDef = new Definition('Symfony\Component\Finder\Finder');
295 5
                $spFinderDef->addMethodCall('in', array(
296 5
                    $config['uri']
297 5
                ));
298 5
                $spDef = new Definition('Mathielen\ImportEngine\Storage\Provider\FinderFileStorageProvider', array(
299 5
                    $spFinderDef,
300
                    $formatDiscoverLocalFileStorageFactoryDef
301 5
                ));
302 5
                break;
303 5
            case 'upload':
304 5
                $spDef = new Definition('Mathielen\ImportEngine\Storage\Provider\UploadFileStorageProvider', array(
305 5
                    $config['uri'],
306
                    $formatDiscoverLocalFileStorageFactoryDef
307 5
                ));
308 5
                break;
309 5
            case 'doctrine':
310 5
                $spDef = new Definition('Mathielen\ImportEngine\Storage\Provider\DoctrineQueryStorageProvider', array(
311 5
                    new Reference('doctrine.orm.entity_manager'),
312 5
                    $config['queries']
313 5
                ));
314 5
                break;
315 5
            case 'service':
316 5
                $spDef = new Definition('Mathielen\ImportEngine\Storage\Provider\ServiceStorageProvider', array(
317 5
                    new Reference('service_container'),
318 5
                    $config['services']
319 5
                ));
320 5
                break;
321 5
            case 'file':
322 5
                $spDef = new Definition('Mathielen\ImportEngine\Storage\Provider\FileStorageProvider', array(
323
                    $formatDiscoverLocalFileStorageFactoryDef
324 5
                ));
325 5
                break;
326
            default:
327
                throw new InvalidConfigurationException('Unknown type for storage provider: '.$config['type']);
328 5
        }
329
330 5
        $storageLocatorDef->addMethodCall('register', array(
331 5
            $id,
332
            $spDef
333 5
        ));
334 5
    }
335
336 4
    private function getStorageFileDefinitionFromUri($uri)
337
    {
338 4
        if (substr($uri, 0, 2) === '@=') {
339
            $uri = new Expression(substr($uri, 2));
340
        }
341
342 4
        return new Definition('SplFileInfo', array(
343
            $uri
344 4
        ));
345
    }
346
347
    /**
348
     * @return Definition
349
     */
350 4
    private function getStorageDef(array $config, Definition $objectFactoryDef=null)
351
    {
352 4
        switch ($config['type']) {
353 4
            case 'file':
354 4
                $fileDef = $this->getStorageFileDefinitionFromUri($config['uri']);
355
356 4
                $format = $config['format'];
357 4
                $storageDef = new Definition('Mathielen\ImportEngine\Storage\LocalFileStorage', array(
358 4
                    $fileDef,
359 4
                    new Definition('Mathielen\ImportEngine\Storage\Format\\'.ucfirst($format['type'])."Format", $format['arguments'])
360 4
                ));
361
362 4
                break;
363
            case 'doctrine':
364
                $storageDef = new Definition('Mathielen\ImportEngine\Storage\DoctrineStorage', array(
365
                    new Reference('doctrine.orm.entity_manager'),
366
                    $config['entity']
367
                ));
368
369
                break;
370
            case 'service':
371
                $storageDef = new Definition('Mathielen\ImportEngine\Storage\ServiceStorage', array(
372
                    array(new Reference($config['service']), $config['method']), //callable
373
                    array(),
374
                    $objectFactoryDef //from parameter array
375
                ));
376
377
                break;
378
            default:
379
                throw new InvalidConfigurationException('Unknown type for storage: '.$config['type']);
380 4
        }
381
382 4
        return $storageDef;
383
    }
384
385 11
    public function getAlias()
386
    {
387 11
        return 'mathielen_import_engine';
388
    }
389
}
390