-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmsServiceApi.php
More file actions
77 lines (65 loc) · 2.49 KB
/
Copy pathSmsServiceApi.php
File metadata and controls
77 lines (65 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
declare(strict_types=1);
namespace Simpay;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\RequestOptions;
use Simpay\Exception\ExceptionFactory;
use Simpay\Model\Request\ServiceId;
use Simpay\Model\Request\ServiceNumber;
use Simpay\Model\Request\SmsCode;
use Simpay\Model\Response\Pagination;
use Simpay\Model\Response\SmsService;
use Simpay\Model\Response\SmsServiceCheckCodeData;
use Simpay\Model\Response\SmsServiceCollection;
class SmsServiceApi implements SmsServiceInterface
{
private ClientInterface $client;
public function __construct(HttpClientFactoryInterface $factory)
{
$this->client = $factory->create();
}
public function smsServiceList(): SmsServiceCollection
{
try {
$response = $this->client->request('GET', 'sms');
$data = \json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR,);
$services = [];
foreach ($data['data'] as $service) {
$services[] = SmsService::createFromResponse($service);
}
return new SmsServiceCollection($services, Pagination::createFromResponse($data['pagination']),);
} catch (RequestException $exception) {
throw ExceptionFactory::create($exception);
}
}
public function smsServiceShow(ServiceId $serviceId): SmsService
{
try {
$response = $this->client->request('GET', \sprintf('sms/%s', $serviceId));
$data = \json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
return SmsService::createFromResponse($data['data']);
} catch (RequestException $exception) {
throw ExceptionFactory::create($exception);
}
}
public function smsServiceCheckCode(
ServiceId $serviceId,
SmsCode $code,
ServiceNumber $serviceNumber
): SmsServiceCheckCodeData {
try {
$response = $this->client->request(
'POST',
\sprintf('sms/%s', $serviceId),
[
RequestOptions::JSON => $code->toArray() + $serviceNumber->toArray(),
],
);
$data = \json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
return SmsServiceCheckCodeData::createFromResponse($data['data']);
} catch (RequestException $exception) {
throw ExceptionFactory::create($exception);
}
}
}