PanaceaMobileApi::send()   B
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 30
rs 8.5806
cc 4
eloc 18
nc 5
nop 2
1
<?php
2
3
namespace NotificationChannels\PanaceaMobile;
4
5
use DomainException;
6
use GuzzleHttp\Client as HttpClient;
7
use NotificationChannels\PanaceaMobile\Exceptions\CouldNotSendNotification;
8
9
class PanaceaMobileApi
10
{
11
    const FORMAT_JSON = 3;
12
13
    /** @var string */
14
    protected $apiUrl = 'https://api.panaceamobile.com/json/3';
15
16
    /** @var HttpClient */
17
    protected $httpClient;
18
19
    /** @var string */
20
    protected $login;
21
22
    /** @var string */
23
    protected $secret;
24
25
    /** @var string */
26
    protected $sender;
27
28
    public function __construct($login, $secret, $sender)
29
    {
30
        $this->login = $login;
31
        $this->secret = $secret;
32
        $this->sender = $sender;
33
34
        $this->httpClient = new HttpClient([
35
            'timeout' => 5,
36
            'connect_timeout' => 5,
37
        ]);
38
    }
39
40
    /**
41
     * @param  string  $recipient
42
     * @param  array   $params
43
     *
44
     * @return array
45
     *
46
     * @throws CouldNotSendNotification
47
     */
48
    public function send($recipient, $params)
49
    {
50
        $params = array_merge([
51
			'action' 	=> 'message_send',
52
            'username'  => $this->login,
53
            'password'  => $this->secret,
54
            'from' 		=> $this->sender,
55
            'to' 		=> $recipient,
56
        ], $params);
57
		
58
		
59
        try {
60
            $request = $this->httpClient->get(
61
			$this->apiUrl, 
62
				[
63
					'query' => $params
64
				]
65
			);
66
			
67
            $response = json_decode($request->getBody(), true);
68
69
            if ( ! isset($response['status']) || $response['status'] != 1) {
70
                throw new DomainException($response['message'], $response['status']);
71
            }
72
73
            return $response;
74
        } catch (DomainException $exception) {
75
            throw CouldNotSendNotification::serviceRespondedWithAnError($exception);
76
        }
77
    }
78
}
79