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

MabaPayseraRestExtension   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 93.1%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 7
dl 0
loc 53
ccs 27
cts 29
cp 0.931
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B load() 0 33 6
A buildFindDenormalizerDefinition() 0 11 1
A getAlias() 0 4 1
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