|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the WoW-Apps/Symfony-Slack-Bot bundle for Symfony 3 |
|
4
|
|
|
* https://github.com/wow-apps/symfony-slack-bot |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2016 WoW-Apps |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace WowApps\SlackBundle\Service; |
|
13
|
|
|
|
|
14
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
15
|
|
|
use WowApps\SlackBundle\DTO\SlackMessage; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class SlackBot |
|
19
|
|
|
* |
|
20
|
|
|
* @author Alexey Samara <[email protected]> |
|
21
|
|
|
* @package WowApps\SlackBundle |
|
22
|
|
|
*/ |
|
23
|
|
|
class SlackBot |
|
24
|
|
|
{ |
|
25
|
|
|
const QUOTE_DEFAULT = 0; |
|
26
|
|
|
const QUOTE_DANGER = 1; |
|
27
|
|
|
const QUOTE_SUCCESS = 2; |
|
28
|
|
|
const QUOTE_WARNING = 3; |
|
29
|
|
|
const QUOTE_INFO = 4; |
|
30
|
|
|
|
|
31
|
|
|
/** @var array */ |
|
32
|
|
|
private $config; |
|
33
|
|
|
|
|
34
|
|
|
/** @var GuzzleClient */ |
|
35
|
|
|
private $guzzleClient; |
|
36
|
|
|
|
|
37
|
|
|
/** @var SlackMessageValidator */ |
|
38
|
|
|
private $validator; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* SlackBot constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @param array $config |
|
44
|
|
|
* @param SlackMessageValidator $validator |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(array $config, SlackMessageValidator $validator) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->setConfig($config); |
|
49
|
|
|
$this->guzzleClient = new GuzzleClient(); |
|
50
|
|
|
$this->validator = $validator; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getConfig(): array |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->config; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param array $config |
|
63
|
|
|
*/ |
|
64
|
|
|
public function setConfig(array $config) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->config = $config; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param int $quoteType |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
public function quoteTypeColor(int $quoteType): string |
|
74
|
|
|
{ |
|
75
|
|
|
switch ($quoteType) { |
|
76
|
|
|
case self::QUOTE_DANGER: |
|
77
|
|
|
$colorHEX = $this->config['quote_color']['danger']; |
|
78
|
|
|
break; |
|
79
|
|
|
case self::QUOTE_SUCCESS: |
|
80
|
|
|
$colorHEX = $this->config['quote_color']['success']; |
|
81
|
|
|
break; |
|
82
|
|
|
case self::QUOTE_WARNING: |
|
83
|
|
|
$colorHEX = $this->config['quote_color']['warning']; |
|
84
|
|
|
break; |
|
85
|
|
|
case self::QUOTE_INFO: |
|
86
|
|
|
$colorHEX = $this->config['quote_color']['info']; |
|
87
|
|
|
break; |
|
88
|
|
|
case self::QUOTE_DEFAULT: |
|
89
|
|
|
default: |
|
90
|
|
|
$colorHEX = $this->config['quote_color']['default']; |
|
91
|
|
|
break; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $colorHEX; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param SlackMessage $slackMessage |
|
99
|
|
|
* @return bool |
|
100
|
|
|
*/ |
|
101
|
|
|
public function sendMessage(SlackMessage $slackMessage): bool |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->sendRequest($this->buildPostBody($slackMessage)); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param SlackMessage $slackMessage |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
private function buildPostBody(SlackMessage $slackMessage): string |
|
111
|
|
|
{ |
|
112
|
|
|
$this->validator->validateMessage($slackMessage); |
|
113
|
|
|
|
|
114
|
|
|
$slackMessage = $this->validator->setDefaultsForEmptyFields($slackMessage, $this->getConfig()); |
|
115
|
|
|
|
|
116
|
|
|
$return['text'] = $slackMessage->getText(); |
|
|
|
|
|
|
117
|
|
|
$return['mrkdwn'] = true; |
|
118
|
|
|
|
|
119
|
|
|
if ($slackMessage->isShowQuote()) { |
|
120
|
|
|
$return['attachments'] = [ |
|
121
|
|
|
'fallback' => $slackMessage->getText(), |
|
122
|
|
|
'pretext' => $slackMessage->getText(), |
|
123
|
|
|
'fields' => [ |
|
124
|
|
|
'title' => (!$slackMessage->getQuoteTitle() ? '' : $slackMessage->getQuoteTitle()), |
|
125
|
|
|
'title_link' => (!$slackMessage->getQuoteTitleLink() ? '' : $slackMessage->getQuoteTitleLink()), |
|
126
|
|
|
'text' => (!$slackMessage->getQuoteText() ? '' : $slackMessage->getQuoteText()), |
|
127
|
|
|
'color' => $this->quoteTypeColor($slackMessage->getQuoteType()), |
|
128
|
|
|
'mrkdwn_in' => ['text', 'pretext'] |
|
129
|
|
|
] |
|
130
|
|
|
]; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return json_encode($return, JSON_UNESCAPED_UNICODE); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param string $postBody |
|
138
|
|
|
* @return bool |
|
139
|
|
|
*/ |
|
140
|
|
|
private function sendRequest(string $postBody): bool |
|
141
|
|
|
{ |
|
142
|
|
|
$request = $this->guzzleClient->post($this->config['api_url'], ['body' => $postBody]); |
|
143
|
|
|
if (!in_array($request->getStatusCode(), [200, 301, 302])) { |
|
144
|
|
|
return false; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return true; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.