Completed
Pull Request — master (#21)
by Michal
02:30
created

MemcacheDataManager::selectDatabase()   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 1
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
    public function tables(array $sorting = [])
47
    {
48
        throw new NoTablesJustItemsException('key', 'all');
49
    }
50
51
    public function itemsCount($type, $table, array $filter = [])
52
    {
53
        if ($table == 'all') {
54
            $slabs = $this->connection->getExtendedStats('slabs');
55
            $databaseSlabs = isset($slabs[$this->database]) ? $slabs[$this->database] : [];
56
            $count = 0;
57 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...
58
                if (!is_int($slabId)) {
59
                    continue;
60
                }
61
                $slabKeys = $this->getSlabKeys($this->database, $slabId);
62
                $count += count($slabKeys);
63
            }
64
            return $count;
65
        }
66
67
        $stats = $this->connection->getExtendedStats('cachedump', (int)$table);
68
        $keys = isset($stats[$this->database]) ? $stats[$this->database] : [];
69
        return count($keys);
70
    }
71
72
    public function items($type, $table, $page, $onPage, array $filter = [], array $sorting = [])
73
    {
74
        if ($table == 'all') {
75
            $slabs = $this->connection->getExtendedStats('slabs');
76
            $databaseSlabs = isset($slabs[$this->database]) ? $slabs[$this->database] : [];
77
            $keys = [];
78 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...
79
                if (!is_int($slabId)) {
80
                    continue;
81
                }
82
                $keys = array_merge($keys, $this->getSlabKeys($this->database, $slabId));
83
            }
84
        } else {
85
            $keys = $this->getSlabKeys($this->database, $table);
86
        }
87
88
        $items = [];
89
        foreach ($keys as $key => $info) {
90
            $flags = false;
91
            $item = [
92
                'key' => $key,
93
                'value' => $this->connection->get($key, $flags),
94
                'length' => $info[0],
95
                'expiration' => ($info[1] - time()) > 0 ? $info[1] - time() : null,
96
                'compressed' => $flags == MEMCACHE_COMPRESSED ? $this->translator->translate('core.yes') : $this->translator->translate('core.no'),
97
            ];
98
            if (Filter::apply($item, $filter)) {
99
                $items[$key] = $item;
100
            }
101
        }
102
103
        $items = Multisort::sort($items, $sorting);
104
        return array_slice($items, ($page - 1) * $onPage, $onPage, true);
105
    }
106
107
    public function deleteItem($type, $table, $item)
108
    {
109
        return $this->connection->delete($item);
110
    }
111
112
    public function selectDatabase($database)
113
    {
114
        return $this->database = $database;
115
    }
116
117
    private function getSlabKeys($database, $slabId)
118
    {
119
        $cacheDump = $this->connection->getExtendedStats('cachedump', (int) $slabId);
120
        $keys = isset($cacheDump[$database]) ? $cacheDump[$database] : [];
121
        if ($keys === false) {
122
            return [];
123
        }
124
        return $keys;
125
    }
126
}
127