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

BasicInfoAttachment   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 51
ccs 18
cts 18
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A getColour() 0 13 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