TurboSmsApi   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 50
c 2
b 0
f 1
dl 0
loc 100
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getResponse() 0 40 5
A sendMessage() 0 26 5
A __construct() 0 9 1
1
<?php
2
3
namespace NotificationChannels\TurboSms;
4
5
use GuzzleHttp\Client as HttpClient;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Client was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
class TurboSmsApi
8
{
9
    /** @var HttpClient */
10
    protected $client;
11
    protected $apiToken;
12
    protected $smsSender;
13
    protected $isTest;
14
15
    protected $baseUri = 'https://api.turbosms.ua/';
16
17
    public function __construct($apiToken, $sender, array $configs = [])
18
    {
19
        $this->apiToken = $apiToken;
20
        $this->smsSender = $sender;
21
        $this->isTest = $configs['is_test'] ?? false;
22
        
23
        $this->client = new HttpClient([
24
            'timeout' => intval($configs['timeout'] ?? 15),
25
            'connect_timeout' => intval($configs['connect_timeout'] ?? 10),
26
        ]);
27
    }
28
29
    /**
30
     * @param $recipient
31
     * @param TurboSmsMessage $message
32
     * @return array
33
     */
34
    public function sendMessage($recipient, TurboSmsMessage $message)
35
    {
36
        $url = $this->baseUri . 'message/send.json';
37
        $body = [
38
            'recipients' => [$recipient],
39
            'sms' => [
40
                'text' => $message->content,
41
            ],
42
        ];
43
44
        if ($this->smsSender) {
45
            $body['sms']['sender'] = $this->smsSender;
46
        }
47
        if ($message->from) {
48
            $body['sms']['sender'] = $message->from;
49
        }
50
51
        if (!is_null($message->test)) {
0 ignored issues
show
introduced by
The condition is_null($message->test) is always false.
Loading history...
52
            $this->isTest = $message->test;
53
        }
54
55
        if (!is_null($message->time)) {
0 ignored issues
show
introduced by
The condition is_null($message->time) is always false.
Loading history...
56
            $body['start_time'] = $message->time;
57
        }
58
59
        return $this->getResponse($url, $body);
60
    }
61
62
    /**
63
     * @param string $url
64
     * @param array $body
65
     * @return array
66
     */
67
    public function getResponse($url, $body)
68
    {
69
        if ($this->isTest) {
70
            return [
71
                'success' => false,
72
                'result' => [
73
                    'url' => $url,
74
                    'body' => $body,
75
                ],
76
                'info' => 'turbosms.test_mode',
77
            ];
78
        }
79
80
        $response = $this->client->request('POST', $url, [
81
            'headers'        => [
82
                'Accept' => 'application/json',
83
                'Content-Type' => 'application/json',
84
                'Authorization' => 'Bearer ' . $this->apiToken,
85
            ],
86
            'json' => $body
87
        ]);
88
89
        $answer = \json_decode((string) $response->getBody(), true);
90
91
        if (isset($answer['error'])) {
92
            throw new \Exception($answer['error'] ?? 'Erorr TurboSMS.');
93
        }
94
95
        if (!isset($answer['response_result']) || !$answer['response_result']) {
96
            $error = 'TurboSMS  response status: ' . ($answer['response_status'] ?? 'null');
97
98
            throw new \Exception($answer['response_status'] ?? $error);
99
        }
100
101
        $info = 'TurboSMS  response status: ' . ($answer['response_status'] ?? '');
102
103
        return [
104
            'success' => true,
105
            'result' => $answer['response_result'],
106
            'info' => $info,
107
        ];
108
    }
109
}
110