Completed
Push — master ( 3f7c7b...59875d )
by Michal
02:57
created

MemcacheDriver   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 0
loc 60
rs 10
c 0
b 0
f 0
ccs 12
cts 12
cp 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A extensions() 0 4 1
A type() 0 4 1
A getCredentialsForm() 0 4 1
A getHeaderManager() 0 4 1
A getPermissions() 0 4 1
A defaultCredentials() 0 6 1
A connect() 0 11 3
A getFormManager() 0 4 1
A getDataManager() 0 4 1
1
<?php
2
3
namespace UniMan\Drivers\Memcache;
4
5
use UniMan\Core\Driver\AbstractDriver;
6
use UniMan\Core\Exception\ConnectException;
7
use UniMan\Drivers\Memcache\Forms\MemcacheCredentialsForm;
8
use Memcache;
9
10
class MemcacheDriver extends AbstractDriver
11
{
12
    const TYPE_KEY = 'key';
13
14
    private $connection;
15
16 2
    public function extensions()
17
    {
18 2
        return ['memcache'];
19
    }
20
21 2
    public function type()
22
    {
23 2
        return 'memcache';
24
    }
25
26 2
    public function defaultCredentials()
27
    {
28
        return [
29 2
            'servers' => 'localhost:11211',
30
        ];
31
    }
32
33 2
    public function getCredentialsForm()
34
    {
35 2
        return new MemcacheCredentialsForm();
36
    }
37
38
    public function connect(array $credentials)
39
    {
40
        $this->connection = new Memcache();
41
        $servers = array_map('trim', explode("\n", trim($credentials['servers'])));
42
        foreach ($servers as $server) {
43
            list($host, $port) = explode(':', $server, 2);
44
            if (!$this->connection->addserver($host, $port)) {
45
                throw new ConnectException("Couldn't connect to $host:$port");
46
            }
47
        }
48
    }
49
50
    protected function getFormManager()
51
    {
52
        return new MemcacheFormManager($this->connection);
53
    }
54
55 2
    protected function getHeaderManager()
56
    {
57 2
        return new MemcacheHeaderManager();
58
    }
59
60 2
    protected function getPermissions()
61
    {
62 2
        return new MemcachePermissions();
63
    }
64
65
    protected function getDataManager()
66
    {
67
        return new MemcacheDataManager($this->connection, $this->translator);
68
    }
69
}
70