1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the WoW-Apps/Symfony-Slack-Bot bundle for Symfony. |
5
|
|
|
* https://github.com/wow-apps/symfony-slack-bot |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* https://github.com/wow-apps/symfony-slack-bot/blob/master/LICENSE |
10
|
|
|
* |
11
|
|
|
* For technical documentation. |
12
|
|
|
* https://wow-apps.github.io/symfony-slack-bot/docs/ |
13
|
|
|
* |
14
|
|
|
* Author Alexey Samara <[email protected]> |
15
|
|
|
* |
16
|
|
|
* Copyright 2016 WoW-Apps. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace WowApps\SlackBundle\Service; |
20
|
|
|
|
21
|
|
|
use GuzzleHttp\Client; |
22
|
|
|
use Symfony\Component\HttpFoundation\Response; |
23
|
|
|
use WowApps\SlackBundle\Exception\SlackbotException; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class SlackProvider. |
27
|
|
|
* |
28
|
|
|
* @author Alexey Samara <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class SlackProvider |
31
|
|
|
{ |
32
|
|
|
const ALLOWED_RESPONSE_STATUSES = [ |
33
|
|
|
Response::HTTP_OK, |
34
|
|
|
Response::HTTP_MOVED_PERMANENTLY, |
35
|
|
|
Response::HTTP_FOUND, |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** @var array */ |
39
|
|
|
private $config; |
40
|
|
|
|
41
|
|
|
/** @var Client */ |
42
|
|
|
private $client; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* SlackProvider constructor. |
46
|
|
|
* |
47
|
|
|
* @param array $config |
48
|
|
|
*/ |
49
|
|
|
public function __construct(array $config) |
50
|
|
|
{ |
51
|
|
|
$this->config = $config; |
52
|
|
|
$this->client = new Client(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $postBody |
57
|
|
|
* |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
public function send(string $postBody): bool |
61
|
|
|
{ |
62
|
|
|
if (empty($this->config['api_url'])) { |
63
|
|
|
throw new SlackbotException(SlackbotException::E_MISSING_API_URL); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$url = $this->getApiUrl(json_decode($postBody, true)); |
|
|
|
|
67
|
|
|
|
68
|
|
|
$request = $this->client->post( |
69
|
|
|
$url, |
70
|
|
|
['body' => $postBody] |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
if (!in_array($request->getStatusCode(), self::ALLOWED_RESPONSE_STATUSES)) { |
74
|
|
|
throw new SlackbotException( |
75
|
|
|
SlackbotException::E_BAD_RESPONSE, |
76
|
|
|
['status_code: ' . $request->getStatusCode()] |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @deprecated |
85
|
|
|
* @param array $postBody |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
private function getApiUrl(array $postBody) |
89
|
|
|
{ |
90
|
|
|
$url = $this->config['api_url']; |
91
|
|
|
|
92
|
|
|
if (!empty($postBody['channel'])) { |
93
|
|
|
$url = $this->config['channels'][$postBody['channel']]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $url; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
This method has been deprecated.