IvoryGoogleMapExtension::loadStaticMapConfig()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

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