Completed
Pull Request — 3.x (#336)
by
unknown
02:17
created

RabbitMQQueueStatusHttpProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\NotificationBundle\Service;
13
14
use Http\Client\Common\Plugin\AuthenticationPlugin;
15
use Http\Client\Common\PluginClient;
16
use Http\Client\HttpClient;
17
use Http\Message\Authentication\BasicAuth;
18
use Http\Message\MessageFactory;
19
use Sonata\NotificationBundle\Exception\MonitoringException;
20
21
/**
22
 * @author Nikolay Mitrofanov <[email protected]>
23
 */
24
final class RabbitMQQueueStatusHttpProvider implements RabbitMQQueueStatusProviderInterface
25
{
26
    /**
27
     * @var HttpClient
28
     */
29
    private $client;
30
31
    /**
32
     * @var MessageFactory
33
     */
34
    private $messageFactory;
35
36
    /**
37
     * @var array
38
     */
39
    private $connectionSettings;
40
41
    public function __construct(array $settings, HttpClient $client, MessageFactory $messageFactory)
42
    {
43
        $this->connectionSettings = $settings;
44
        $this->messageFactory = $messageFactory;
45
46
        $this->client = new PluginClient(
47
            $client,
48
            [new AuthenticationPlugin(
49
                new BasicAuth($this->connectionSettings['user'], $this->connectionSettings['pass'])
50
            )]
51
        );
52
    }
53
54
    public function getApiQueueStatus()
55
    {
56
        try {
57
            $request = $this->messageFactory->createRequest('GET', sprintf('%s/queues', $this->connectionSettings['console_url']));
58
            $response = $this->client->sendRequest($request);
59
        } catch (\Exception $exception) {
60
            throw new MonitoringException($exception->getMessage(), $exception->getCode(), $exception->getPrevious());
61
        } catch (\Http\Client\Exception $exception) {
62
            throw new MonitoringException($exception->getMessage(), $exception->getCode(), $exception->getPrevious());
63
        }
64
        try {
65
            if (200 === $response->getStatusCode()) {
66
                return json_decode($response->getBody(), true);
67
            }
68
        } catch (\RuntimeException $exception) {
69
            throw new MonitoringException($exception->getMessage(), $exception->getCode(), $exception->getPrevious());
70
        }
71
72
        throw new MonitoringException($response->getStatusCode(), $response->getReasonPhrase());
73
    }
74
}
75