Completed
Push — master ( f33ef3...ffb7e5 )
by
unknown
04:55
created

MediaMonksRestApiExtension::usePathRequestMatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
crap 2
1
<?php
2
3
namespace MediaMonks\RestApiBundle\DependencyInjection;
4
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
8
use Symfony\Component\DependencyInjection\Reference;
9
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
10
use Symfony\Component\DependencyInjection\Loader;
11
12
/**
13
 * This is the class that loads and manages your bundle configuration.
14
 *
15
 * @link http://symfony.com/doc/current/cookbook/bundles/extension.html
16
 */
17
class MediaMonksRestApiExtension extends Extension implements ExtensionInterface
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22 1
    public function load(array $configs, ContainerBuilder $container)
23
    {
24 1
        $config = $this->processConfiguration(new Configuration(), $configs);
25
26 1
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
27 1
        $loader->load('services.xml');
28
29 1
        if (!empty($config['request_matcher']['path'])) {
30
            $this->usePathRequestMatcher($container, $config);
31
        }
32 1
        elseif (!empty($config['request_matcher']['whitelist'])) {
33 1
            $this->useRegexRequestMatcher($container, $config);
34
        }
35
36 1
        $container->getDefinition('mediamonks_rest_api.response_transformer')
37 1
            ->replaceArgument(
38 1
                2,
39
                [
40 1
                    'debug'               => $this->getDebug($config, $container),
41 1
                    'post_message_origin' => $config['post_message_origin'],
42
                ]
43
            );
44
45 1
        if (!empty($config['response_model'])) {
46
            $this->replaceResponseModel($container, $config);
47
        }
48
49 1
        $this->loadSerializer($container, $config);
50 1
    }
51
52
    /**
53
     * @param ContainerBuilder $container
54
     * @param array $config
55
     */
56
    protected function usePathRequestMatcher(ContainerBuilder $container, array $config)
57
    {
58
        $container->getDefinition('mediamonks_rest_api.path_request_matcher')
59
            ->replaceArgument(0, $config['request_matcher']['path']);
60
61
        $container->getDefinition('mediamonks_rest_api.rest_api_event_subscriber')
62
            ->replaceArgument(0, new Reference('mediamonks_rest_api.path_request_matcher'));
63
    }
64
65
    /**
66
     * @param ContainerBuilder $container
67
     * @param array $config
68
     */
69 1
    protected function useRegexRequestMatcher(ContainerBuilder $container, array $config)
70
    {
71 1
        $container->getDefinition('mediamonks_rest_api.regex_request_matcher')
72 1
            ->replaceArgument(0, $config['request_matcher']['whitelist']);
73 1
        $container->getDefinition('mediamonks_rest_api.regex_request_matcher')
74 1
            ->replaceArgument(1, $config['request_matcher']['blacklist']);
75 1
    }
76
77
    /**
78
     * @param ContainerBuilder $container
79
     * @param array $config
80
     */
81 1
    protected function loadSerializer(ContainerBuilder $container, array $config)
82
    {
83 1
        if (!$container->has($config['serializer'])) {
84 1
            $config['serializer'] = 'mediamonks_rest_api.serializer.'.$config['serializer'];
85
        }
86
87 1
        $container->getDefinition('mediamonks_rest_api.request_transformer')
88 1
            ->replaceArgument(0, new Reference($config['serializer']));
89 1
        $container->getDefinition('mediamonks_rest_api.response_transformer')
90 1
            ->replaceArgument(0, new Reference($config['serializer']));
91 1
    }
92
93
    /**
94
     * @param ContainerBuilder $container
95
     * @param array $config
96
     */
97
    protected function replaceResponseModel(ContainerBuilder $container, array $config)
98
    {
99
        $container->getDefinition('mediamonks_rest_api.response_model_factory')
100
            ->replaceArgument(0, new Reference($config['response_model']));
101
    }
102
103
    /**
104
     * @return string
105
     */
106 1
    public function getAlias()
107
    {
108 1
        return 'mediamonks_rest_api';
109
    }
110
111
    /**
112
     * @param array $config
113
     * @param ContainerBuilder $container
114
     * @return bool
115
     */
116 1
    public function getDebug(array $config, ContainerBuilder $container)
117
    {
118 1
        if (isset($config['debug'])) {
119
            return $config['debug'];
120
        }
121 1
        if ($container->hasParameter('kernel.debug')) {
122 1
            return $container->getParameter('kernel.debug');
123
        }
124
125
        return false;
126
    }
127
}
128