Completed
Push — master ( 3e38c4...3f7c7b )
by Michal
02:49
created

MemcacheDataManager::getDatabaseNameColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace UniMan\Drivers\Memcache;
4
5
use UniMan\Core\DataManager\AbstractDataManager;
6
use UniMan\Core\Exception\NoTablesJustItemsException;
7
use UniMan\Core\Utils\Multisort;
8
use UniMan\Core\Utils\Filter;
9
use Memcache;
10
use Nette\Localization\ITranslator;
11
12
class MemcacheDataManager extends AbstractDataManager
13
{
14
    private $translator;
15
16
    private $connection;
17
18
    private $database;
19
20
    public function __construct(Memcache $connection, ITranslator $translator)
21
    {
22
        $this->connection = $connection;
23
        $this->translator = $translator;
24
    }
25
26
    public function databases(array $sorting = [])
27
    {
28
        $stats = $this->connection->getExtendedStats();
29
        $allSlabs = $this->connection->getExtendedStats('slabs');
30
        $databases = [];
31
        foreach ($stats as $server => $serverInfo) {
32
            $databases[$server] = [
33
                'server' => $server,
34
                'process_id' => $serverInfo['pid'],
35
                'uptime' => $serverInfo['uptime'],
36
                'current_items' => $serverInfo['curr_items'],
37
                'total_items' => $serverInfo['total_items'],
38
                'size' => $serverInfo['bytes'],
39
                'active_slabs' => $allSlabs[$server]['active_slabs'],
40
                'total_malloced' => $allSlabs[$server]['total_malloced'],
41
            ];
42
        }
43
        return Multisort::sort($databases, $sorting);
44
    }
45
46
    protected function getDatabaseNameColumn()
47
    {
48
        return 'server';
49
    }
50
51
    public function tables(array $sorting = [])
52
    {
53
        throw new NoTablesJustItemsException('key', 'all');
54
    }
55
56
    public function itemsCount($type, $table, array $filter = [])
57
    {
58
        if ($table == 'all') {
59
            $slabs = $this->connection->getExtendedStats('slabs');
60
            $databaseSlabs = isset($slabs[$this->database]) ? $slabs[$this->database] : [];
61
            $count = 0;
62 View Code Duplication
            foreach (array_keys($databaseSlabs) as $slabId) {
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...
63
                if (!is_int($slabId)) {
64
                    continue;
65
                }
66
                $slabKeys = $this->getSlabKeys($this->database, $slabId);
67
                $count += count($slabKeys);
68
            }
69
            return $count;
70
        }
71
72
        $stats = $this->connection->getExtendedStats('cachedump', (int)$table);
73
        $keys = isset($stats[$this->database]) ? $stats[$this->database] : [];
74
        return count($keys);
75
    }
76
77
    public function items($type, $table, $page, $onPage, array $filter = [], array $sorting = [])
78
    {
79
        if ($table == 'all') {
80
            $slabs = $this->connection->getExtendedStats('slabs');
81
            $databaseSlabs = isset($slabs[$this->database]) ? $slabs[$this->database] : [];
82
            $keys = [];
83 View Code Duplication
            foreach (array_keys($databaseSlabs) as $slabId) {
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...
84
                if (!is_int($slabId)) {
85
                    continue;
86
                }
87
                $keys = array_merge($keys, $this->getSlabKeys($this->database, $slabId));
88
            }
89
        } else {
90
            $keys = $this->getSlabKeys($this->database, $table);
91
        }
92
93
        $items = [];
94
        foreach ($keys as $key => $info) {
95
            $flags = false;
96
            $item = [
97
                'key' => $key,
98
                'value' => $this->connection->get($key, $flags),
99
                'length' => $info[0],
100
                'expiration' => ($info[1] - time()) > 0 ? $info[1] - time() : null,
101
                'compressed' => $flags == MEMCACHE_COMPRESSED ? $this->translator->translate('core.yes') : $this->translator->translate('core.no'),
102
            ];
103
            if (Filter::apply($item, $filter)) {
104
                $items[$key] = $item;
105
            }
106
        }
107
108
        $items = Multisort::sort($items, $sorting);
109
        return array_slice($items, ($page - 1) * $onPage, $onPage, true);
110
    }
111
112
    public function deleteItem($type, $table, $item)
113
    {
114
        return $this->connection->delete($item);
115
    }
116
117
    public function selectDatabase($database)
118
    {
119
        return $this->database = $database;
120
    }
121
122
    private function getSlabKeys($database, $slabId)
123
    {
124
        $cacheDump = $this->connection->getExtendedStats('cachedump', (int) $slabId);
125
        $keys = isset($cacheDump[$database]) ? $cacheDump[$database] : [];
126
        if ($keys === false) {
127
            return [];
128
        }
129
        return $keys;
130
    }
131
}
132