Completed
Pull Request — master (#71)
by Peter
17:14 queued 08:34
created

GpsLabGeoIP2Extension::load()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 0
cts 33
cp 0
rs 8.9599
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 12

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Symfony\Component\DependencyInjection\ContainerBuilder;
20
use Symfony\Component\DependencyInjection\Definition;
21
use Symfony\Component\DependencyInjection\Reference;
22
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
23
24
class GpsLabGeoIP2Extension extends Extension
25
{
26
    /**
27
     * Pattern of database service name.
28
     */
29
    private const SERVICE_NAME = 'geoip2.database.%s_reader';
30
31
    /**
32
     * @param array[]          $configs
33
     * @param ContainerBuilder $container
34
     */
35
    public function load(array $configs, ContainerBuilder $container): void
36
    {
37
        $configuration = $this->getConfiguration($configs, $container);
38
        $config = $this->processConfiguration($configuration, $configs);
39
40
        $default_database = $config['default_database'];
41
        $databases = $config['databases'];
42
43
        // aliases for default database
44
        if (array_key_exists($default_database, $databases)) {
45
            $container->setAlias('geoip2.reader', sprintf(self::SERVICE_NAME, $default_database))->setPublic(true);
46
            $container->setAlias(Reader::class, sprintf(self::SERVICE_NAME, $default_database))->setPublic(true);
47
        }
48
49
        // define database services
50
        foreach ($databases as $name => $database) {
51
            $container
52
                ->setDefinition(sprintf(self::SERVICE_NAME, $name), new Definition(Reader::class))
53
                ->setPublic(true)
54
                ->setLazy(true)
55
                ->setArguments([
56
                    $database['path'],
57
                    $database['locales'],
58
                ]);
59
        }
60
61
        // define MaxMind downloader service
62
        $container
63
            ->setDefinition(MaxMindDownloader::class, new Definition(MaxMindDownloader::class))
64
            ->setPublic(false)
65
            ->setArguments([
66
                new Reference('filesystem'),
67
                new Reference('logger'),
68
            ]);
69
70
        $container->setAlias(Downloader::class, MaxMindDownloader::class)->setPublic(true);
71
72
        // configure update database console command
73
        $container
74
            ->setDefinition(UpdateDatabaseCommand::class, new Definition(UpdateDatabaseCommand::class))
75
            ->setPublic(false)
76
            ->setArguments([
77
                new Reference(Downloader::class),
78
                $databases,
79
            ])
80
            ->addTag('console.command');
81
82
        // configure download database console command
83
        $container
84
            ->setDefinition(DownloadDatabaseCommand::class, new Definition(DownloadDatabaseCommand::class))
85
            ->setPublic(false)
86
            ->setArguments([
87
                new Reference(Downloader::class),
88
            ])
89
            ->addTag('console.command');
90
    }
91
92
    /**
93
     * @param array[]          $config
94
     * @param ContainerBuilder $container
95
     *
96
     * @return Configuration
97
     */
98
    public function getConfiguration(array $config, ContainerBuilder $container): Configuration
99
    {
100
        $cache_dir = null;
101
102
        if ($container->hasParameter('kernel.cache_dir')) {
103
            $cache_dir = $container->getParameter('kernel.cache_dir');
104
        }
105
106
        return new Configuration(is_string($cache_dir) ? $cache_dir : null);
107
    }
108
109
    /**
110
     * @return string
111
     */
112 1
    public function getAlias(): string
113
    {
114 1
        return 'gpslab_geoip';
115
    }
116
}
117