HipChatHandler   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 7
c 3
b 0
f 1
lcom 1
cbo 5
dl 0
loc 71
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A write() 0 8 1
A getDefaultFormatter() 0 4 1
A getLevelColor() 0 16 4
1
<?php
2
3
namespace Oqq\Minc\Log\Handler;
4
5
use Oqq\Minc\Log\Formatter\LineFormatter;
6
use Oqq\Minc\Log\Level;
7
use Oqq\Minc\Log\Record;
8
use Oqq\Minc\HipChat\Room as HipChatRoom;
9
10
/**
11
 * @author Eric Braun <[email protected]>
12
 */
13
class HipChatHandler extends AbstractHandler
14
{
15
    /** @var HipChatRoom */
16
    protected $room;
17
18
    /** @var string */
19
    protected $from;
20
21
    /**
22
     * @param HipChatRoom $room
23
     * @param string $from
24
     * @param int $level
25
     * @param bool $pass
26
     */
27
    public function __construct(
28
        HipChatRoom $room,
29
        $from,
30
        $level = HandlerInterface::DEFAULT_LEVEL,
31
        $pass = HandlerInterface::DEFAULT_PASS
32
    ) {
33
        $this->room = $room;
34
        $this->from = $from;
35
36
        parent::__construct($level, $pass);
37
    }
38
39
    /**
40
     * @param Record $record
41
     *
42
     * @return mixed
43
     * @throws \RuntimeException
44
     */
45
    protected function write(Record $record)
46
    {
47
        $this->room->send(
48
            $this->getFormattedMessage($record),
49
            $this->from,
50
            $this->getLevelColor($record->getLevel()->getValue())
51
        );
52
    }
53
54
    /**
55
     * @param int $levelValue
56
     *
57
     * @return string
58
     */
59
    protected function getLevelColor($levelValue)
60
    {
61
        if ($levelValue >= Level::ERROR) {
62
            return 'red';
63
        }
64
65
        if ($levelValue >= Level::WARNING) {
66
            return 'yellow';
67
        }
68
69
        if ($levelValue === Level::DEBUG) {
70
            return 'grey';
71
        }
72
73
        return 'green';
74
    }
75
76
    /**
77
     * @return LineFormatter
78
     */
79
    protected function getDefaultFormatter()
80
    {
81
        return new LineFormatter('%channel%.%level_name%: %message% %context% %extras%');
82
    }
83
}
84