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
128:17 queued 63:19
created

IvoryGoogleMapExtension::loadConfig()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 4
nop 2
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
    protected function loadInternal(array $config, ContainerBuilder $container)
32
    {
33
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
34
35
        $resources = [
36
            'form',
37
            'helper/collector',
38
            'helper/helper',
39
            'helper/renderer',
40
            'helper/subscriber',
41
            'helper/utility',
42
            'templating',
43
            'twig',
44
        ];
45
46
        foreach ($resources as $resource) {
47
            $loader->load($resource.'.xml');
48
        }
49
50
        $this->loadMapConfig($config['map'], $container);
51
        $this->loadStaticMapConfig($config['static_map'], $container);
52
        $this->loadServicesConfig($config, $container, $loader);
53
    }
54
55
    /**
56
     * @param mixed[]          $config
57
     * @param ContainerBuilder $container
58
     */
59
    private function loadMapConfig(array $config, ContainerBuilder $container)
60
    {
61
        $container
62
            ->getDefinition('ivory.google_map.helper.renderer.loader')
63
            ->addArgument($config['language']);
64
65
        if ($config['debug']) {
66
            $container
67
                ->getDefinition('ivory.google_map.helper.formatter')
68
                ->addArgument($config['debug']);
69
        }
70
71
        if (isset($config['api_key'])) {
72
            $container
73
                ->getDefinition('ivory.google_map.helper.renderer.loader')
74
                ->addArgument($config['api_key']);
75
        }
76
    }
77
78
    /**
79
     * @param mixed[]          $config
80
     * @param ContainerBuilder $container
81
     */
82
    private function loadStaticMapConfig(array $config, ContainerBuilder $container)
83
    {
84
        if (isset($config['api_key'])) {
85
            $container
86
                ->getDefinition('ivory.google_map.helper.subscriber.static.key')
87
                ->addArgument($config['api_key']);
88
        }
89
90
        if (isset($config['business_account'])) {
91
            $businessAccount = $config['business_account'];
92
93
            $container
94
                ->getDefinition('ivory.google_map.helper.map.static')
95
                ->addArgument(isset($businessAccount['secret']) ? $businessAccount['secret'] : null)
96
                ->addArgument(isset($businessAccount['client_id']) ? $businessAccount['client_id'] : null)
97
                ->addArgument(isset($businessAccount['channel']) ? $businessAccount['channel'] : null);
98
        }
99
    }
100
101
    /**
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
            '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
            'time_zone'          => true,
118
        ];
119
120
        foreach ($services as $service => $http) {
121
            if (isset($config[$service])) {
122
                $this->loadServiceConfig($service, $config[$service], $container, $loader, $http);
123
            }
124
        }
125
    }
126
127
    /**
128
     * @param string           $service
129
     * @param mixed[]          $config
130
     * @param ContainerBuilder $container
131
     * @param LoaderInterface  $loader
132
     * @param bool             $http
133
     */
134
    private function loadServiceConfig(
135
        $service,
136
        array $config,
137
        ContainerBuilder $container,
138
        LoaderInterface $loader,
139
        $http = true
140
    ) {
141
        $loader->load('service/'.$service.'.xml');
142
        $definition = $container->getDefinition($serviceName = 'ivory.google_map.'.$service);
143
144
        if ($http) {
145
            $loader->load('service/serializer.xml');
146
147
            $definition
148
                ->addArgument(new Reference($config['client']))
149
                ->addArgument(new Reference($config['message_factory']))
150
                ->addArgument(new Reference('ivory.serializer'));
151
        }
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