MabaPayseraRestExtension::load()   B
last analyzed

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 81
    public function load(array $configs, ContainerBuilder $container)
19
    {
20 81
        $configuration = new Configuration();
21 81
        $config = $this->processConfiguration($configuration, $configs);
22
23 81
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
24 81
        $loader->load('services.xml');
25
26 81
        $container->setParameter('paysera_rest.locales', $config['locales']);
27 81
        if (count($config['locales']) === 0) {
28 81
            $container->removeDefinition('paysera_rest.listener.locale');
29
        }
30
31 81
        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 81
        if (count($config['path_attribute_resolvers']) > 0 && !class_exists('Doctrine\ORM\EntityManager')) {
39
            throw new RuntimeException(
40
                'Please install doctrine/orm before configuring paysera_rest.path_attribute_resolvers'
41
            );
42
        }
43
44 81
        foreach ($config['path_attribute_resolvers'] as $className => $resolverConfig) {
45 81
            $container->setDefinition(
46 81
                'paysera_rest.auto_registered.path_attribute_resolver.' . $className,
47 81
                $this->buildPathAttributeResolverDefinition($className, $resolverConfig['field'])
48
            );
49
        }
50 81
    }
51
52 81
    private function buildPathAttributeResolverDefinition(string $className, string $field): Definition
53
    {
54 81
        $repositoryDefinition = (new Definition(ObjectRepository::class, [$className]))
55 81
            ->setFactory([new Reference('doctrine.orm.entity_manager'), 'getRepository'])
56
        ;
57
58 81
        return (new Definition(DoctrinePathAttributeResolver::class, [
59 81
            $repositoryDefinition,
60 81
            $field,
61 81
        ]))->addTag('paysera_rest.path_attribute_resolver', ['type' => $className]);
62
    }
63
64 81
    public function getAlias()
65
    {
66 81
        return 'maba_paysera_rest';
67
    }
68
}
69