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

MemcacheFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 27.27 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 12
loc 44
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getAdapter() 0 18 4
A configureOptionResolver() 12 12 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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