PlaySmsClient::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

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 1
nc 1
nop 4
1
<?php
2
3
namespace CoreProc\NotificationChannels\PlaySms;
4
5
use CoreProc\NotificationChannels\PlaySms\Exceptions\CouldNotSendNotification;
6
use GuzzleHttp\Client;
7
use GuzzleHttp\Exception\RequestException;
8
9
class PlaySmsClient
10
{
11
    private $client;
12
    private $apiKey;
13
    private $username;
14
15
    public function __construct($baseUrl, $username, $apiKey, $options = [])
16
    {
17
        $this->username = $username;
18
        $this->apiKey = $apiKey;
19
        $this->client = new Client(array_merge(
20
            ['base_uri' => $baseUrl],
21
            $options
22
        ));
23
    }
24
25
    /**
26
     * @param $mobileNumber
27
     * @param $message
28
     * @throws CouldNotSendNotification
29
     */
30
    public function send($mobileNumber, $message)
31
    {
32
        try {
33
            $this->client->get('/index.php', [
34
                'query' => [
35
                    'app' => 'ws',
36
                    'u' => $this->username,
37
                    'h' => $this->apiKey,
38
                    'op' => 'pv',
39
                    'to' => $mobileNumber,
40
                    'msg' => $message,
41
                ],
42
            ]);
43
        } catch (RequestException $e) {
44
            throw new CouldNotSendNotification("Failed to call PlaySMS API. Endpoint responded with {$e->getCode()}.");
45
        }
46
    }
47
}
48