SmspohApi::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace NotificationChannels\Smspoh;
4
5
use GuzzleHttp\Client as HttpClient;
6
use GuzzleHttp\Exception\ClientException;
7
use GuzzleHttp\Exception\GuzzleException;
8
use Illuminate\Support\Arr;
9
use NotificationChannels\Smspoh\Exceptions\CouldNotSendNotification;
10
11
class SmspohApi
12
{
13
    /**
14
     * @var HttpClient
15
     */
16
    protected $client;
17
18
    /**
19
     * @var string
20
     */
21
    protected $endpoint;
22
23
    /**
24
     * @var string
25
     */
26
    protected $sender;
27
28
    /**
29
     * @var string
30
     */
31
    protected $token;
32
33
    public function __construct($token = null, HttpClient $httpClient = null)
34 2
    {
35
        $this->token = $token;
36 2
        $this->client = $httpClient;
37 2
38
        $this->endpoint = config('services.smspoh.endpoint', 'https://smspoh.com/api/v2/send');
39 2
    }
40 2
41
    /**
42
     * Send text message.
43
     *
44
     * <code>
45
     * $message = [
46
     *   'sender'   => '',
47
     *   'to'       => '',
48
     *   'message'  => '',
49
     *   'test'     => '',
50
     * ];
51
     * </code>
52
     *
53
     * @link https://smspoh.com/rest-api-documentation/send?version=2
54
     *
55
     * @param array $message
56
     *
57
     * @return mixed|\Psr\Http\Message\ResponseInterface
58
     * @throws CouldNotSendNotification
59
     */
60
    public function send($message)
61
    {
62
        try {
63
            $response = $this->client->request('POST', $this->endpoint, [
64
                'headers' => [
65
                    'Authorization' => "Bearer {$this->token}",
66
                ],
67
                'json' => [
68
                    'sender' => Arr::get($message, 'sender'),
69
                    'to' => Arr::get($message, 'to'),
70
                    'message' => Arr::get($message, 'message'),
71
                    'test' => Arr::get($message, 'test', false),
72
                ],
73
            ]);
74
75
            return json_decode((string) $response->getBody(), true);
76
        } catch (ClientException $e) {
77
            throw CouldNotSendNotification::smspohRespondedWithAnError($e);
78
        } catch (GuzzleException $e) {
79
            throw CouldNotSendNotification::couldNotCommunicateWithSmspoh($e);
80
        }
81
    }
82
}
83