Completed
Push — master ( 544afa...7f76dc )
by jelmer
02:40
created

BasicInfoAttachment::__construct()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 27
Code Lines 16

Duplication

Lines 6
Ratio 22.22 %

Code Coverage

Tests 20
CRAP Score 5

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 6
loc 27
ccs 20
cts 20
cp 1
rs 8.439
cc 5
eloc 16
nc 16
nop 2
crap 5
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 10
    public function __construct(array $record, ErrorInterface $error = null)
36
    {
37 10
        $this->record = $record;
38 10
        $this->error = $error;
39
40 10
        $message = ($this->error !== null) ? $this->error->getMessage() : $this->record['message'];
41 10
        parent::__construct($message);
42
43 10
        $this->setColour($this->getColourForLoggerLevel());
44 10
        $this->setText(sprintf('*Error:* %s', $this->record['level_name']));
45 10
        $this->addField(new Field('What', $message));
46
47 10
        $this->addField(new Field('When', $this->record['datetime']->format('d/m/Y H:m:i'), true));
48
49 10 View Code Duplication
        if (!empty($this->record['context'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50 1
            $this->addField(new Field('Context', json_encode($this->record['context'])));
51 1
        }
52
53 10 View Code Duplication
        if (!empty($this->record['extra'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54 1
            $this->addField(new Field('Extra', json_encode($this->record['extra'])));
55 1
        }
56
57 10
        if ($this->error !== null) {
58 6
            $this->addField(new Field('Line', $this->error->getLine(), true));
59 6
            $this->addField(new Field('File', $this->error->getFile()));
60 6
        }
61 10
    }
62
63
    /**
64
     * Returned a Slack message attachment color associated with
65
     * provided level.
66
     *
67
     * @return Colour
68
     */
69 10
    private function getColourForLoggerLevel()
70
    {
71 10
        switch (true) {
72 10
            case $this->record['level'] >= Logger::ERROR:
73 10
                return new Colour('danger');
74 1
            case $this->record['level'] >= Logger::WARNING:
75 1
                return new Colour('warning');
76 1
            case $this->record['level'] >= Logger::INFO:
77 1
                return new Colour('good');
78 1
            default:
79 1
                return new Colour('#e3e4e6');
80 1
        }
81
    }
82
}
83