RedisDriver   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 93.1%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 8
dl 0
loc 81
ccs 27
cts 29
cp 0.931
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A check() 0 4 2
A extensions() 0 4 1
A classes() 0 4 1
A type() 0 4 1
A defaultCredentials() 0 8 1
A getCredentialsForm() 0 4 1
A connect() 0 8 2
A getFormManager() 0 4 1
A getHeaderManager() 0 4 1
A getPermissions() 0 4 1
A getDataManager() 0 4 1
1
<?php
2
3
namespace UniMan\Drivers\Redis;
4
5
use Nette\Localization\ITranslator;
6
use RedisException;
7
use RedisProxy\RedisProxy;
8
use UniMan\Core\Driver\AbstractDriver;
9
use UniMan\Core\Exception\ConnectException;
10
use UniMan\Drivers\Redis\Forms\RedisCredentialsForm;
11
use UniMan\Drivers\Redis\RedisDatabaseAliasStorage;
12
13
class RedisDriver extends AbstractDriver
14
{
15
    const TYPE_KEY = 'key';
16
    const TYPE_HASH = 'hash';
17
    const TYPE_SET = 'set';
18
    const TYPE_LIST = 'list';
19
    const TYPE_SORTED_SET = 'sorted_set';
20
21
    private $connection;
22
23
    private $databaseAliasStorage;
24
25 2
    public function __construct(ITranslator $translator, RedisDatabaseAliasStorage $databaseAliasStorage)
26
    {
27 2
        parent::__construct($translator);
28 2
        $this->databaseAliasStorage = $databaseAliasStorage;
29 2
    }
30
31 2
    public function check()
32
    {
33 2
        return extension_loaded('redis') || class_exists('Predis\Client');
34
    }
35
36 2
    public function extensions()
37
    {
38 2
        return ['redis'];
39
    }
40
41 2
    public function classes()
42
    {
43 2
        return ['Predis\Client'];
44
    }
45
46 2
    public function type()
47
    {
48 2
        return 'redis';
49
    }
50
51 2
    public function defaultCredentials()
52
    {
53
        return [
54 2
            'host' => 'localhost',
55
            'port' => '6379',
56
            'database' => '0',
57
        ];
58
    }
59
60 2
    public function getCredentialsForm()
61
    {
62 2
        return new RedisCredentialsForm();
63
    }
64
65 2
    public function connect(array $credentials)
66
    {
67
        try {
68 2
            $this->connection = new RedisProxy($credentials['host'], $credentials['port'], $credentials['database']);
69
        } catch (RedisException $e) {
70
            throw new ConnectException($e->getMessage());
71
        }
72 2
    }
73
74 2
    protected function getFormManager()
75
    {
76 2
        return new RedisFormManager($this->connection, $this->databaseAliasStorage);
77
    }
78
79 2
    protected function getHeaderManager()
80
    {
81 2
        return new RedisHeaderManager();
82
    }
83
84 2
    protected function getPermissions()
85
    {
86 2
        return new RedisPermissions();
87
    }
88
89 2
    protected function getDataManager()
90
    {
91 2
        return new RedisDataManager($this->connection, $this->databaseAliasStorage);
92
    }
93
}
94