Completed
Push — master ( 3baf41...27da68 )
by Anton
01:35
created

RocketChat::getToken()   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 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace NotificationChannels\RocketChat;
6
7
use GuzzleHttp\Client as HttpClient;
8
9
final class RocketChat
10
{
11
    /** @var \GuzzleHttp\Client */
12
    private $http;
13
14
    /** @var string */
15
    private $url;
16
17
    /** @var string */
18
    private $token;
19
20
    /** @var string|null */
21
    private $defaultChannel;
22
23
    /**
24
     * @param  \GuzzleHttp\Client  $http
25
     * @param  string  $url
26
     * @param  string  $token
27
     * @param  string|null  $defaultChannel
28
     * @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...
29
     */
30 4
    public function __construct(HttpClient $http, string $url, string $token, ?string $defaultChannel = null)
31
    {
32 4
        $this->http = $http;
33 4
        $this->url = rtrim($url, '/');
34 4
        $this->token = $token;
35 4
        $this->defaultChannel = $defaultChannel;
36 4
    }
37
38
    /**
39
     * Returns RocketChat token.
40
     *
41
     * @return string
42
     */
43 1
    public function getToken(): string
44
    {
45 1
        return $this->token;
46
    }
47
48
    /**
49
     * Returns default channel id or name.
50
     *
51
     * @return string|null
52
     */
53 1
    public function getDefaultChannel(): ?string
54
    {
55 1
        return $this->defaultChannel;
56
    }
57
58
    /**
59
     * Send a message.
60
     *
61
     * @param  string  $to
62
     * @param  array  $message
63
     * @return void
64
     */
65 3
    public function sendMessage(string $to, array $message): void
66
    {
67 3
        $url = sprintf('%s/hooks/%s', $this->url, $this->token);
68
69 3
        $this->post($url, [
70 3
            'json' => array_merge($message, [
71 3
                'channel' => $to,
72
            ]),
73
        ]);
74 1
    }
75
76
    /**
77
     * Perform a simple post request.
78
     *
79
     * @param  string  $url
80
     * @param  array  $options
81
     * @return void
82
     */
83 3
    private function post(string $url, array $options): void
84
    {
85 3
        $this->http->post($url, $options);
86 1
    }
87
}
88