SmsRuApi::send()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kafkiansky\SmsRu;
6
7
use GuzzleHttp\Client as HttpClient;
8
use GuzzleHttp\Exception\GuzzleException;
9
use Kafkiansky\SmsRu\Exceptions\HttpClientErrorException;
10
use Kafkiansky\SmsRu\Exceptions\SmsSendingFailedException;
11
use Kafkiansky\SmsRu\Message\SmsRuMessage;
12
13
final class SmsRuApi
14
{
15
    private const HOST = 'https://sms.ru';
16
17
    /**
18
     * @var SmsRuConfig
19
     */
20
    private $config;
21
22
    /**
23
     * @var HttpClient
24
     */
25
    private $client;
26
27
    public function __construct(SmsRuConfig $config, HttpClient $client)
28
    {
29
        $this->config = $config;
30
        $this->client = $client;
31
    }
32
33
    /**
34
     * @param SmsRuMessage $message
35
     *
36
     * @throws HttpClientErrorException
37
     * @throws SmsSendingFailedException
38
     *
39
     * @return SmsRuResponse
40
     */
41
    public function send(SmsRuMessage $message): SmsRuResponse
42
    {
43
        return $this->requestWithMessage($message, 'sms/send');
44
    }
45
46
    /**
47
     * @param SmsRuMessage $message
48
     *
49
     * @throws HttpClientErrorException
50
     * @throws SmsSendingFailedException
51
     *
52
     * @return SmsRuResponse
53
     */
54
    public function cost(SmsRuMessage $message): SmsRuResponse
55
    {
56
        return $this->requestWithMessage($message, 'sms/cost');
57
    }
58
59
    /**
60
     * @throws HttpClientErrorException
61
     * @throws SmsSendingFailedException
62
     *
63
     * @return SmsRuResponse
64
     */
65
    public function balance(): SmsRuResponse
66
    {
67
        return $this->emptyRequest('my/balance');
68
    }
69
70
    /**
71
     * @throws HttpClientErrorException
72
     * @throws SmsSendingFailedException
73
     *
74
     * @return SmsRuResponse
75
     */
76
    public function limit(): SmsRuResponse
77
    {
78
        return $this->emptyRequest('my/limit');
79
    }
80
81
    /**
82
     * @throws HttpClientErrorException
83
     * @throws SmsSendingFailedException
84
     *
85
     * @return SmsRuResponse
86
     */
87
    public function senders(): SmsRuResponse
88
    {
89
        return $this->emptyRequest('my/senders');
90
    }
91
92
    /**
93
     * @param SmsRuMessage $message
94
     * @param string       $url
95
     *
96
     * @throws HttpClientErrorException
97
     * @throws SmsSendingFailedException
98
     *
99
     * @return SmsRuResponse
100
     */
101
    private function requestWithMessage(SmsRuMessage $message, string $url): SmsRuResponse
102
    {
103
        $payload = \array_merge($this->config->toArray(), $message->toArray());
104
105
        return $this->request($payload, $url);
106
    }
107
108
    /**
109
     * @param string $url
110
     *
111
     * @throws HttpClientErrorException
112
     * @throws SmsSendingFailedException
113
     *
114
     * @return SmsRuResponse
115
     */
116
    private function emptyRequest(string $url): SmsRuResponse
117
    {
118
        return $this->request($this->config->toArray(), $url);
119
    }
120
121
    /**
122
     * @param array  $payload
123
     * @param string $endpoint
124
     *
125
     * @throws HttpClientErrorException
126
     * @throws SmsSendingFailedException
127
     *
128
     * @return SmsRuResponse
129
     */
130
    private function request(array $payload, string $endpoint): SmsRuResponse
131
    {
132
        try {
133
            $response = $this->client->request('POST', $this->endpoint($endpoint), [
134
                'form_params' => $payload,
135
            ]);
136
137
            $response = \json_decode((string) $response->getBody(), true);
138
139
            if (!\is_array($response)) {
140
                throw new SmsSendingFailedException($response, SmsRuResponse::errorTextFromCode($response));
141
            }
142
143
            if ('ERROR' === $response['status']) {
144
                throw new SmsSendingFailedException($response['status_code'], $response['status_text']);
145
            }
146
147
            return new SmsRuResponse($response);
148
        } catch (GuzzleException $e) {
149
            throw new HttpClientErrorException($e->getMessage());
150
        }
151
    }
152
153
    /**
154
     * @param string $path
155
     *
156
     * @return string
157
     */
158
    private function endpoint(string $path): string
159
    {
160
        return \sprintf('%s/%s', self::HOST, $path);
161
    }
162
}
163