Completed
Push — master ( 257b0b...6d5ae9 )
by Marius
14:23
created

MabaPayseraRestExtension::load()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6.0359

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 18
cts 20
cp 0.9
rs 8.7697
c 0
b 0
f 0
cc 6
nc 12
nop 2
crap 6.0359
1
<?php
2
declare(strict_types=1);
3
4
namespace Maba\Bundle\RestBundle\DependencyInjection;
5
6
use Doctrine\Common\Persistence\ObjectRepository;
7
use Maba\Bundle\RestBundle\Service\PathAttributeResolver\DoctrinePathAttributeResolver;
8
use RuntimeException;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\Config\FileLocator;
11
use Symfony\Component\DependencyInjection\Definition;
12
use Symfony\Component\DependencyInjection\Reference;
13
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
14
use Symfony\Component\DependencyInjection\Loader;
15
16
class MabaPayseraRestExtension extends Extension
17
{
18 80
    public function load(array $configs, ContainerBuilder $container)
19
    {
20 80
        $configuration = new Configuration();
21 80
        $config = $this->processConfiguration($configuration, $configs);
22
23 80
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
24 80
        $loader->load('services.xml');
25
26 80
        $container->setParameter('paysera_rest.locales', $config['locales']);
27 80
        if (count($config['locales']) === 0) {
28 80
            $container->removeDefinition('paysera_rest.listener.locale');
29
        }
30
31 80
        if ($config['validation']['property_path_converter'] !== null) {
32 1
            $container->setAlias(
33 1
                'paysera_rest.validation.property_path_converter',
34 1
                $config['validation']['property_path_converter']
35
            );
36
        }
37
38 80
        if (count($config['find_denormalizers']) > 0 && !class_exists('Doctrine\ORM\EntityManager')) {
39
            throw new RuntimeException(
40
                'Please install doctrine/orm before configuring paysera_rest.find_denormalizers'
41
            );
42
        }
43
44 80
        foreach ($config['find_denormalizers'] as $className => $denormalizerConfig) {
45 80
            $container->setDefinition(
46 80
                'paysera_rest.auto_registered.find_denormalizer.' . $className,
47 80
                $this->buildFindDenormalizerDefinition($className, $denormalizerConfig['field'])
48
            );
49
        }
50 80
    }
51
52 80
    private function buildFindDenormalizerDefinition(string $className, string $field): Definition
53
    {
54 80
        $repositoryDefinition = (new Definition(ObjectRepository::class, [$className]))
55 80
            ->setFactory([new Reference('doctrine.orm.entity_manager'), 'getRepository'])
56
        ;
57
58 80
        return (new Definition(DoctrinePathAttributeResolver::class, [
59 80
            $repositoryDefinition,
60 80
            $field,
61 80
        ]))->addTag('paysera_rest.path_attribute_resolver', ['type' => $className]);
62
    }
63
64 80
    public function getAlias()
65
    {
66 80
        return 'maba_paysera_rest';
67
    }
68
}
69