1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Azine\GeoBlockingBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
6
|
|
|
use Symfony\Component\Config\FileLocator; |
7
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
8
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This is the class that loads and manages your bundle configuration |
12
|
|
|
* |
13
|
|
|
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} |
14
|
|
|
*/ |
15
|
|
|
class AzineGeoBlockingExtension extends Extension |
16
|
|
|
{ |
17
|
|
|
const COUNTRIES = "countries"; |
18
|
|
|
const ROUTES = "routes"; |
19
|
|
|
const WHITELIST = "whitelist"; |
20
|
|
|
const BLACKLIST = "blacklist"; |
21
|
|
|
/** |
22
|
|
|
* {@inheritDoc} |
23
|
|
|
*/ |
24
|
4 |
|
public function load(array $configs, ContainerBuilder $container) |
25
|
|
|
{ |
26
|
4 |
|
$configuration = new Configuration(); |
27
|
4 |
|
$config = $this->processConfiguration($configuration, $configs); |
28
|
|
|
|
29
|
4 |
View Code Duplication |
if (array_key_exists(self::COUNTRIES, $config)) { |
|
|
|
|
30
|
4 |
|
$config[self::COUNTRIES] = $this->checkExclusiveness($config[self::COUNTRIES]); |
31
|
4 |
|
} |
32
|
|
|
|
33
|
4 |
View Code Duplication |
if (array_key_exists(self::ROUTES, $config)) { |
|
|
|
|
34
|
4 |
|
$config[self::ROUTES] = $this->checkExclusiveness($config[self::ROUTES]); |
35
|
4 |
|
} |
36
|
|
|
|
37
|
4 |
|
$prefix = 'azine_geo_blocking_'; |
38
|
4 |
|
$container->setParameter($prefix."enabled", $config["enabled"]); |
39
|
4 |
|
$container->setParameter($prefix."block_anonymouse_users_only", $config["block_anonymouse_users_only"]); |
40
|
4 |
|
$container->setParameter($prefix."countries_whitelist", $config["countries"]["whitelist"]); |
41
|
4 |
|
$container->setParameter($prefix."countries_blacklist", $config["countries"]["blacklist"]); |
42
|
4 |
|
$container->setParameter($prefix."routes_whitelist", $config["routes"]["whitelist"]); |
43
|
4 |
|
$container->setParameter($prefix."routes_blacklist", $config["routes"]["blacklist"]); |
44
|
4 |
|
$container->setAlias($prefix."lookup_adapter", $config["lookup_adapter"]); |
45
|
4 |
|
$container->setParameter($prefix."allow_private_ips", $config["allow_private_ips"]); |
46
|
4 |
|
$container->setParameter($prefix."access_denied_view", $config["access_denied_view"]); |
47
|
4 |
|
$container->setParameter($prefix."login_route", $config["login_route"]); |
48
|
4 |
|
$container->setParameter($prefix."ip_whitelist", $config['ip_whitelist']); |
49
|
4 |
|
$container->setParameter($prefix."logBlockedRequests", $config['logBlockedRequests']); |
50
|
4 |
|
$container->setParameter($prefix."allow_search_bots", $config['allow_search_bots']); |
51
|
4 |
|
$container->setParameter($prefix."search_bot_domains", $config['search_bot_domains']); |
52
|
4 |
|
$container->setParameter($prefix."allow_by_cookie", $config['allow_by_cookie']); |
53
|
4 |
|
$container->setParameter($prefix."allow_by_cookie_name", $config['allow_by_cookie_name']);
|
54
|
|
|
|
55
|
4 |
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
56
|
4 |
|
$loader->load('services.yml'); |
57
|
|
|
|
58
|
4 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Make sure that only either whitelist or blacklist is present. If both are present (and both contain values), the blacklist is cleared. |
62
|
|
|
* @param unknown_type $config |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
4 |
|
private function checkExclusiveness(array $config) |
66
|
|
|
{ |
67
|
4 |
|
$whiteListIsSet = array_key_exists(self::WHITELIST, $config) && is_array($config[self::WHITELIST]) && !empty($config[self::WHITELIST]); |
68
|
4 |
|
$blackListIsSet = array_key_exists(self::BLACKLIST, $config) && is_array($config[self::BLACKLIST]) && !empty($config[self::BLACKLIST]); |
69
|
4 |
|
if ($whiteListIsSet && $blackListIsSet) { |
70
|
1 |
|
$config[self::BLACKLIST] = array(); |
71
|
2 |
|
} |
72
|
|
|
|
73
|
4 |
|
return $config; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.