BasicInfoAttachment::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 22
ccs 16
cts 16
cp 1
rs 9.2
cc 3
eloc 14
nc 4
nop 2
crap 3
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 11
    public function __construct(array $record, ErrorInterface $error = null)
36
    {
37 11
        $this->record = $record;
38 11
        $this->error = $error;
39
40 11
        $message = ($this->error !== null) ? $this->error->getMessage() : $this->record['message'];
41 11
        parent::__construct($message);
42
43 11
        $this->setColour($this->getColourForLoggerLevel());
44 11
        $this->setText(sprintf('*Error:* %s', $this->record['level_name']));
45 11
        $this->addField(new Field('What', $message));
46
47 11
        $this->addField(new Field('When', $this->record['datetime']->format('d/m/Y H:i:s'), true));
48
49 11
        $this->addRecordDataAsJsonEncodedField('context', 'Context');
50 11
        $this->addRecordDataAsJsonEncodedField('extra', 'Extra');
51
52 11
        if ($this->error !== null) {
53 6
            $this->addField(new Field('Line', $this->error->getLine(), true));
54 6
            $this->addField(new Field('File', $this->error->getFile()));
55 6
        }
56 11
    }
57
58
    /**
59
     * Check if a key is available in the record. If so, add it as a field.
60
     *
61
     * @param string $key
62
     * @param string $label
63
     */
64 11
    private function addRecordDataAsJsonEncodedField($key, $label)
65
    {
66 11
        if (!empty($this->record[$key])) {
67 3
            $this->addField(new Field($label, json_encode($this->record[$key])));
68 3
        }
69 11
    }
70
71
    /**
72
     * Returned a Slack message attachment color associated with
73
     * provided level.
74
     *
75
     * @return Colour
76
     */
77 11
    private function getColourForLoggerLevel()
78
    {
79 11
        switch (true) {
80 11
            case $this->record['level'] >= Logger::ERROR:
81 11
                return new Colour('danger');
82 1
            case $this->record['level'] >= Logger::WARNING:
83 1
                return new Colour('warning');
84 1
            case $this->record['level'] >= Logger::INFO:
85 1
                return new Colour('good');
86 1
            default:
87 1
                return new Colour('#e3e4e6');
88 1
        }
89
    }
90
}
91