Completed
Push — master ( ee9a51...a02f02 )
by Tobias
21:27 queued 19:30
created

MemcacheFactory::getAdapter()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 16
cp 0
rs 9.6666
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 20
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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