1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WowApps\SlackBotBundle\Service; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
6
|
|
|
use WowApps\SlackBotBundle\DTO\SlackMessage; |
7
|
|
|
|
8
|
|
|
class SlackBot |
9
|
|
|
{ |
10
|
|
|
const QUOTE_DEFAULT = 0; |
11
|
|
|
const QUOTE_DANGER = 1; |
12
|
|
|
const QUOTE_SUCCESS = 2; |
13
|
|
|
const QUOTE_WARNING = 3; |
14
|
|
|
const QUOTE_INFO = 4; |
15
|
|
|
|
16
|
|
|
/** @var array */ |
17
|
|
|
private $config; |
18
|
|
|
|
19
|
|
|
/** @var GuzzleClient */ |
20
|
|
|
private $guzzleClient; |
21
|
|
|
|
22
|
|
|
public function __construct(array $config) |
23
|
|
|
{ |
24
|
|
|
$this->setConfig($config); |
25
|
|
|
$this->guzzleClient = new GuzzleClient(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
|
|
public function getConfig(): array |
32
|
|
|
{ |
33
|
|
|
return $this->config; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array $config |
38
|
|
|
*/ |
39
|
|
|
public function setConfig(array $config) |
40
|
|
|
{ |
41
|
|
|
$this->config = $config; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param int $quoteType |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public function quoteTypeColor(int $quoteType): string |
49
|
|
|
{ |
50
|
|
|
switch ($quoteType) { |
51
|
|
|
case self::QUOTE_DANGER: |
52
|
|
|
$colorHEX = $this->config['quote_color']['danger']; |
53
|
|
|
break; |
54
|
|
|
case self::QUOTE_SUCCESS: |
55
|
|
|
$colorHEX = $this->config['quote_color']['success']; |
56
|
|
|
break; |
57
|
|
|
case self::QUOTE_WARNING: |
58
|
|
|
$colorHEX = $this->config['quote_color']['warning']; |
59
|
|
|
break; |
60
|
|
|
case self::QUOTE_INFO: |
61
|
|
|
$colorHEX = $this->config['quote_color']['info']; |
62
|
|
|
break; |
63
|
|
|
case self::QUOTE_DEFAULT: |
64
|
|
|
default: |
65
|
|
|
$colorHEX = $this->config['quote_color']['default']; |
66
|
|
|
break; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $colorHEX; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param SlackMessage $slackMessage |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
public function sendMessage(SlackMessage $slackMessage): bool |
77
|
|
|
{ |
78
|
|
|
return $this->sendRequest($this->buildPostBody($slackMessage)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param SlackMessage $slackMessage |
83
|
|
|
* @return string |
84
|
|
|
* @throws \InvalidArgumentException |
85
|
|
|
*/ |
86
|
|
|
private function buildPostBody(SlackMessage $slackMessage): string |
87
|
|
|
{ |
88
|
|
|
if (!$slackMessage->getText()) { |
89
|
|
|
throw new \InvalidArgumentException('Message can\'t be empty'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (!$slackMessage->getIcon()) { |
93
|
|
|
$return['icon_url'] = $this->config['default_icon']; |
|
|
|
|
94
|
|
|
} else { |
95
|
|
|
$return['icon_url'] = $slackMessage->getIcon(); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (!$slackMessage->getRecipient()) { |
99
|
|
|
$return['channel'] = $this->config['default_channel']; |
100
|
|
|
} else { |
101
|
|
|
$return['channel'] = $slackMessage->getRecipient(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (!$slackMessage->getSender()) { |
105
|
|
|
$return['username'] = 'SlackBot'; |
106
|
|
|
} else { |
107
|
|
|
$return['username'] = $slackMessage->getSender(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$return['text'] = $slackMessage->getText(); |
111
|
|
|
$return['mrkdwn'] = true; |
112
|
|
|
|
113
|
|
|
if ($slackMessage->isShowQuote()) { |
114
|
|
|
$return['attachments'] = [ |
115
|
|
|
'fallback' => $slackMessage->getText(), |
116
|
|
|
'pretext' => $slackMessage->getText(), |
117
|
|
|
'fields' => [ |
118
|
|
|
'title' => (!$slackMessage->getQuoteTitle() ? '' : $slackMessage->getQuoteTitle()), |
119
|
|
|
'title_link' => (!$slackMessage->getQuoteTitleLink() ? '' : $slackMessage->getQuoteTitleLink()), |
120
|
|
|
'text' => (!$slackMessage->getQuoteText() ? '' : $slackMessage->getQuoteText()), |
121
|
|
|
'color' => $this->quoteTypeColor($slackMessage->getQuoteType()), |
122
|
|
|
'mrkdwn_in' => ['text', 'pretext'] |
123
|
|
|
] |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return json_encode($return, JSON_UNESCAPED_UNICODE); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $postBody |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
private function sendRequest(string $postBody): bool |
135
|
|
|
{ |
136
|
|
|
$request = $this->guzzleClient->post($this->config['api_url'], ['body' => $postBody]); |
137
|
|
|
if (!in_array($request->getStatusCode(), [200, 301, 302])) { |
138
|
|
|
return false; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return true; |
142
|
|
|
} |
143
|
|
|
} |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.