1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ninjacto\OrtcPhp; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use ninjacto\OrtcPhp\Configs\OrtcConfig; |
7
|
|
|
use ninjacto\OrtcPhp\Models\Requests\AuthRequest; |
8
|
|
|
use ninjacto\OrtcPhp\Models\Requests\BalancerUrlRequest; |
9
|
|
|
use ninjacto\OrtcPhp\Models\Requests\SendMessageRequest; |
10
|
|
|
|
11
|
|
|
class Ortc |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var OrtcConfig |
15
|
|
|
*/ |
16
|
|
|
private $ortcConfig; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Client |
20
|
|
|
*/ |
21
|
|
|
private $guzzleClient; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $baseUrl; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Ortc constructor. |
30
|
|
|
* |
31
|
|
|
* @param OrtcConfig $ortcConfig |
32
|
|
|
*/ |
33
|
|
|
public function __construct(OrtcConfig $ortcConfig) |
34
|
|
|
{ |
35
|
|
|
$this->ortcConfig = $ortcConfig; |
36
|
|
|
$this->guzzleClient = new Client(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function getBaseUrl() |
43
|
|
|
{ |
44
|
|
|
return $this->baseUrl; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $baseUrl |
49
|
|
|
*/ |
50
|
|
|
public function setBaseUrl($baseUrl) |
51
|
|
|
{ |
52
|
|
|
$this->baseUrl = $baseUrl; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return Client |
57
|
|
|
*/ |
58
|
|
|
public function getGuzzleClient() |
59
|
|
|
{ |
60
|
|
|
return $this->guzzleClient; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Client $guzzleClient |
65
|
|
|
*/ |
66
|
|
|
public function setGuzzleClient($guzzleClient) |
67
|
|
|
{ |
68
|
|
|
$this->guzzleClient = $guzzleClient; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* prepare client before requesting. |
73
|
|
|
*/ |
74
|
|
|
protected function prepare() |
75
|
|
|
{ |
76
|
|
|
if (!$this->baseUrl) { |
77
|
|
|
$balancedUrlResponse = $this->getBalancerUrl(); |
78
|
|
|
$this->baseUrl = $balancedUrlResponse->getUrl(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* get balanced url. |
84
|
|
|
* |
85
|
|
|
* @throws Exceptions\NetworkErrorException |
86
|
|
|
* @throws Exceptions\UnauthorizedException |
87
|
|
|
* @throws Exceptions\InvalidBalancerUrlException |
88
|
|
|
* |
89
|
|
|
* @return Models\Responses\BalancerUrlResponse |
90
|
|
|
*/ |
91
|
|
|
public function getBalancerUrl() |
92
|
|
|
{ |
93
|
|
|
$this->baseUrl = null; |
94
|
|
|
|
95
|
|
|
$balancedUrlRequest = new BalancerUrlRequest(); |
96
|
|
|
$balancedUrlRequest->setOrtcConfig($this->ortcConfig); |
97
|
|
|
|
98
|
|
|
$ortcClient = new OrtcClient(); |
99
|
|
|
$ortcClient->setRequest($balancedUrlRequest); |
100
|
|
|
$ortcClient->setGuzzleClient($this->guzzleClient); |
101
|
|
|
|
102
|
|
|
return $ortcClient->execute(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* authenticate user. |
107
|
|
|
* |
108
|
|
|
* @param AuthRequest $authRequest |
109
|
|
|
* |
110
|
|
|
* @throws Exceptions\NetworkErrorException |
111
|
|
|
* @throws Exceptions\UnauthorizedException |
112
|
|
|
* |
113
|
|
|
* @return Models\Responses\AuthResponse |
114
|
|
|
*/ |
115
|
|
|
public function authenticate(AuthRequest $authRequest) |
116
|
|
|
{ |
117
|
|
|
$this->prepare(); |
118
|
|
|
|
119
|
|
|
$authRequest->setOrtcConfig($this->ortcConfig); |
120
|
|
|
|
121
|
|
|
$ortcClient = new OrtcClient(); |
122
|
|
|
$ortcClient->setRequest($authRequest); |
123
|
|
|
$ortcClient->setGuzzleClient($this->guzzleClient); |
124
|
|
|
$ortcClient->setBaseUrl($this->baseUrl); |
125
|
|
|
|
126
|
|
|
return $ortcClient->execute(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* send message (push). |
131
|
|
|
* |
132
|
|
|
* @param SendMessageRequest $sendMessageRequest |
133
|
|
|
* |
134
|
|
|
* @throws Exceptions\BatchRequestException |
135
|
|
|
* |
136
|
|
|
* @return Models\Responses\SendMessageResponse |
137
|
|
|
*/ |
138
|
|
|
public function sendMessage(SendMessageRequest $sendMessageRequest) |
139
|
|
|
{ |
140
|
|
|
$this->prepare(); |
141
|
|
|
|
142
|
|
|
$sendMessageRequest->setOrtcConfig($this->ortcConfig); |
143
|
|
|
|
144
|
|
|
$ortcClient = new OrtcClient(); |
145
|
|
|
$ortcClient->setRequest($sendMessageRequest); |
146
|
|
|
$ortcClient->setGuzzleClient($this->guzzleClient); |
147
|
|
|
$ortcClient->setBaseUrl($this->baseUrl); |
148
|
|
|
|
149
|
|
|
return $ortcClient->batchExecute(); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|