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