1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jcid\Voys; |
4
|
|
|
|
5
|
|
|
use Assert\Assertion; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use GuzzleHttp\RequestOptions; |
8
|
|
|
|
9
|
|
|
final class Voys |
10
|
|
|
{ |
11
|
|
|
const API_URL = 'https://api.voipgrid.nl/api/clicktodial/'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var Client |
15
|
|
|
*/ |
16
|
|
|
private $client; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $login; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $password; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $login |
30
|
|
|
* @param string $password |
31
|
|
|
*/ |
32
|
|
|
public function __construct(string $login, string $password) |
33
|
|
|
{ |
34
|
|
|
$this->client = new Client(); |
35
|
|
|
$this->validateLoginAndPassword($login, $password); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $fromPhonenumer |
40
|
|
|
* @param string $toPhonenumer |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function callPhonenumer(string $toPhonenumer, string $fromPhonenumer): string |
45
|
|
|
{ |
46
|
|
|
$this->validatePhonenumbers($toPhonenumer, $fromPhonenumer); |
47
|
|
|
|
48
|
|
|
$response = $this->client->request( |
49
|
|
|
'POST', |
50
|
|
|
self::API_URL, |
51
|
|
|
[ |
52
|
|
|
RequestOptions::AUTH => [$this->login, $this->password], |
53
|
|
|
RequestOptions::JSON => [ |
54
|
|
|
VoysOptionsInterface::B_NUMBER => $toPhonenumer, |
55
|
|
|
VoysOptionsInterface::A_NUMBER => $fromPhonenumer, |
56
|
|
|
], |
57
|
|
|
] |
58
|
|
|
); |
59
|
|
|
$voysResponse = new VoysResponse($response); |
|
|
|
|
60
|
|
|
|
61
|
|
|
return $voysResponse->getCallid(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $callid |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function getCallStatus(string $callid): string |
70
|
|
|
{ |
71
|
|
|
$this->validateCallId($callid); |
72
|
|
|
|
73
|
|
|
$response = $this->client->request( |
74
|
|
|
'GET', |
75
|
|
|
sprintf('%s%s/', self::API_URL, $callid), |
76
|
|
|
[ |
77
|
|
|
RequestOptions::AUTH => [$this->login, $this->password], |
78
|
|
|
] |
79
|
|
|
); |
80
|
|
|
$voysResponse = new VoysResponse($response); |
|
|
|
|
81
|
|
|
|
82
|
|
|
return $voysResponse->getStatus(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $login |
87
|
|
|
* @param string $password |
88
|
|
|
*/ |
89
|
|
|
private function validateLoginAndPassword(string $login, string $password): void |
90
|
|
|
{ |
91
|
|
|
// Login validation |
92
|
|
|
Assertion::string($login, 'Login: Value "%s" expected to be string, type %s given.'); |
93
|
|
|
Assertion::email($login, 'Login: Value "%s" was expected to be a valid e-mail address.'); |
94
|
|
|
|
95
|
|
|
// Password validation |
96
|
|
|
Assertion::string($password, 'Password: Value "%s" expected to be string, type %s given.'); |
97
|
|
|
|
98
|
|
|
$this->login = $login; |
99
|
|
|
$this->password = $password; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $toPhonenumer |
104
|
|
|
* @param string $fromPhonenumer |
105
|
|
|
*/ |
106
|
|
|
private function validatePhonenumbers(string $toPhonenumer, string $fromPhonenumer): void |
107
|
|
|
{ |
108
|
|
|
Assertion::notNull($toPhonenumer); |
109
|
|
|
Assertion::notNull($fromPhonenumer); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $callid |
114
|
|
|
*/ |
115
|
|
|
private function validateCallId($callid): void |
116
|
|
|
{ |
117
|
|
|
Assertion::string($callid); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.