Completed
Push — master ( 96700d...96e717 )
by jelmer
02:14
created

Payload::generatePayloadForCustomUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 2
cts 2
cp 1
rs 9.4285
cc 1
eloc 3
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 3
    public function __construct(array $record, SlackConfigInterface $slackConfig = null)
51
    {
52 3
        $this->record = $record;
53 3
        $this->slackConfig = $slackConfig;
54
55 3
        $this->generatePayload();
56 3
    }
57
58 3
    private function generatePayload()
59
    {
60 3
        $this->setErrorData();
61
62 3
        $this->setMessage();
63 3
64
        if ($this->slackConfig !== null) {
65
            $this->generatePayloadForSlackConfig();
66
        }
67
    }
68 3
69
    /**
70 3
     * Generate the payload for the slack config
71 2
     */
72
    private function generatePayloadForSlackConfig()
73
    {
74 1
        $this->setChannel();
75 1
76
        if ($this->slackConfig->hasCustomUser()) {
77
            $this->generatePayloadForCustomUser();
78
        }
79
    }
80
81
    private function generatePayloadForCustomUser()
82 3
    {
83
        $this->setIcon();
84 3
        $this->setUsername();
85
    }
86
87 3
    /**
88
     * Set a custom icon if available.
89 3
     */
90 3
    private function setIcon()
91 3
    {
92 3
        if (!$this->slackConfig->getCustomUser()->hasIcon()) {
93 3
            return;
94 3
        }
95
96
        $iconType = 'icon_' . $this->slackConfig->getCustomUser()->getIcon()->getType();
97
        $this->payload[$iconType] = $this->slackConfig->getCustomUser()->getIcon();
98
    }
99 3
100
    /**
101 3
     * Set a custom username if available.
102
     */
103
    private function setUsername()
104
    {
105
        if (!$this->slackConfig->getCustomUser()->hasUsername()) {
106
            return;
107
        }
108
109
        $this->payload['username'] = $this->slackConfig->getCustomUser()->getUsername();
110
    }
111
112
    /**
113
     * Set a custom channel if available.
114
     */
115
    private function setChannel()
116
    {
117
        if (!$this->slackConfig->getWebhook()->hasCustomChannel()) {
118
            return;
119
        }
120
121
        $this->payload['channel'] = $this->slackConfig->getWebhook()->getCustomChannel();
122
    }
123
124
    /**
125
     * If available set the error data.
126
     */
127
    private function setErrorData()
128
    {
129
        if (!isset($this->record['context']['error'])) {
130
            return;
131
        }
132
133
        $this->errorData = $this->record['context']['error'];
134
    }
135
136
    /**
137
     * Check if we have the extra error data.
138
     *
139
     * @return bool
140
     */
141
    private function hasErrorData()
142
    {
143
        return $this->errorData !== null;
144
    }
145
146
    private function setMessage()
147
    {
148
        $this->payload['text'] = sprintf(
149
            '*%s:* %s',
150
            $this->record['level_name'],
151
            $this->hasErrorData() ? $this->errorData->getMessage() : $this->record['message']
152
        );
153
    }
154
155
    /**
156
     * @inheritdoc
157
     */
158
    public function jsonSerialize()
159
    {
160
        return $this->payload;
161
    }
162
}
163