WebhookAlerter::addUri()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Innmind\ProvisionerBundle\Alert;
4
5
use GuzzleHttp\Client;
6
use Psr\Log\LoggerInterface;
7
8
/**
9
 * Send a HTTP POST request when a provision alert is raised
10
 */
11
class WebhookAlerter implements AlerterInterface
12
{
13
    protected $client;
14
    protected $uris = [];
15
    protected $logger;
16
17
    /**
18
     * Set the guzzle http client
19
     *
20
     * @param Client $client
21
     */
22 3
    public function setHttpClient(Client $client)
23
    {
24 3
        $this->client = $client;
25 3
    }
26
27
    /**
28
     * Add a uri to call when an alert is raised
29
     *
30
     * @param string $uri
31
     */
32 3
    public function addUri($uri)
33
    {
34 3
        $this->uris[] = (string) $uri;
35 3
    }
36
37
    /**
38
     * Set the logger
39
     *
40
     * @param LoggerInterface $logger
41
     */
42
    public function setLogger(LoggerInterface $logger)
43
    {
44
        $this->logger = $logger;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 3
    public function alert(Alert $alert)
51
    {
52 3
        foreach ($this->uris as $uri) {
53 3
            $this->client->post(
54 3
                $uri,
55
                [
56
                    'body' => [
57 3
                        'type' => $alert->getType(),
58 3
                        'command' => (string) $alert->getCommandInput(),
59 3
                        'cpu' => $alert->getCpuUsage(),
60 3
                        'load_average' => $alert->getLoadAverage(),
61 3
                        'required_processes' => $alert->getLeftOver(),
62 3
                        'running_processes' => $alert->getRunningProcesses(),
63
                    ]
64 3
                ]
65 3
            );
66
67 3
            if ($this->logger) {
68
                $this->logger->info(
69
                    'Provision alert notified to a uri',
70
                    ['uri' => $uri]
71
                );
72
            }
73 3
        }
74 3
    }
75
}
76