Completed
Push — master ( 2afc29...29656f )
by Philip
06:07
created

DdrRestExtension::loadSecurityConfig()   C

Complexity

Conditions 7
Paths 3

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 8.4953

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 11
cts 16
cp 0.6875
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 17
nc 3
nop 3
crap 8.4953
1
<?php
2
3
namespace Dontdrinkandroot\RestBundle\DependencyInjection;
4
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Loader;
8
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
9
10
/**
11
 * @author Philip Washington Sorst <[email protected]>
12
 */
13
class DdrRestExtension extends Extension
14
{
15
    const ACCESS_TOKEN_CLASS = 'access_token_class';
16
    const AUTHENTICATION_PROVIDER_KEY = 'authentication_provider_key';
17
18
    /**
19
     * {@inheritdoc}
20
     */
21 14
    public function load(array $configs, ContainerBuilder $container)
22
    {
23 14
        $configuration = new Configuration();
24 14
        $config = $this->processConfiguration($configuration, $configs);
25
26 14
        $container->setParameter('ddr_rest.paths', $config['paths']);
27
28 14
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
29 14
        $loader->load('services.yml');
30
31 14
        $directories = [];
32 14
        $directories['Dontdrinkandroot\RestBundle'] = realpath(__DIR__ . '/../Resources/config/rest/');
33 14
        if (array_key_exists('metadata', $config) && array_key_exists('directories', $config['metadata'])) {
34 14
            foreach ($config['metadata']['directories'] as $directory) {
35 14
                $directories[$directory['namespace_prefix']] = $directory['path'];
36
            }
37
        }
38
39
        $container
40 14
            ->getDefinition('ddr_rest.metadata.file_locator')
41 14
            ->setArguments([$directories]);
42
43 14
        if (array_key_exists('security', $config)) {
44 12
            $this->loadSecurityConfig($config['security'], $container, $loader);
45
        }
46 14
    }
47
48 12
    private function loadSecurityConfig(array $config, ContainerBuilder $container, Loader\YamlFileLoader $loader)
49
    {
50 12
        $accessTokenClass = $config[self::ACCESS_TOKEN_CLASS];
51 12
        $authenticationProviderKey = $config[self::AUTHENTICATION_PROVIDER_KEY];
52 12
        if (null === $accessTokenClass && null === $authenticationProviderKey) {
53
            return;
54
        }
55
56
        if (
57 12
            (null === $accessTokenClass && null !== $authenticationProviderKey)
58 12
            || (null !== $accessTokenClass && null === $authenticationProviderKey)
59
        ) {
60
            throw new \RuntimeException(
61
                sprintf(
62
                    'You need to provide values for "%s" AND "%s"',
63
                    self::ACCESS_TOKEN_CLASS,
64
                    self::AUTHENTICATION_PROVIDER_KEY
65
                )
66
            );
67
        }
68
69 12
        $container->setParameter('ddr_rest.access_token_class', $accessTokenClass);
70 12
        $container->setParameter('ddr_rest.authentication_provider_key', $authenticationProviderKey);
71 12
        $container->setParameter('ddr_rest.security.enabled', true);
72
73 12
        $loader->load('services_security.yml');
74 12
    }
75
}
76