Completed
Push — master ( 1d1fa3...f8a440 )
by Eric
07:48
created

HipChatHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 9.4285
cc 1
eloc 8
nc 1
nop 4
1
<?php
2
3
namespace Oqq\Minc\Log\Handler;
4
5
use Oqq\Minc\Log\Level;
6
use Oqq\Minc\Log\Record;
7
use Oqq\Minc\HipChat\Room as HipChatRoom;
8
9
/**
10
 * @author Eric Braun <[email protected]>
11
 */
12
class HipChatHandler extends AbstractHandler
13
{
14
    /** @var HipChatRoom */
15
    protected $room;
16
17
    /** @var string */
18
    protected $from;
19
20
    /**
21
     * @param HipChatRoom $room
22
     * @param string $from
23
     * @param int $level
24
     * @param bool $pass
25
     */
26
    public function __construct(
27
        HipChatRoom $room,
28
        $from,
29
        $level = HandlerInterface::DEFAULT_LEVEL,
30
        $pass = HandlerInterface::DEFAULT_PASS
31
    ) {
32
        $this->room = $room;
33
        $this->from = $from;
34
35
        parent::__construct($level, $pass);
36
    }
37
38
    /**
39
     * @param Record $record
40
     *
41
     * @return mixed
42
     * @throws \RuntimeException
43
     */
44
    protected function write(Record $record)
45
    {
46
        $this->room->send(
47
            $this->getFormattedMessage($record),
48
            $this->from,
49
            $this->getLevelColor($record->getLevel())
50
        );
51
    }
52
53
    /**
54
     * @param string $level
55
     *
56
     * @return string
57
     */
58
    protected function getLevelColor($level)
59
    {
60
        if ($level >= Level::ERROR) {
61
            return 'red';
62
        }
63
64
        if ($level >= Level::WARNING) {
65
            return 'yellow';
66
        }
67
68
        if ($level === Level::DEBUG) {
69
            return 'grey';
70
        }
71
72
        return 'green';
73
    }
74
}
75