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

RedisFactory::getAdapter()   B

Complexity

Conditions 9
Paths 12

Size

Total Lines 34

Duplication

Lines 6
Ratio 17.65 %

Code Coverage

Tests 10
CRAP Score 16.1088

Importance

Changes 0
Metric Value
dl 6
loc 34
ccs 10
cts 18
cp 0.5556
rs 8.0555
c 0
b 0
f 0
cc 9
nc 12
nop 1
crap 16.1088
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