1 | <?php |
||
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 | const QUOTE_MAP = [ |
||
31 | self::QUOTE_DEFAULT => 'default', |
||
32 | self::QUOTE_DANGER => 'danger', |
||
33 | self::QUOTE_SUCCESS => 'success', |
||
34 | self::QUOTE_WARNING => 'warning', |
||
35 | self::QUOTE_INFO => 'info' |
||
36 | ]; |
||
37 | |||
38 | const ALLOWED_RESPONSE_STATUSES = [ |
||
39 | Response::HTTP_OK, |
||
40 | Response::HTTP_MOVED_PERMANENTLY, |
||
41 | Response::HTTP_FOUND |
||
42 | ]; |
||
43 | |||
44 | /** @var array */ |
||
45 | private $config; |
||
46 | |||
47 | /** @var GuzzleClient */ |
||
48 | private $guzzleClient; |
||
49 | |||
50 | /** @var SlackMessageValidator */ |
||
51 | private $validator; |
||
52 | |||
53 | /** |
||
54 | * @param array $config |
||
55 | * @param SlackMessageValidator $validator |
||
56 | */ |
||
57 | 3 | public function __construct(array $config, SlackMessageValidator $validator) |
|
63 | |||
64 | /** |
||
65 | * @return array |
||
66 | */ |
||
67 | 2 | public function getConfig(): array |
|
71 | |||
72 | /** |
||
73 | * @param array $config |
||
74 | */ |
||
75 | 3 | public function setConfig(array $config) |
|
79 | |||
80 | /** |
||
81 | * @param int $quoteType |
||
82 | * @return string |
||
83 | */ |
||
84 | 1 | public function quoteTypeColor(int $quoteType): string |
|
92 | |||
93 | /** |
||
94 | * @param SlackMessage $slackMessage |
||
95 | * @return bool |
||
96 | */ |
||
97 | public function sendMessage(SlackMessage $slackMessage): bool |
||
101 | |||
102 | /** |
||
103 | * @param SlackMessage $slackMessage |
||
104 | * @return string |
||
105 | */ |
||
106 | private function buildPostBody(SlackMessage $slackMessage): string |
||
107 | { |
||
108 | $this->validator->validateMessage($slackMessage); |
||
109 | $slackMessage = $this->validator->setDefaultsForEmptyFields($slackMessage, $this->getConfig()); |
||
110 | |||
111 | $return = [ |
||
112 | 'text' => $slackMessage->getText(), |
||
113 | 'channel' => $slackMessage->getRecipient(), |
||
114 | 'username' => $slackMessage->getSender(), |
||
115 | 'mrkdwn' => true, |
||
116 | 'as_user' => false |
||
117 | ]; |
||
118 | |||
119 | if (!empty($slackMessage->getIconUrl())) { |
||
120 | $return['icon_url'] = $slackMessage->getIconUrl(); |
||
121 | } |
||
122 | |||
123 | if (empty($slackMessage->getIconUrl()) && !empty($slackMessage->getIconEmoji())) { |
||
124 | $this->validator->validateIconEmoji($slackMessage); |
||
125 | $return['icon_emoji'] = $slackMessage->getIconEmoji(); |
||
126 | } |
||
127 | |||
128 | if ($slackMessage->isShowQuote()) { |
||
129 | $return['attachments'] = [ |
||
130 | 'fallback' => $slackMessage->getText(), |
||
131 | 'pretext' => $slackMessage->getText(), |
||
132 | 'fields' => [ |
||
133 | 'title' => (!$slackMessage->getQuoteTitle() ? '' : $slackMessage->getQuoteTitle()), |
||
134 | 'title_link' => (!$slackMessage->getQuoteTitleLink() ? '' : $slackMessage->getQuoteTitleLink()), |
||
135 | 'text' => (!$slackMessage->getQuoteText() ? '' : $slackMessage->getQuoteText()), |
||
136 | 'color' => $this->quoteTypeColor($slackMessage->getQuoteType()), |
||
137 | 'mrkdwn_in' => ['text', 'pretext'] |
||
138 | ] |
||
139 | ]; |
||
140 | } |
||
141 | |||
142 | return json_encode($return, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @param string $postBody |
||
147 | * @return bool |
||
148 | */ |
||
149 | private function sendRequest(string $postBody): bool |
||
158 | } |
||
159 |