1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of php-cache organization. |
5
|
|
|
* |
6
|
|
|
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Cache\AdapterBundle\Factory; |
13
|
|
|
|
14
|
|
|
use Cache\Adapter\Memcache\MemcacheCachePool; |
15
|
|
|
use Memcache; |
16
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Nicholas Ruunu <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
final class MemcacheFactory extends AbstractAdapterFactory |
22
|
|
|
{ |
23
|
|
|
protected static $dependencies = [ |
24
|
|
|
['requiredClass' => 'Cache\Adapter\Memcache\MemcacheCachePool', 'packageName' => 'cache/memcache-adapter'], |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function getAdapter(array $config) |
31
|
|
|
{ |
32
|
|
|
$client = new Memcache(); |
33
|
|
|
$client->connect($config['host'], $config['port']); |
34
|
|
|
|
35
|
|
|
foreach ($config['redundant_servers'] as $server) { |
36
|
|
|
if (!isset($server['host'])) { |
37
|
|
|
continue; |
38
|
|
|
} |
39
|
|
|
$port = $config['port']; |
40
|
|
|
if (isset($server['port'])) { |
41
|
|
|
$port = $server['port']; |
42
|
|
|
} |
43
|
|
|
$client->addserver($server['host'], $port); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return new MemcacheCachePool($client); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
protected static function configureOptionResolver(OptionsResolver $resolver) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$resolver->setDefaults([ |
55
|
|
|
'host' => '127.0.0.1', |
56
|
|
|
'port' => 11211, |
57
|
|
|
'redundant_servers' => [], |
58
|
|
|
]); |
59
|
|
|
|
60
|
|
|
$resolver->setAllowedTypes('host', ['string']); |
61
|
|
|
$resolver->setAllowedTypes('port', ['string', 'int']); |
62
|
|
|
$resolver->setAllowedTypes('redundant_servers', ['array']); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
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.