Test Failed
Pull Request — master (#17)
by
unknown
03:14
created

SlackProvider::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
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 string */
42
    private $apiUrl;
43
44
    /** @var Client */
45
    private $client;
46
47
    /**
48
     * SlackProvider constructor.
49
     *
50
     * @param array $config
51
     */
52
    public function __construct(array $config)
53
    {
54
        $this->config = $config;
55
        if (empty($this->config['api_url'])) {
56
            throw new SlackbotException(SlackbotException::E_MISSING_API_URL);
57
        }
58
        $this->apiUrl = $this->config['api_url'];
59
        $this->client = new Client();
60
    }
61
62
    /**
63
     * @param string $postBody
64
     *
65
     * @return bool
66
     */
67
    public function send(string $postBody): bool
68
    {
69
        $request = $this->client->post(
70
            $this->apiUrl,
71
            ['body' => $postBody]
72
        );
73
74
        if (!in_array($request->getStatusCode(), self::ALLOWED_RESPONSE_STATUSES)) {
75
            throw new SlackbotException(
76
                SlackbotException::E_BAD_RESPONSE,
77
                ['status_code: ' . $request->getStatusCode()]
78
            );
79
        }
80
81
        return true;
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getApiUrl(): string
88
    {
89
        return $this->apiUrl;
90
    }
91
92
    /**
93
     * @param string $apiUrl
94
     *
95
     * @return SlackProvider
96
     */
97
    public function setApiUrl(string $apiUrl)
98
    {
99
        $this->apiUrl = $apiUrl;
100
101
        return $this;
102
    }
103
}
104