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

RedisFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 9.09 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 70.37%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 6
dl 6
loc 66
ccs 19
cts 27
cp 0.7037
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configureOptionResolver() 0 18 1
B getAdapter() 6 34 9

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\Redis\RedisCachePool;
15
use Cache\AdapterBundle\Exception\ConnectException;
16
use Cache\Namespaced\NamespacedCachePool;
17
use Symfony\Component\OptionsResolver\OptionsResolver;
18
19
/**
20
 * @author Tobias Nyholm <[email protected]>
21
 * @author Aaron Scherer <[email protected]>
22
 */
23
final class RedisFactory extends AbstractDsnAdapterFactory
24
{
25
    protected static $dependencies = [
26
        ['requiredClass' => 'Cache\Adapter\Redis\RedisCachePool', 'packageName' => 'cache/redis-adapter'],
27
    ];
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 3
    public function getAdapter(array $config)
33
    {
34 3
        $client = new \Redis();
35
36 3
        $dsn = $this->getDsn();
37 3
        if (empty($dsn)) {
38 3 View Code Duplication
            if (false === $client->connect($config['host'], $config['port'])) {
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...
39 3
                throw new ConnectException(sprintf('Could not connect to Redis database on "%s:%s".', $config['host'], $config['port']));
40
            }
41
        } else {
42
            if (false === $client->connect($dsn->getFirstHost(), $dsn->getFirstPort())) {
43
                throw new ConnectException(sprintf('Could not connect to Redis database on "%s:%s".', $dsn->getFirstHost(), $dsn->getFirstPort()));
44
            }
45
46
            if (!empty($dsn->getPassword())) {
47
                if (false === $client->auth($dsn->getPassword())) {
48
                    throw new ConnectException('Could not connect authenticate connection to Redis database.');
49
                }
50
            }
51
            $config['database'] = $dsn->getDatabase();
52
        }
53
54 3 View Code Duplication
        if (null !== $config['database'] && false === $client->select($config['database'])) {
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...
55
            throw new ConnectException(sprintf('Could not select Redis database with index "%s".', $config['database']));
56
        }
57
58 3
        $pool = new RedisCachePool($client);
59
60 3
        if (null !== $config['pool_namespace']) {
61
            $pool = new NamespacedCachePool($pool, $config['pool_namespace']);
62
        }
63
64 3
        return $pool;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 3
    protected static function configureOptionResolver(OptionsResolver $resolver)
71
    {
72 3
        parent::configureOptionResolver($resolver);
73
74 3
        $resolver->setDefaults(
75
            [
76 3
                'host'           => '127.0.0.1',
77
                'port'           => '6379',
78
                'pool_namespace' => null,
79
                'database'       => null,
80
            ]
81
        );
82
83 3
        $resolver->setAllowedTypes('host', ['string']);
84 3
        $resolver->setAllowedTypes('port', ['string', 'int']);
85 3
        $resolver->setAllowedTypes('pool_namespace', ['string', 'null']);
86 3
        $resolver->setAllowedTypes('database', ['int', 'null']);
87 3
    }
88
}
89