Completed
Push — master ( fb58ff...f5adf6 )
by Michal
02:16
created

RabbitMQManagementApiClient   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 33.96%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 1
dl 0
loc 102
rs 10
c 0
b 0
f 0
ccs 18
cts 53
cp 0.3396

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A overview() 0 4 1
A getVhosts() 0 19 5
A getQueues() 0 9 2
A getMessages() 0 22 4
A call() 0 21 4
1
<?php
2
3
namespace UniMan\Drivers\RabbitMQ;
4
5
use UniMan\Core\Exception\ConnectException;
6
7
class RabbitMQManagementApiClient
8
{
9
    private $baseUrl;
10
11 2
    public function __construct($host = 'localhost', $port = '15672', $user = 'guest', $password = 'guest')
12
    {
13 2
        $this->baseUrl = 'http://' . $user . ':' . $password . '@' . $host . ':' . $port;
14 2
    }
15
16
    /**
17
     * @return just for check if user authorised
18
     */
19 2
    public function overview()
20
    {
21 2
        return $this->call('/api/overview');
22
    }
23
24
    /**
25
     * returns list of vhosts visible for user
26
     * @param string|null $user
27
     * @return array
28
     */
29
    public function getVhosts($user = null)
30
    {
31
        $result = $this->call('/api/vhosts');
32
        $vhosts = [];
33
        foreach ($result as $item) {
34
            if ($user === null) {
35
                $vhosts[] = $item;
36
                continue;
37
            }
38
            $permissions = $this->call('/api/vhosts/' . urlencode($item['name']) . '/permissions');
39
            foreach ($permissions as $permission) {
40
                if ($permission['user'] == $user) {
41
                    $vhosts[] = $item;
42
                    continue;
43
                }
44
            }
45
        }
46
        return $vhosts;
47
    }
48
49
    /**
50
     * returns list of queues for vhost
51
     * @param string|null $vhost
52
     * @return array
53
     */
54
    public function getQueues($vhost = null)
55
    {
56
        $endpoint = '/api/queues';
57
        if ($vhost) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $vhost of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
58
            $endpoint .= '/' . urlencode($vhost);
59
        }
60
        $queues = $this->call($endpoint);
61
        return $queues;
62
    }
63
64
    public function getMessages($vhost, $queue)
65
    {
66
        $count = 0;
67
        foreach ($this->getQueues($vhost) as $vhostQueue) {
68
            if ($vhostQueue['name'] == $queue) {
69
                $count = $vhostQueue['messages'];
70
                break;
71
            }
72
        }
73
        if ($count == 0) {
74
            return [];
75
        }
76
77
        $endpoint = '/api/queues/' . urlencode($vhost) . '/' . urlencode($queue) . '/get';
78
        $params = [
79
            'count' => $count,
80
            'requeue' => true,
81
            'encoding' => 'auto',
82
        ];
83
        $result = $this->call($endpoint, 'POST', json_encode($params));
84
        return $result;
85
    }
86
87 2
    private function call($endpoint, $method = 'GET', $params = null)
88
    {
89 2
        $ch = curl_init($this->baseUrl . $endpoint);
90 2
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
91 2
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
92 2
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
93 2
        if ($params) {
94
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
95
        }
96 2
        curl_setopt($ch, CURLOPT_TIMEOUT, 3);
97 2
        $response = curl_exec($ch);
98 2
        curl_close($ch);
99 2
        if (!$response) {
100
            return [];
101
        }
102 2
        $result = json_decode((string)$response, true);
103 2
        if (isset($result['error'])) {
104
            throw new ConnectException($result['reason']);
105
        }
106 2
        return $result;
107
    }
108
}
109