Completed
Pull Request — master (#7)
by Anton
01:33
created

RocketChat::post()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace NotificationChannels\RocketChat;
6
7
use GuzzleHttp\Client as HttpClient;
8
use Psr\Http\Message\ResponseInterface;
9
10
final class RocketChat
11
{
12
    /** @var \GuzzleHttp\Client */
13
    private $http;
14
15
    /** @var string */
16
    private $url;
17
18
    /** @var string */
19
    private $token;
20
21
    /** @var string|null */
22
    private $defaultChannel;
23
24
    /**
25
     * @param  \GuzzleHttp\Client  $http
26
     * @param  string  $url
27
     * @param  string  $token
28
     * @param  string|null  $defaultChannel
29
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
30
     */
31 4
    public function __construct(HttpClient $http, string $url, string $token, ?string $defaultChannel = null)
32
    {
33 4
        $this->http = $http;
34 4
        $this->url = rtrim($url, '/');
35 4
        $this->token = $token;
36 4
        $this->defaultChannel = $defaultChannel;
37 4
    }
38
39
    /**
40
     * Returns RocketChat base url.
41
     *
42
     * @return string
43
     */
44
    public function url(): string
45
    {
46
        return $this->url;
47
    }
48
49
    /**
50
     * Returns RocketChat token.
51
     *
52
     * @return string
53
     */
54 1
    public function token(): string
55
    {
56 1
        return $this->token;
57
    }
58
59
    /**
60
     * Returns default channel id or name.
61
     *
62
     * @return string|null
63
     */
64 1
    public function defaultChannel(): ?string
65
    {
66 1
        return $this->defaultChannel;
67
    }
68
69
    /**
70
     * Send a message.
71
     *
72
     * @param  string  $to
73
     * @param  array  $message
74
     * @return \Psr\Http\Message\ResponseInterface
75
     */
76 2
    public function sendMessage(string $to, array $message): ResponseInterface
77
    {
78 2
        $url = sprintf('%s/hooks/%s', $this->url, $this->token);
79
80 2
        return $this->post($url, [
81 2
            'json' => array_merge($message, [
82 2
                'channel' => $to,
83
            ]),
84
        ]);
85
    }
86
87
    /**
88
     * Perform a simple post request.
89
     *
90
     * @param  string  $url
91
     * @param  array  $options
92
     * @return \Psr\Http\Message\ResponseInterface
93
     */
94 2
    private function post(string $url, array $options): ResponseInterface
95
    {
96 2
        return $this->http->post($url, $options);
97
    }
98
}
99