Passed
Push — master ( 454eba...132dab )
by jelmer
04:25 queued 02:00
created

BasicInfoAttachment::getColour()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 11
cts 11
cp 1
rs 9.2
cc 4
eloc 10
nc 4
nop 0
crap 4
1
<?php
2
3
namespace Pageon\SlackWebhookMonolog\Slack\Attachment;
4
5
use JsonSerializable;
6
use Monolog\Logger;
7
use Pageon\SlackWebhookMonolog\General\Url;
8
use Pageon\SlackWebhookMonolog\Monolog\Interfaces\ErrorInterface;
9
10
/**
11
 * An attachment containing basic info about the error.
12
 *
13
 * @author Jelmer Prins <[email protected]>
14
 *
15
 * @since 0.4.0
16
 */
17
class BasicInfoAttachment extends Attachment
18
{
19
    /**
20
     * @var array The data from the error handler
21
     */
22
    private $record;
23
24
    /**
25
     * Extra error data.
26
     *
27
     * @var ErrorInterface
28
     */
29
    private $error;
30
31
    /**
32
     * BasicInfoAttachment constructor.
33
     *
34
     * @param array $record
35
     * @param ErrorInterface $error
36
     */
37 8
    public function __construct(array $record, ErrorInterface $error = null)
38
    {
39 8
        $this->record = $record;
40 8
        $this->error = $error;
41
42 8
        $message = ($this->error !== null) ? $this->error->getMessage() : $this->record['message'];
43 8
        parent::__construct(sprintf('*%s:* %s', $this->record['level_name'], $message));
44
45 8
        $this->setColour($this->getColour());
46 8
    }
47
48
    /**
49
     * Returned a Slack message attachment color associated with
50
     * provided level.
51
     *
52
     * @return Colour
53
     */
54 8
    private function getColour()
55
    {
56 8
        switch (true) {
57 8
            case $this->record['level'] >= Logger::ERROR:
58 8
                return new Colour('danger');
59 1
            case $this->record['level'] >= Logger::WARNING:
60 1
                return new Colour('warning');
61 1
            case $this->record['level'] >= Logger::INFO:
62 1
                return new Colour('good');
63 1
            default:
64 1
                return new Colour('#e3e4e6');
65 1
        }
66
    }
67
}
68