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
|
|
|
$request = $this->client->post( |
67
|
|
|
$this->config['api_url'], |
68
|
|
|
['body' => $postBody] |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
if (!in_array($request->getStatusCode(), self::ALLOWED_RESPONSE_STATUSES)) { |
72
|
|
|
throw new SlackbotException( |
73
|
|
|
SlackbotException::E_BAD_RESPONSE, |
74
|
|
|
['status_code: ' . $request->getStatusCode()] |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|