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