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