1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Superdesk Publisher Geo IP Bundle. |
7
|
|
|
* |
8
|
|
|
* Copyright 2019 Sourcefabric z.ú. and contributors. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please see the |
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @copyright 2019 Sourcefabric z.ú |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Bundle\GeoIPBundle\DependencyInjection; |
18
|
|
|
|
19
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
20
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
21
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
22
|
|
|
|
23
|
|
|
class Configuration implements ConfigurationInterface |
24
|
|
|
{ |
25
|
|
|
private const URL = 'https://download.maxmind.com/app/geoip_download?edition_id=%s&license_key=%s&suffix=tar.gz'; |
26
|
|
|
|
27
|
|
|
private const PATH = '%s/%s.mmdb'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public function getConfigTreeBuilder() |
33
|
|
|
{ |
34
|
|
|
$treeBuilder = new TreeBuilder('swp_geo_ip'); |
35
|
|
|
$rootNode = $treeBuilder->getRootNode(); |
36
|
|
|
$this->normalizeDatabaseUrl($rootNode); |
|
|
|
|
37
|
|
|
$this->normalizeDatabasePath($rootNode); |
|
|
|
|
38
|
|
|
$rootNode |
39
|
|
|
->beforeNormalization() |
40
|
|
|
->ifTrue(static function ($v): bool { |
41
|
|
|
return |
42
|
|
|
is_array($v) && |
43
|
|
|
array_key_exists('license_key', $v) && |
44
|
|
|
array_key_exists('edition_id', $v); |
45
|
|
|
}) |
46
|
|
|
->then(static function (array $v): array { |
47
|
|
|
$v['database_url'] = sprintf(self::URL, urlencode($v['edition_id']), urlencode($v['license_key'])); |
48
|
|
|
|
49
|
|
|
return $v; |
50
|
|
|
}); |
51
|
|
|
|
52
|
|
|
$rootNode |
53
|
|
|
->children() |
54
|
|
|
->scalarNode('license_key') |
55
|
|
|
->cannotBeEmpty() |
56
|
|
|
->isRequired() |
57
|
|
|
->info('MaxMind license key') |
58
|
|
|
->end() |
59
|
|
|
->scalarNode('database_url') |
60
|
|
|
->cannotBeEmpty() |
61
|
|
|
->isRequired() |
62
|
|
|
->end() |
63
|
|
|
->scalarNode('edition_id') |
64
|
|
|
->cannotBeEmpty() |
65
|
|
|
->isRequired() |
66
|
|
|
->info('MaxMind database id') |
67
|
|
|
->end() |
68
|
|
|
->scalarNode('database_path') |
69
|
|
|
->cannotBeEmpty() |
70
|
|
|
->isRequired() |
71
|
|
|
->info('Path to the downloaded GeoIP2 database') |
72
|
|
|
->end() |
73
|
|
|
->end(); |
74
|
|
|
|
75
|
|
|
return $treeBuilder; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function normalizeDatabaseUrl(ArrayNodeDefinition $node): void |
79
|
|
|
{ |
80
|
|
|
$node |
81
|
|
|
->beforeNormalization() |
82
|
|
|
->ifTrue(static function ($v): bool { |
83
|
|
|
return |
84
|
|
|
is_array($v) && |
85
|
|
|
array_key_exists('license_key', $v) && |
86
|
|
|
array_key_exists('edition_id', $v); |
87
|
|
|
}) |
88
|
|
|
->then(static function (array $v): array { |
89
|
|
|
$v['database_url'] = sprintf(self::URL, urlencode($v['edition_id']), urlencode($v['license_key'])); |
90
|
|
|
|
91
|
|
|
return $v; |
92
|
|
|
}); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function normalizeDatabasePath(ArrayNodeDefinition $node): void |
96
|
|
|
{ |
97
|
|
|
$node |
98
|
|
|
->beforeNormalization() |
99
|
|
|
->ifTrue(static function ($v): bool { |
100
|
|
|
return is_array($v) && array_key_exists('edition_id', $v); |
101
|
|
|
}) |
102
|
|
|
->then(static function (array $v): array { |
103
|
|
|
$v['database_path'] = sprintf(self::PATH, '%kernel.project_dir%/var', $v['edition_id']); |
104
|
|
|
|
105
|
|
|
return $v; |
106
|
|
|
}); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.