Completed
Pull Request — 3.x (#336)
by
unknown
02:15
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
final class RabbitMQQueueStatusHttpProvider implements RabbitMQQueueStatusProviderInterface
22
{
23
    /**
24
     * @var HttpClient
25
     */
26
    private $client;
27
28
    /**
29
     * @var MessageFactory
30
     */
31
    private $messageFactory;
32
33
    /**
34
     * Array with RabbitMQ connection settings.
35
     *
36
     * @var array
37
     */
38
    private $settings;
39
40
    public function __construct(array $settings, HttpClient $client, MessageFactory $messageFactory)
41
    {
42
        $this->settings = $settings;
43
        $this->messageFactory = $messageFactory;
44
45
        $this->client = new PluginClient(
46
            $client,
47
            [new AuthenticationPlugin(
48
                new BasicAuth($this->settings['user'], $this->settings['pass'])
49
            )]
50
        );
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getApiQueueStatus()
57
    {
58
        try {
59
            $request = $this->messageFactory->createRequest('GET', sprintf('%s/queues', $this->settings['console_url']));
60
            $response = $this->client->sendRequest($request);
61
        } catch (\Exception $exception) {
62
            throw new MonitoringException($exception->getMessage(), $exception->getCode(), $exception->getPrevious());
63
        } catch (\Http\Client\Exception $exception) {
64
            throw new MonitoringException($exception->getMessage(), $exception->getCode(), $exception->getPrevious());
65
        }
66
        try {
67
            if (200 === $response->getStatusCode()) {
68
                return json_decode($response->getBody(), true);
69
            }
70
        } catch (\RuntimeException $exception) {
71
            throw new MonitoringException($exception->getMessage(), $exception->getCode(), $exception->getPrevious());
72
        }
73
74
        throw new MonitoringException($response->getStatusCode(), $response->getReasonPhrase());
75
    }
76
}
77