Completed
Pull Request — master (#4)
by Pavel
10:02
created

BankiruDoctrineApiExtension::load()   C

Complexity

Conditions 8
Paths 26

Size

Total Lines 41
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 8.1373

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 27
cts 31
cp 0.871
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 24
nc 26
nop 2
crap 8.1373
1
<?php
2
3
namespace Bankiru\Api\DependencyInjection;
4
5
use Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle;
6
use Symfony\Component\Config\FileLocator;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Extension\Extension;
9
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
10
use Symfony\Component\DependencyInjection\Reference;
11
12
class BankiruDoctrineApiExtension extends Extension
13
{
14
    /** {@inheritdoc} */
15 6
    public function load(array $configs, ContainerBuilder $container)
16
    {
17 6
        $config = $this->processConfiguration(new Configuration(), $configs);
18
19 6
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
20 6
        $loader->load('api.yml');
21
22 6
        if ($container->hasParameter('kernel.environment') &&
23 5
            $container->getParameter('kernel.environment') === 'test'
24 6
        ) {
25 5
            $loader->load('test.yml');
26 5
        }
27
28 6
        $container->setParameter('bankiru_api.logger_id', $config['logger']['id']);
29 6
        $container->setParameter('bankiru_api.profiler_enabled', $config['profiling']);
30
31 6
        $configuration = $container->getDefinition('bankiru_api.configuration');
32 6
        if ($this->isConfigEnabled($container, $config['entity_cache'])) {
33 1
            if (null === $config['entity_cache']['service']) {
34
                throw new \LogicException('You should specify PSR-6 cache service in order to enable caching');
35
            }
36
37 1
            $configuration->addMethodCall('setApiCache', [new Reference($config['entity_cache']['service'])]);
38 1
            if ($config['entity_cache']['logger'] !== null) {
39 1
                $configuration->addMethodCall('setApiCacheLogger', [new Reference($config['entity_cache']['logger'])]);
40 1
            }
41 1
        }
42
43 6
        if ($this->isConfigEnabled($container, $config['metadata_cache'])) {
44
            $container->getDefinition('bankiru_api.metadata_factory')
45
                      ->addMethodCall('setCacheDriver', [new Reference($config['metadata_cache']['service'])]);
46
        }
47
48 6
        $this->processSensioExtraConfig($container, $loader);
49
50 6
        foreach ($config['entity_cache']['configuration'] as $class => $options) {
51 1
            assert(array_key_exists('enabled', $options));
52 1
            assert(array_key_exists('ttl', $options));
53 1
            $configuration->addMethodCall('setCacheConfiguration', [$class, $options]);
54 6
        }
55 6
    }
56
57 5
    public function getAlias()
58
    {
59 5
        return 'api_client';
60
    }
61
62
    /**
63
     * @param ContainerBuilder $container
64
     * @param YamlFileLoader   $loader
65
     */
66 6
    private function processSensioExtraConfig(ContainerBuilder $container, YamlFileLoader $loader)
67
    {
68 6
        if (!$container->hasParameter('kernel.bundles')) {
69 1
            return;
70
        }
71
72 5
        if (!class_exists('Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle')) {
73
            return;
74
        }
75
76 5
        if (in_array(SensioFrameworkExtraBundle::class, $container->getParameter('kernel.bundles'))) {
77 1
            $loader->load('sensio.yml');
78 1
        }
79 5
    }
80
}
81