HipChatAlerter::alert()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 32
ccs 26
cts 26
cp 1
rs 8.8571
cc 2
eloc 24
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Innmind\ProvisionerBundle\Alert;
4
5
use GorkaLaucirica\HipchatAPIv2Client\API\RoomAPI;
6
use GorkaLaucirica\HipchatAPIv2Client\Model\Message;
7
8
/**
9
 * Sends a room notification when an alert is raised
10
 */
11
class HipChatAlerter implements AlerterInterface
12
{
13
    protected $api;
14
    protected $room;
15
16
    /**
17
     * Set the hipchat room api
18
     *
19
     * @param RoomAPI $api
20
     */
21 6
    public function setRoomApi(RoomAPI $api)
22
    {
23 6
        $this->api = $api;
24 6
    }
25
26
    /**
27
     * Set the room name where to send notifications to
28
     *
29
     * @param string $name
30
     */
31 6
    public function setRoom($name)
32
    {
33 6
        $this->room = (string) $name;
34 6
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 6
    public function alert(Alert $alert)
40
    {
41 6
        $message = new Message();
42
43 6
        if ($alert->isOverUsed()) {
44 3
            $color = Message::COLOR_RED;
45 3
            $text = sprintf(
46 3
                'Server at full capacity! Command: %s | CPU: %s | Load: %s | Required: %s | Running: %s',
47 3
                (string) $alert->getCommandInput(),
48 3
                $alert->getCpuUsage(),
49 3
                $alert->getLoadAverage(),
50 3
                $alert->getLeftOver(),
51 3
                $alert->getRunningProcesses()
52 3
            );
53 3
        } else {
54 3
            $color = Message::COLOR_YELLOW;
55 3
            $text = sprintf(
56 3
                'Server under used. You may take it down! Command: %s | CPU: %s | Load: %s',
57 3
                (string) $alert->getCommandInput(),
58 3
                $alert->getCpuUsage(),
59 3
                $alert->getLoadAverage()
60 3
            );
61
        }
62
63
        $message
64 6
            ->setColor($color)
65 6
            ->setNotify(true)
66 6
            ->setMessageFormat(Message::FORMAT_TEXT)
67 6
            ->setMessage($text);
68
69 6
        $this->api->sendRoomNotification($this->room, $message);
70 6
    }
71
}
72