PredisFactory::configureOptionResolver()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 9.6
c 0
b 0
f 0
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\Predis\PredisCachePool;
15
use Cache\Namespaced\NamespacedCachePool;
16
use Predis\Client;
17
use Symfony\Component\OptionsResolver\OptionsResolver;
18
19
/**
20
 * @author Tobias Nyholm <[email protected]>
21
 * @author Aaron Scherer <[email protected]>
22
 */
23
final class PredisFactory extends AbstractDsnAdapterFactory
24
{
25
    protected static $dependencies = [
26
        ['requiredClass' => 'Cache\Adapter\Predis\PredisCachePool', 'packageName' => 'cache/predis-adapter'],
27
    ];
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 1
    public function getAdapter(array $config)
33
    {
34 1
        $dsn = $this->getDsn();
35 1
        if (empty($dsn)) {
36 1
            $client = new Client(
37
                [
38 1
                    'scheme' => $config['scheme'],
39 1
                    'host' => $config['host'],
40 1
                    'port' => $config['port'],
41 1
                    'persistent' => $config['persistent'],
42
                ]
43
            );
44
        } else {
45
            $client = new Client($dsn->getDsn());
46
        }
47
48 1
        $pool = new PredisCachePool($client);
49
50 1
        if (null !== $config['pool_namespace']) {
51
            $pool = new NamespacedCachePool($pool, $config['pool_namespace']);
52
        }
53
54 1
        return $pool;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 3
    protected static function configureOptionResolver(OptionsResolver $resolver)
61
    {
62 3
        parent::configureOptionResolver($resolver);
63
64 3
        $resolver->setDefaults(
65
            [
66 3
                'host' => '127.0.0.1',
67
                'port' => '6379',
68
                'scheme' => 'tcp',
69
                'pool_namespace' => null,
70
                'persistent' => false,
71
            ]
72
        );
73
74 3
        $resolver->setAllowedTypes('host', ['string']);
75 3
        $resolver->setAllowedTypes('port', ['string', 'int']);
76 3
        $resolver->setAllowedTypes('scheme', ['string']);
77 3
        $resolver->setAllowedTypes('pool_namespace', ['string', 'null']);
78 3
        $resolver->setAllowedTypes('persistent', ['bool']);
79 3
    }
80
}
81