1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EmanueleMinotto\SafeBrowsingBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\FileLocator; |
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
7
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
8
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
9
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* {@inheritdoc} |
13
|
|
|
*/ |
14
|
|
|
class SafeBrowsingExtension extends Extension |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Main service ID. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
const SERVICE_ID = 'safe_browsing_bundle.validator.constraints.safe_validator'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
15 |
|
public function load(array $configs, ContainerBuilder $container) |
27
|
|
|
{ |
28
|
15 |
|
$config = $this->processConfiguration(new Configuration(), $configs); |
29
|
|
|
|
30
|
15 |
|
$loader = new Loader\XmlFileLoader( |
31
|
15 |
|
$container, |
32
|
15 |
|
new FileLocator(__DIR__.'/../Resources/config') |
33
|
15 |
|
); |
34
|
15 |
|
$loader->load('services.xml'); |
35
|
|
|
|
36
|
15 |
|
$this->setCache($config['cache'], $container); |
37
|
15 |
|
$this->setConfigs($config['configs'], $container); |
38
|
15 |
|
$this->setHttpClient($config['http_client'], $container); |
39
|
15 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Set cache directory. |
43
|
|
|
* |
44
|
|
|
* @param string $cache |
45
|
|
|
* @param ContainerBuilder $container |
46
|
|
|
*/ |
47
|
15 |
View Code Duplication |
private function setCache($cache, ContainerBuilder $container) |
|
|
|
|
48
|
|
|
{ |
49
|
15 |
|
if (!$cache) { |
50
|
12 |
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$container |
54
|
3 |
|
->getDefinition(self::SERVICE_ID) |
55
|
3 |
|
->addMethodCall('setCache', [new Reference($cache)]); |
56
|
3 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Initialize service configuration. |
60
|
|
|
* |
61
|
|
|
* @param array $configs |
62
|
|
|
* @param ContainerBuilder $container |
63
|
|
|
*/ |
64
|
15 |
|
private function setConfigs(array $configs, ContainerBuilder $container) |
65
|
|
|
{ |
66
|
|
|
$container |
67
|
15 |
|
->getDefinition(self::SERVICE_ID) |
68
|
15 |
|
->replaceArgument(0, $configs); |
69
|
15 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Set HTTP client. |
73
|
|
|
* |
74
|
|
|
* @param string $httpClient |
75
|
|
|
* @param ContainerBuilder $container |
76
|
|
|
*/ |
77
|
15 |
View Code Duplication |
private function setHttpClient($httpClient, ContainerBuilder $container) |
|
|
|
|
78
|
|
|
{ |
79
|
15 |
|
if (!$httpClient) { |
80
|
12 |
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$container |
84
|
3 |
|
->getDefinition(self::SERVICE_ID) |
85
|
3 |
|
->addMethodCall('setHttpClient', [new Reference($httpClient)]); |
86
|
3 |
|
} |
87
|
|
|
} |
88
|
|
|
|
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.