1 | <?php |
||
2 | /** |
||
3 | * Created by PhpStorm. |
||
4 | * User: james |
||
5 | * Date: 12/07/2018 |
||
6 | * Time: 15:48 |
||
7 | */ |
||
8 | |||
9 | namespace CwsOps\LivePerson\Rest; |
||
10 | |||
11 | /** |
||
12 | * Class Url |
||
13 | * |
||
14 | * Handles the creation of a LivePerson API URL. |
||
15 | * |
||
16 | * This follows a fluid interface for ease of use. |
||
17 | * |
||
18 | * @package CwsOps\LivePerson |
||
19 | */ |
||
20 | class UrlBuilder |
||
21 | { |
||
22 | /** @var string $url the built url */ |
||
23 | private $url; |
||
24 | /** @var bool true or false if the URL has a query */ |
||
25 | private $hasQuery = false; |
||
26 | /** @var bool true or false if the builder is currently locked. */ |
||
27 | private $locked = false; |
||
28 | /** @var bool $isSecure true or false if the URL should be secure. */ |
||
29 | private $isSecure = true; |
||
30 | /** @var string $domain */ |
||
31 | private $domain; |
||
32 | /** @var string $service */ |
||
33 | private $service; |
||
34 | /** @var string $action */ |
||
35 | private $action; |
||
36 | /** @var string $account */ |
||
37 | private $account; |
||
38 | /** @var array $queryParams */ |
||
39 | private $queryParams = []; |
||
40 | /** @var int the version. */ |
||
41 | private $version = 1; |
||
42 | /** @var string|null any additional context to the action. */ |
||
43 | private $actionContext = null; |
||
44 | |||
45 | |||
46 | /** |
||
47 | * Starts creating URL |
||
48 | * |
||
49 | * @param bool $secure true or false if the Url should be secure. |
||
50 | * |
||
51 | * @return UrlBuilder |
||
52 | * |
||
53 | * @throws BuilderLockedException |
||
54 | */ |
||
55 | public function create(bool $secure = true): UrlBuilder |
||
56 | { |
||
57 | if (!$this->isValid()) { |
||
58 | throw new BuilderLockedException(); |
||
59 | } |
||
60 | $this->isSecure = $secure; |
||
61 | |||
62 | return $this; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Sets the domain. |
||
67 | * |
||
68 | * @param string $domain |
||
69 | * |
||
70 | * @return UrlBuilder |
||
71 | * |
||
72 | * @throws BuilderLockedException |
||
73 | */ |
||
74 | public function setDomain(string $domain): UrlBuilder |
||
75 | { |
||
76 | if (!$this->isValid()) { |
||
77 | throw new BuilderLockedException(); |
||
78 | } |
||
79 | $this->domain = $domain; |
||
80 | |||
81 | return $this; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Sets the service |
||
86 | * |
||
87 | * @param string $service |
||
88 | * |
||
89 | * @return UrlBuilder |
||
90 | * |
||
91 | * @throws BuilderLockedException |
||
92 | */ |
||
93 | public function setService(string $service): UrlBuilder |
||
94 | { |
||
95 | if (!$this->isValid()) { |
||
96 | throw new BuilderLockedException(); |
||
97 | } |
||
98 | |||
99 | $this->service = $service; |
||
100 | |||
101 | return $this; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param $context |
||
106 | * |
||
107 | * @return UrlBuilder |
||
108 | * |
||
109 | * @throws BuilderLockedException |
||
110 | */ |
||
111 | public function addActionContext($context): UrlBuilder |
||
112 | { |
||
113 | if (!$this->isValid()) { |
||
114 | throw new BuilderLockedException(); |
||
115 | } |
||
116 | |||
117 | $this->actionContext = $context; |
||
118 | |||
119 | return $this; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Sets the action |
||
124 | * |
||
125 | * @param string $action |
||
126 | * |
||
127 | * @return UrlBuilder |
||
128 | * |
||
129 | * @throws BuilderLockedException |
||
130 | */ |
||
131 | public function setAction(string $action): UrlBuilder |
||
132 | { |
||
133 | if (!$this->isValid()) { |
||
134 | throw new BuilderLockedException(); |
||
135 | } |
||
136 | |||
137 | $this->action = $action; |
||
138 | |||
139 | return $this; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Sets the account. |
||
144 | * |
||
145 | * @param string $account |
||
146 | * |
||
147 | * @return UrlBuilder |
||
148 | * |
||
149 | * @throws BuilderLockedException |
||
150 | */ |
||
151 | public function setAccount(string $account): UrlBuilder |
||
152 | { |
||
153 | if (!$this->isValid()) { |
||
154 | throw new BuilderLockedException(); |
||
155 | } |
||
156 | |||
157 | $this->account = $account; |
||
158 | |||
159 | return $this; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Sets the URL. to have a query parameters. |
||
164 | * |
||
165 | * @param bool $hasParam |
||
166 | * |
||
167 | * @return UrlBuilder |
||
168 | */ |
||
169 | public function hasQueryParam(bool $hasParam): UrlBuilder |
||
170 | { |
||
171 | $this->hasQuery = $hasParam; |
||
172 | |||
173 | return $this; |
||
174 | } |
||
175 | |||
176 | |||
177 | /** |
||
178 | * Adds any Query parameters |
||
179 | * |
||
180 | * @param string $key a key to place into the url |
||
181 | * @param string $value the value relating to the key. |
||
182 | * |
||
183 | * @return UrlBuilder |
||
184 | * |
||
185 | * @throws BuilderLockedException |
||
186 | */ |
||
187 | public function addQueryParam(string $key, string $value): UrlBuilder |
||
188 | { |
||
189 | if (!$this->isValid()) { |
||
190 | return $this; |
||
191 | } |
||
192 | |||
193 | if (!$this->hasQuery) { |
||
194 | return $this; |
||
195 | } |
||
196 | |||
197 | if (null !== $value) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
198 | $this->queryParams[$key] = $value; |
||
199 | } |
||
200 | |||
201 | return $this; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Sets the version. |
||
206 | * |
||
207 | * @param string $version |
||
208 | * |
||
209 | * @return UrlBuilder |
||
210 | * |
||
211 | * @throws BuilderLockedException |
||
212 | */ |
||
213 | public function setVersion(string $version): UrlBuilder |
||
214 | { |
||
215 | if (!$this->isValid()) { |
||
216 | throw new BuilderLockedException(); |
||
217 | } |
||
218 | $this->version = $version; |
||
0 ignored issues
–
show
The property
$version was declared of type integer , but $version is of type string . Maybe add a type cast?
This check looks for assignments to scalar types that may be of the wrong type. To ensure the code behaves as expected, it may be a good idea to add an explicit type cast. $answer = 42;
$correct = false;
$correct = (bool) $answer;
![]() |
|||
219 | return $this; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Builds the URL from the parameters passed. |
||
224 | * |
||
225 | * @return UrlBuilder |
||
226 | */ |
||
227 | public function build(): UrlBuilder |
||
228 | { |
||
229 | $this->createUrl(); |
||
230 | $this->locked = true; |
||
231 | return $this; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Gets the built URL. |
||
236 | * |
||
237 | * @return string |
||
238 | * |
||
239 | * @throws URLNotBuiltException |
||
240 | */ |
||
241 | public function getUrl(): string |
||
242 | { |
||
243 | if (!$this->locked) { |
||
244 | throw new URLNotBuiltException(); |
||
245 | } |
||
246 | |||
247 | $generatedUrl = $this->url; |
||
248 | $this->url = null; |
||
249 | $this->locked = false; |
||
250 | |||
251 | return $generatedUrl; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Returns true or false if the URL is built. |
||
256 | * |
||
257 | * @return bool |
||
258 | */ |
||
259 | public function isUrlBuilt() |
||
260 | { |
||
261 | return $this->locked; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Creates the URL from all the different parts. |
||
266 | * |
||
267 | * @return void |
||
268 | */ |
||
269 | private function createUrl() |
||
270 | { |
||
271 | // Build the URL. |
||
272 | $url = $this->isSecure ? $url = 'https://' : $url = 'http://'; |
||
0 ignored issues
–
show
|
|||
273 | |||
274 | // Add the attributes, to the URL. |
||
275 | $this->addToUrl($url, $this->domain); |
||
276 | $this->addToUrl($url, $this->service); |
||
277 | $this->addToUrl($url, 'api/account'); |
||
278 | $this->addToUrl($url, $this->account); |
||
279 | |||
280 | |||
281 | if ($this->actionContext !== null) { |
||
282 | $this->addToUrl($url, $this->action); |
||
283 | $this->addToUrl($url, $this->actionContext, false); |
||
284 | } else { |
||
285 | $this->addToUrl($url, $this->action, false); |
||
286 | } |
||
287 | |||
288 | |||
289 | // If the query has any parameters add them now. |
||
290 | $i = 0; |
||
291 | if ($this->hasQuery) { |
||
292 | $url .= '?'; |
||
293 | foreach ($this->queryParams as $key => $value) { |
||
294 | if ($i > 0) { |
||
295 | $url .= '&'; |
||
296 | } |
||
297 | $url .= $key . '=' . $value; |
||
298 | $i++; |
||
299 | } |
||
300 | |||
301 | $url .= '&v=' . $this->version; |
||
302 | } else { |
||
303 | $url .= '?v=' . $this->version; |
||
304 | } |
||
305 | |||
306 | $this->url = $url; |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Adds an attribute |
||
311 | * |
||
312 | * Note: the url is passed by reference. |
||
313 | * |
||
314 | * @param string $url The url to edit. |
||
315 | * @param string $attribute the attribute to add. |
||
316 | * @param bool $trailingSlash true or false to add the trailing slash. |
||
317 | * |
||
318 | * @return void |
||
319 | */ |
||
320 | private function addToUrl(&$url, $attribute, $trailingSlash = true) |
||
321 | { |
||
322 | if (null !== $attribute) { |
||
0 ignored issues
–
show
|
|||
323 | $url .= $attribute; |
||
324 | |||
325 | if ($trailingSlash) { |
||
326 | $url .= '/'; |
||
327 | } |
||
328 | } |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | */ |
||
333 | private function isValid() |
||
334 | { |
||
335 | if (!$this->locked) { |
||
336 | return true; |
||
337 | } |
||
338 | return false; |
||
339 | } |
||
340 | } |
||
341 |