RedisFactory::configureOptionResolver()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
ccs 9
cts 9
cp 1
cc 1
nc 1
nop 1
crap 1
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 1
    public function getAdapter(array $config)
33
    {
34 1
        $client = new \Redis();
35
36 1
        $dsn = $this->getDsn();
37 1
        if (empty($dsn)) {
38 1 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 1
                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 1 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 1
        $pool = new RedisCachePool($client);
59
60 1
        if (null !== $config['pool_namespace']) {
61
            $pool = new NamespacedCachePool($pool, $config['pool_namespace']);
62
        }
63
64 1
        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