MemcachedFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 69.23%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 5
dl 10
loc 60
ccs 18
cts 26
cp 0.6923
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configureOptionResolver() 0 18 1
B getAdapter() 10 28 6

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\Memcached\MemcachedCachePool;
15
use Cache\AdapterBundle\ProviderHelper\Memcached;
16
use Cache\Namespaced\NamespacedCachePool;
17
use Symfony\Component\OptionsResolver\OptionsResolver;
18
19
/**
20
 * @author Tobias Nyholm <[email protected]>
21
 */
22
final class MemcachedFactory extends AbstractAdapterFactory
23
{
24
    protected static $dependencies = [
25
        ['requiredClass' => 'Cache\Adapter\Memcached\MemcachedCachePool', 'packageName' => 'cache/memcached-adapter'],
26
    ];
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 1
    public function getAdapter(array $config)
32
    {
33 1
        $client = new Memcached($config['persistent_id']);
34 1
        $client->addServer($config['host'], $config['port']);
35
36 1 View Code Duplication
        foreach ($config['redundant_servers'] as $server) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
37
            if (!isset($server['host'])) {
38
                continue;
39
            }
40
            $port = $config['port'];
41
            if (isset($server['port'])) {
42
                $port = $server['port'];
43
            }
44
            $client->addServer($server['host'], $port);
45
        }
46
47 1
        foreach ($config['driver_options'] as $constant => $value) {
48
            $client->setOption(constant($constant), $value);
49
        }
50
51 1
        $pool = new MemcachedCachePool($client);
52
53 1
        if (null !== $config['pool_namespace']) {
54
            $pool = new NamespacedCachePool($pool, $config['pool_namespace']);
55
        }
56
57 1
        return $pool;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 3
    protected static function configureOptionResolver(OptionsResolver $resolver)
64
    {
65 3
        $resolver->setDefaults([
66 3
            'persistent_id' => null,
67
            'host' => '127.0.0.1',
68
            'port' => 11211,
69
            'pool_namespace' => null,
70
            'redundant_servers' => [],
71
            'driver_options' => [],
72
        ]);
73
74 3
        $resolver->setAllowedTypes('persistent_id', ['string', 'null']);
75 3
        $resolver->setAllowedTypes('host', ['string']);
76 3
        $resolver->setAllowedTypes('port', ['string', 'int']);
77 3
        $resolver->setAllowedTypes('pool_namespace', ['string', 'null']);
78 3
        $resolver->setAllowedTypes('redundant_servers', ['array']);
79 3
        $resolver->setAllowedTypes('driver_options', ['array']);
80 3
    }
81
}
82