GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#202)
by Eric
63:19
created

IvoryGoogleMapExtension::loadServiceConfig()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 42
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 15
cts 15
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 27
nc 16
nop 5
crap 7
1
<?php
2
3
/*
4
 * This file is part of the Ivory Google Map bundle package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\GoogleMapBundle\DependencyInjection;
13
14
use Ivory\GoogleMap\Service\BusinessAccount;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\Config\Loader\LoaderInterface;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Definition;
19
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
20
use Symfony\Component\DependencyInjection\Reference;
21
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
22
23
/**
24
 * @author GeLo <[email protected]>
25
 */
26
class IvoryGoogleMapExtension extends ConfigurableExtension
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31 486
    protected function loadInternal(array $config, ContainerBuilder $container)
32
    {
33 486
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
34
35
        $resources = [
36 486
            'form',
37 378
            'helper/collector',
38 378
            'helper/helper',
39 378
            'helper/renderer',
40 378
            'helper/subscriber',
41 378
            'helper/utility',
42 378
            'templating',
43 378
            'twig',
44 378
        ];
45
46 486
        foreach ($resources as $resource) {
47 486
            $loader->load($resource.'.xml');
48 378
        }
49
50 486
        $this->loadMapConfig($config['map'], $container);
51 486
        $this->loadStaticMapConfig($config['static_map'], $container);
52 486
        $this->loadServicesConfig($config, $container, $loader);
53
    }
54
55
    /**
56
     * @param mixed[]          $config
57
     * @param ContainerBuilder $container
58 486
     */
59
    private function loadMapConfig(array $config, ContainerBuilder $container)
60
    {
61 486
        $container
62 486
            ->getDefinition('ivory.google_map.helper.renderer.loader')
63
            ->addArgument($config['language']);
64 486
65
        if ($config['debug']) {
66 486
            $container
67 486
                ->getDefinition('ivory.google_map.helper.formatter')
68 378
                ->addArgument($config['debug']);
69
        }
70 486
71
        if (isset($config['api_key'])) {
72 9
            $container
73 9
                ->getDefinition('ivory.google_map.helper.renderer.loader')
74 7
                ->addArgument($config['api_key']);
75 486
        }
76
    }
77
78
    /**
79
     * @param mixed[]          $config
80
     * @param ContainerBuilder $container
81
     */
82 486
    private function loadStaticMapConfig(array $config, ContainerBuilder $container)
83
    {
84
        if (isset($config['api_key'])) {
85 486
            $container
86 378
                ->getDefinition('ivory.google_map.helper.subscriber.static.key')
87 378
                ->addArgument($config['api_key']);
88 378
        }
89 378
90 378
        if (isset($config['business_account'])) {
91 378
            $businessAccount = $config['business_account'];
92 378
93 378
            $container
94 378
                ->getDefinition('ivory.google_map.helper.map.static')
95
                ->addArgument(isset($businessAccount['secret']) ? $businessAccount['secret'] : null)
96 486
                ->addArgument(isset($businessAccount['client_id']) ? $businessAccount['client_id'] : null)
97 486
                ->addArgument(isset($businessAccount['channel']) ? $businessAccount['channel'] : null);
98 416
        }
99 308
    }
100 378
101 486
    /**
102
     * @param mixed[]          $config
103
     * @param ContainerBuilder $container
104
     * @param LoaderInterface  $loader
105
     */
106
    private function loadServicesConfig(array $config, ContainerBuilder $container, LoaderInterface $loader)
107
    {
108
        $services = [
109
            'direction'          => true,
110 396
            'distance_matrix'    => true,
111
            'elevation'          => true,
112
            'geocoder'           => true,
113
            'place_autocomplete' => true,
114
            'place_detail'       => true,
115
            'place_photo'        => false,
116
            'place_search'       => true,
117 396
            'time_zone'          => true,
118 396
        ];
119
120 396
        foreach ($services as $service => $http) {
121 360
            if (isset($config[$service])) {
122
                $this->loadServiceConfig($service, $config[$service], $container, $loader, $http);
123
            }
124 360
        }
125 360
    }
126 360
127 280
    /**
128
     * @param string           $service
129 396
     * @param mixed[]          $config
130 72
     * @param ContainerBuilder $container
131 56
     * @param LoaderInterface  $loader
132
     * @param bool             $http
133 396
     */
134 81
    private function loadServiceConfig(
135 63
        $service,
136
        array $config,
137 396
        ContainerBuilder $container,
138 162
        LoaderInterface $loader,
139
        $http = true
140 162
    ) {
141 162
        $loader->load('service/'.$service.'.xml');
142 162
        $definition = $container->getDefinition($serviceName = 'ivory.google_map.'.$service);
143 162
144 162
        if ($http) {
145 162
            $loader->load('service/serializer.xml');
146 126
147 126
            $definition
148
                ->addArgument(new Reference($config['client']))
149 162
                ->addArgument(new Reference($config['message_factory']))
150 126
                ->addArgument(new Reference('ivory.serializer'));
151 396
        }
152
153
        if ($http && isset($config['format'])) {
154
            $definition->addMethodCall('setFormat', [$config['format']]);
155
        }
156
157
        if (isset($config['api_key'])) {
158
            $definition->addMethodCall('setKey', [$config['api_key']]);
159
        }
160
161
        if (isset($config['business_account'])) {
162
            $businessAccountConfig = $config['business_account'];
163
164
            $container->setDefinition(
165
                $businessAccountName = $serviceName.'.business_account',
166
                new Definition(BusinessAccount::class, [
167
                    $businessAccountConfig['client_id'],
168
                    $businessAccountConfig['secret'],
169
                    isset($businessAccountConfig['channel']) ? $businessAccountConfig['channel'] : null,
170
                ])
171
            );
172
173
            $definition->addMethodCall('setBusinessAccount', [new Reference($businessAccountName)]);
174
        }
175
    }
176
}
177