Completed
Push — master ( bb237b...7defa1 )
by jelmer
04:51
created

Payload::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Pageon\SlackWebhookMonolog\Slack;
4
5
use JsonSerializable;
6
use Pageon\SlackWebhookMonolog\Monolog\Interfaces\ErrorInterface;
7
8
/**
9
 * Turns the record into a payload for slack.
10
 *
11
 * Class Payload
12
 *
13
 * @author Jelmer Prins <jelmer@$pageon.be>
14
 */
15
class Payload implements JsonSerializable
16
{
17
    /**
18
     * @var array The data from the error handler
19
     */
20
    private $record;
21
22
    /**
23
     * The data that will be send to the api.
24
     *
25
     * @var array
26
     */
27
    private $payload;
28
29
    /**
30
     * Extra error data.
31
     *
32
     * @var ErrorInterface
33
     */
34
    private $errorData;
35
36
    /**
37
     * Payload constructor.
38
     *
39
     * @param $record
40
     */
41 1
    public function __construct(array $record)
42
    {
43 1
        $this->record = $record;
44 1
        $this->generatePayload();
45 1
    }
46
47 1
    private function generatePayload()
48
    {
49 1
        $this->setErrorData();
50
51 1
        $this->setMessage();
52 1
    }
53
54
    /**
55
     * If available set the error data.
56
     */
57 1
    private function setErrorData()
58
    {
59 1
        if (!isset($this->record['context']['error'])) {
60 1
            return;
61
        }
62
63
        $this->errorData = $this->record['context']['error'];
64
    }
65
66
    /**
67
     * Check if we have the extra error data.
68
     *
69
     * @return bool
70
     */
71 1
    private function hasErrorData()
72
    {
73 1
        return $this->errorData !== null;
74
    }
75
76 1
    private function setMessage()
77
    {
78 1
        $this->payload['text'] = $this->hasErrorData() ? $this->errorData->getMessage() : $this->record['message'];
79 1
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84 1
    public function jsonSerialize()
85
    {
86 1
        return $this->payload;
87
    }
88
}
89