Completed
Push — master ( 36743e...544afa )
by jelmer
02:21
created

BasicInfoAttachment::getColourForLoggerLevel()   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 1
Metric Value
c 1
b 0
f 1
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 Monolog\Logger;
6
use Pageon\SlackWebhookMonolog\Monolog\Interfaces\ErrorInterface;
7
8
/**
9
 * An attachment containing basic info about the error.
10
 *
11
 * @author Jelmer Prins <[email protected]>
12
 *
13
 * @since 0.4.0
14
 */
15
class BasicInfoAttachment extends Attachment
16
{
17
    /**
18
     * @var array The data from the error handler
19
     */
20
    private $record;
21
22
    /**
23
     * Extra error data.
24
     *
25
     * @var ErrorInterface
26
     */
27
    private $error;
28
29
    /**
30
     * BasicInfoAttachment constructor.
31
     *
32
     * @param array $record
33
     * @param ErrorInterface $error
34
     */
35 8
    public function __construct(array $record, ErrorInterface $error = null)
36
    {
37 8
        $this->record = $record;
38 8
        $this->error = $error;
39
40 8
        $message = ($this->error !== null) ? $this->error->getMessage() : $this->record['message'];
41 8
        parent::__construct($message);
42
43 8
        $this->setColour($this->getColourForLoggerLevel());
44 8
        $this->setText(sprintf('*Error:* %s', $this->record['level_name']));
45 8
        $this->addField(new Field('What', $message));
46
47 8
        $this->addField(new Field('When', $this->record['datetime']->format('d/m/Y H:m:i'), true));
48
49 8
        if ($this->error !== null) {
50 6
            $this->addField(new Field('Line', $this->error->getLine(), true));
51 6
            $this->addField(new Field('File', $this->error->getFile()));
52 6
        }
53 8
    }
54
55
    /**
56
     * Returned a Slack message attachment color associated with
57
     * provided level.
58
     *
59
     * @return Colour
60
     */
61 8
    private function getColourForLoggerLevel()
62
    {
63 8
        switch (true) {
64 8
            case $this->record['level'] >= Logger::ERROR:
65 8
                return new Colour('danger');
66 1
            case $this->record['level'] >= Logger::WARNING:
67 1
                return new Colour('warning');
68 1
            case $this->record['level'] >= Logger::INFO:
69 1
                return new Colour('good');
70 1
            default:
71 1
                return new Colour('#e3e4e6');
72 1
        }
73
    }
74
}
75