Completed
Push — master ( fe3dbd...6d023a )
by jean
03:47
created

DarkilliantImportExtension::loadInternal()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 16
ccs 9
cts 9
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Darkilliant\ImportBundle\DependencyInjection;
4
5
use Darkilliant\ImportBundle\Step\CsvExtractorStep;
6
use Darkilliant\ImportBundle\Step\DoctrinePersisterStep;
7
use Darkilliant\ImportBundle\Step\LoadObjectNormalizedStep;
8
use Darkilliant\ImportBundle\Step\MappingTransformerStep;
9
use Darkilliant\ProcessBundle\Step\DebugStep;
10
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
11
use JMS\SerializerBundle\JMSSerializerBundle;
12
use Symfony\Component\Config\FileLocator;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
15
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
16
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
17
18
/**
19
 * @internal
20
 * Class DarkilliantImportExtension
21
 */
22
class DarkilliantImportExtension extends ConfigurableExtension implements PrependExtensionInterface
23
{
24 1
    public function prepend(ContainerBuilder $container)
25
    {
26 1
        $configs = $container->getExtensionConfig($this->getAlias());
27 1
        $config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs);
28
29 1
        $processCollection = [];
30 1
        foreach ($config['imports'] as $name => $importConfig) {
31
            $process = [
32 1
                'steps' => [
33
                    [
34
                        'service' => CsvExtractorStep::class,
35
                        'options' => [
36 1
                            'filepath' => $this->resolvePath($importConfig['source']),
37
                            'colums_names' => null,
38 1
                            'delimiter' => ';',
39
                        ],
40
                    ],
41
                    [
42
                        'service' => DebugStep::class,
43
                        'options' => [],
44
                    ],
45
                    [
46
                        'service' => MappingTransformerStep::class,
47
                        'options' => [
48 1
                            'mapping' => $importConfig['mapping'],
49
                        ],
50
                    ],
51
                    [
52
                        'service' => LoadObjectNormalizedStep::class,
53
                        'options' => [
54 1
                            'entity_class' => $importConfig['entity_class'],
55
                        ],
56
                    ],
57
                    [
58
                        'service' => DoctrinePersisterStep::class,
59
                        'options' => [
60
                            'batch_count' => 20,
61
                        ],
62
                    ],
63
                ],
64
            ];
65
66 1
            $processCollection[$name] = $process;
67
        }
68
69 1
        $container->prependExtensionConfig('darkilliant_process', ['process' => $processCollection]);
70 1
    }
71
72 1
    protected function loadInternal(array $mergedConfig, ContainerBuilder $container)
73
    {
74 1
        $yamlLoader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
75
76 1
        $container->setParameter('darkilliant_import_field_entity_resolver', $mergedConfig['fields_entity_resolver']);
77
78 1
        $yamlLoader->load('services.yml');
79 1
        $yamlLoader->load('transformers.yml');
80
81
        // Only when doctrine is in project
82 1
        if (in_array(DoctrineBundle::class, $container->getParameter('kernel.bundles'))) {
83 1
            $yamlLoader->load('symfony_serializer.yml');
84
85
            // Only when jms is in project
86 1
            if (in_array(JMSSerializerBundle::class, $container->getParameter('kernel.bundles'))) {
87 1
                $yamlLoader->load('jms_serializer.yml');
88
            }
89
        }
90 1
    }
91
92 1
    private function resolvePath(string $path): string
93
    {
94 1
        return str_replace('file://', '', $path);
95
    }
96
}
97