-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmsNumberApi.php
More file actions
82 lines (66 loc) · 2.86 KB
/
Copy pathSmsNumberApi.php
File metadata and controls
82 lines (66 loc) · 2.86 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
78
79
80
81
82
<?php
declare(strict_types=1);
namespace Simpay;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use Simpay\Exception\ExceptionFactory;
use Simpay\Model\Request\ServiceId;
use Simpay\Model\Response\Pagination;
use Simpay\Model\Response\SmsNumber;
use Simpay\Model\Response\SmsNumberCollection;
class SmsNumberApi implements SmsNumbersInterface
{
private ClientInterface $client;
public function __construct(HttpClientFactoryInterface $factory)
{
$this->client = $factory->create();
}
public function smsServiceNumbersList(ServiceId $serviceId): SmsNumberCollection
{
try {
$response = $this->client->request('GET', \sprintf('sms/%s/numbers', $serviceId),);
$data = \json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
$smsNumbers = [];
foreach ($data['data'] as $smsNumber) {
$smsNumbers[] = SmsNumber::createFromResponse($smsNumber);
}
return new SmsNumberCollection($smsNumbers, Pagination::createFromResponse($data['pagination']));
} catch (RequestException $exception) {
throw ExceptionFactory::create($exception);
}
}
public function smsServiceNumber(ServiceId $serviceId, Model\Request\SmsNumber $number): Model\Response\SmsNumber
{
try {
$response = $this->client->request('GET', \sprintf('sms/%s/numbers/%s', $serviceId, $number),);
$data = \json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
return SmsNumber::createFromResponse($data['data']);
} catch (RequestException $exception) {
throw ExceptionFactory::create($exception);
}
}
public function smsNumbers(): SmsNumberCollection
{
try {
$response = $this->client->request('GET', 'sms/numbers');
$data = \json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
$smsNumbers = [];
foreach ($data['data'] as $smsNumber) {
$smsNumbers[] = SmsNumber::createFromResponse($smsNumber);
}
return new SmsNumberCollection($smsNumbers, Pagination::createFromResponse($data['pagination']));
} catch (RequestException $exception) {
throw ExceptionFactory::create($exception);
}
}
public function smsNumber(Model\Request\SmsNumber $number): Model\Response\SmsNumber
{
try {
$response = $this->client->request('GET', \sprintf('sms/numbers/%s', $number));
$data = \json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
return SmsNumber::createFromResponse($data['data']);
} catch (RequestException $exception) {
throw ExceptionFactory::create($exception);
}
}
}