Completed
Push — master ( 96e717...fd49b4 )
by jelmer
02:28
created

Payload::hasErrorData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Pageon\SlackWebhookMonolog\Slack;
4
5
use JsonSerializable;
6
use Pageon\SlackWebhookMonolog\Monolog\Interfaces\ErrorInterface;
7
use Pageon\SlackWebhookMonolog\Slack\Interfaces\ConfigInterface as SlackConfigInterface;
8
9
/**
10
 * Turns the record into a payload for slack.
11
 *
12
 * Class Payload
13
 *
14
 * @author Jelmer Prins <jelmer@$pageon.be>
15
 */
16
class Payload implements JsonSerializable
17
{
18
    /**
19
     * @var array The data from the error handler
20
     */
21
    private $record;
22
23
    /**
24
     * The data that will be send to the api.
25
     *
26
     * @var array
27
     */
28
    private $payload;
29
30
    /**
31
     * Extra error data.
32
     *
33
     * @var ErrorInterface
34
     */
35
    private $errorData;
36
37
    /**
38
     * Contains some extra information like channel and user etc that can be used in the payload.
39
     *
40
     * @var SlackConfigInterface
41
     */
42
    private $slackConfig;
43
44
    /**
45
     * Payload constructor.
46
     *
47
     * @param array $record
48
     * @param SlackConfigInterface $slackConfig
49
     */
50 7
    public function __construct(array $record, SlackConfigInterface $slackConfig = null)
51
    {
52 7
        $this->record = $record;
53 7
        $this->slackConfig = $slackConfig;
54
55 7
        $this->generatePayload();
56 7
    }
57
58 7
    private function generatePayload()
59
    {
60 7
        $this->setErrorData();
61
62 7
        $this->setMessage();
63
64 7
        if ($this->slackConfig !== null) {
65 5
            $this->generatePayloadForSlackConfig();
66 5
        }
67 7
    }
68
69
    /**
70
     * Generate the payload for the slack config
71
     */
72 5
    private function generatePayloadForSlackConfig()
73
    {
74 5
        $this->setChannel();
75
76 5
        if ($this->slackConfig->hasCustomUser()) {
77 3
            $this->generatePayloadForCustomUser();
78 3
        }
79 5
    }
80
81 3
    private function generatePayloadForCustomUser()
82
    {
83 3
        $this->setIcon();
84 3
        $this->setUsername();
85 3
    }
86
87
    /**
88
     * Set a custom icon if available.
89
     */
90 3
    private function setIcon()
91
    {
92 3
        if (!$this->slackConfig->getCustomUser()->hasIcon()) {
93 1
            return;
94
        }
95
96 2
        $iconType = 'icon_' . $this->slackConfig->getCustomUser()->getIcon()->getType();
97 2
        $this->payload[$iconType] = $this->slackConfig->getCustomUser()->getIcon();
98 2
    }
99
100
    /**
101
     * Set a custom username if available.
102
     */
103 3
    private function setUsername()
104
    {
105 3
        if (!$this->slackConfig->getCustomUser()->hasUsername()) {
106 2
            return;
107
        }
108
109 1
        $this->payload['username'] = $this->slackConfig->getCustomUser()->getUsername();
110 1
    }
111
112
    /**
113
     * Set a custom channel if available.
114
     */
115 5
    private function setChannel()
116
    {
117 5
        if (!$this->slackConfig->getWebhook()->hasCustomChannel()) {
118 1
            return;
119
        }
120
121 4
        $this->payload['channel'] = $this->slackConfig->getWebhook()->getCustomChannel();
122 4
    }
123
124
    /**
125
     * If available set the error data.
126
     */
127 7
    private function setErrorData()
128
    {
129 7
        if (!isset($this->record['context']['error'])) {
130 2
            return;
131
        }
132
133 5
        $this->errorData = $this->record['context']['error'];
134 5
    }
135
136
    /**
137
     * Check if we have the extra error data.
138
     *
139
     * @return bool
140
     */
141 7
    private function hasErrorData()
142
    {
143 7
        return $this->errorData !== null;
144
    }
145
146 7
    private function setMessage()
147
    {
148 7
        $this->payload['text'] = sprintf(
149 7
            '*%s:* %s',
150 7
            $this->record['level_name'],
151 7
            $this->hasErrorData() ? $this->errorData->getMessage() : $this->record['message']
152 7
        );
153 7
    }
154
155
    /**
156
     * @inheritdoc
157
     */
158 7
    public function jsonSerialize()
159
    {
160 7
        return $this->payload;
161
    }
162
}
163