RedisDriver::getCredentialsForm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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