Completed
Push — master ( cff003...e41ce1 )
by Michal
05:27
created

RabbitMQDataManager::getMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Adminerng\Drivers\RabbitMQ;
4
5
use Adminerng\Core\DataManager\AbstractDataManager;
6
use Adminerng\Core\Helper\Formatter;
7
use Adminerng\Core\Multisort;
8
use Adminerng\Core\Utils\Filter;
9
use Nette\Localization\ITranslator;
10
use PhpAmqpLib\Connection\AMQPStreamConnection;
11
12
class RabbitMQDataManager extends AbstractDataManager
13
{
14
    private $translator;
15
16
    private $formatter;
17
18
    /** @var AMQPStreamConnection */
19
    private $connection;
20
21
    private $credentials = [];
22
23
    private $client;
24
25
    private $vhost;
26
27
    public function __construct(array $credentials, RabbitMQManagementApiClient $client, ITranslator $translator, Formatter $formatter)
28
    {
29
        $this->credentials = $credentials;
30
        $this->client = $client;
31
        $this->translator = $translator;
32
        $this->formatter = $formatter;
33
    }
34
35
    public function getConnection()
36
    {
37
        return $this->connection;
38
    }
39
40
    public function databases(array $sorting = [])
41
    {
42
        $vhosts = [];
43
        foreach ($this->client->getVhosts($this->credentials['user']) as $vhost) {
44
            $vhosts[$vhost['name']] = [
45
                'vhost' => $vhost['name'],
46
                'queues' => count($this->client->getQueues($vhost['name'])),
47
                'messages' => isset($vhost['messages']) ? $vhost['messages'] : 0,
48
            ];
49
        }
50
        return Multisort::sort($vhosts, $sorting);
51
    }
52
53
    public function selectDatabase($vhost)
54
    {
55
        $this->vhost = $vhost;
56
        $this->connection = new AMQPStreamConnection(
57
            $this->credentials['host'],
58
            $this->credentials['port'],
59
            $this->credentials['user'],
60
            $this->credentials['password'],
61
            $vhost
62
        );
63
        return $this->connection;
64
    }
65
66
    public function tables(array $sorting = [])
67
    {
68
        $tables = [
69
            RabbitMQDriver::TYPE_QUEUE => [],
70
        ];
71
        foreach ($this->client->getQueues($this->vhost) as $queue) {
72
            $tables[RabbitMQDriver::TYPE_QUEUE][$queue['name']] = [
73
                'queue' => $queue['name'],
74
                'number_of_items' => $queue['messages'],
75
                'size' => $queue['message_bytes'],
76
            ];
77
        }
78
        return [
79
            RabbitMQDriver::TYPE_QUEUE => Multisort::sort($tables[RabbitMQDriver::TYPE_QUEUE], $sorting),
80
        ];
81
    }
82
83
    public function itemsCount($type, $table, array $filter = [])
84
    {
85
        if ($type != RabbitMQDriver::TYPE_QUEUE) {
86
            return 0;
87
        }
88
        if (!$filter) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $filter of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
89
            foreach ($this->client->getQueues($this->vhost) as $queue) {
90
                if ($queue['name'] == $table) {
91
                    return $queue['messages'];
92
                }
93
            }
94
        }
95
        $totalCount = 0;
96
        foreach ($this->client->getMessages($this->vhost, $table) as $message) {
97
            $item = [
98
                'message_body' => $message['payload'],
99
                'length' => $message['payload_bytes'],
100
                'is_truncated' => false ? $this->translator->translate('core.yes') : $this->translator->translate('core.no'),
101
                'content_encoding' => $message['payload_encoding'],
102
                'redelivered' => $message['redelivered'] ? $this->translator->translate('core.yes') : $this->translator->translate('core.no'),
103
            ];
104
            if (Filter::apply($item, $filter)) {
105
                $totalCount++;
106
            }
107
        }
108
109
        return $totalCount;
110
    }
111
112
    public function items($type, $table, $page, $onPage, array $filter = [], array $sorting = [])
113
    {
114
        if ($type != RabbitMQDriver::TYPE_QUEUE) {
115
            return [];
116
        }
117
        $items = [];
118
        foreach ($this->client->getMessages($this->vhost, $table) as $message) {
119
            $item = [
120
                'message_body' => $message['payload'],
121
                'length' => $message['payload_bytes'],
122
                'is_truncated' => false ? $this->translator->translate('core.yes') : $this->translator->translate('core.no'),
123
                'content_encoding' => $message['payload_encoding'],
124
                'redelivered' => $message['redelivered'] ? $this->translator->translate('core.yes') : $this->translator->translate('core.no'),
125
            ];
126
            if (!Filter::apply($item, $filter)) {
127
                continue;
128
            }
129
            $items[$message['payload']] = $item;
130
        }
131
        $items = Multisort::sort($items, $sorting);
132
        return array_slice($items, ($page - 1) * $onPage, $onPage, true);
133
    }
134
135
    public function deleteTable($type, $queue)
136
    {
137
        $channel = $this->connection->channel();
138
        $channel->queue_delete($queue);
139
        $channel->close();
140
        $this->connection->close();
141
        return true;
142
    }
143
}
144