Passed
Pull Request — master (#73)
by Peter
02:15
created

GpsLabGeoIP2Extension   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 1 Features 1
Metric Value
eloc 50
dl 0
loc 102
ccs 47
cts 47
cp 1
rs 10
c 8
b 1
f 1
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfiguration() 0 9 3
A load() 0 65 3
A getAlias() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * GpsLab component.
6
 *
7
 * @author    Peter Gribanov <[email protected]>
8
 * @copyright Copyright (c) 2017, Peter Gribanov
9
 * @license   http://opensource.org/licenses/MIT
10
 */
11
12
namespace GpsLab\Bundle\GeoIP2Bundle\DependencyInjection;
13
14
use GeoIp2\Database\Reader;
15
use GpsLab\Bundle\GeoIP2Bundle\Command\DownloadDatabaseCommand;
16
use GpsLab\Bundle\GeoIP2Bundle\Command\UpdateDatabaseCommand;
17
use GpsLab\Bundle\GeoIP2Bundle\Downloader\Downloader;
18
use GpsLab\Bundle\GeoIP2Bundle\Downloader\MaxMindDownloader;
19
use GpsLab\Bundle\GeoIP2Bundle\Reader\ReaderFactory;
20
use Symfony\Component\DependencyInjection\ContainerBuilder;
21
use Symfony\Component\DependencyInjection\Definition;
22
use Symfony\Component\DependencyInjection\Reference;
23
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
24
25
class GpsLabGeoIP2Extension extends Extension
26
{
27
    /**
28
     * Pattern of database service name.
29
     */
30
    private const SERVICE_NAME = 'geoip2.database.%s_reader';
31
32
    /**
33
     * @param array[]          $configs
34
     * @param ContainerBuilder $container
35
     */
36 6
    public function load(array $configs, ContainerBuilder $container): void
37
    {
38 6
        $configuration = $this->getConfiguration($configs, $container);
39 6
        $config = $this->processConfiguration($configuration, $configs);
40
41 6
        $default_database = $config['default_database'];
42 6
        $databases = $config['databases'];
43
44
        // aliases for default database
45 6
        if (array_key_exists($default_database, $databases)) {
46 3
            $container->setAlias('geoip2.reader', sprintf(self::SERVICE_NAME, $default_database));
47 3
            $container->getAlias('geoip2.reader')->setPublic(true);
48 3
            $container->setAlias(Reader::class, sprintf(self::SERVICE_NAME, $default_database));
49 3
            $container->getAlias(Reader::class)->setPublic(true);
50
        }
51
52
        // define database services
53 6
        foreach ($databases as $name => $database) {
54
            $container
55 3
                ->setDefinition(sprintf(self::SERVICE_NAME, $name), new Definition(Reader::class))
56 3
                ->setPublic(true)
57 3
                ->setLazy(true)
58 3
                ->setArguments([
59 3
                    $database['path'],
60 3
                    $database['locales'],
61
                ]);
62
        }
63
64
        // define MaxMind downloader service
65
        $container
66 6
            ->setDefinition(MaxMindDownloader::class, new Definition(MaxMindDownloader::class))
67 6
            ->setPublic(false)
68 6
            ->setArguments([
69 6
                new Reference('filesystem'),
70 6
                new Reference('logger'),
71
            ]);
72
73 6
        $container->setAlias(Downloader::class, MaxMindDownloader::class);
74 6
        $container->getAlias(Downloader::class)->setPublic(true);
75
76
        // configure update database console command
77
        $container
78 6
            ->setDefinition(UpdateDatabaseCommand::class, new Definition(UpdateDatabaseCommand::class))
79 6
            ->setPublic(false)
80 6
            ->setArguments([
81 6
                new Reference(Downloader::class),
82 6
                $databases,
83
            ])
84 6
            ->addTag('console.command');
85
86
        // configure download database console command
87
        $container
88 6
            ->setDefinition(DownloadDatabaseCommand::class, new Definition(DownloadDatabaseCommand::class))
89 6
            ->setPublic(false)
90 6
            ->setArguments([
91 6
                new Reference(Downloader::class),
92
            ])
93 6
            ->addTag('console.command');
94
95
        // configure reader factory service
96
        $container
97 6
            ->setDefinition(ReaderFactory::class, new Definition(ReaderFactory::class))
98 6
            ->setPublic(false)
99 6
            ->setArguments([
100 6
                $databases,
101
            ]);
102 6
    }
103
104
    /**
105
     * @param array[]          $config
106
     * @param ContainerBuilder $container
107
     *
108
     * @return Configuration
109
     */
110 6
    public function getConfiguration(array $config, ContainerBuilder $container): Configuration
111
    {
112 6
        $cache_dir = null;
113
114 6
        if ($container->hasParameter('kernel.cache_dir')) {
115 4
            $cache_dir = $container->getParameter('kernel.cache_dir');
116
        }
117
118 6
        return new Configuration(is_string($cache_dir) ? $cache_dir : null);
119
    }
120
121
    /**
122
     * @return string
123
     */
124 1
    public function getAlias(): string
125
    {
126 1
        return 'gpslab_geoip';
127
    }
128
}
129