|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace juniorb2ss\DeathByCaptcha\Abstracts; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
6
|
|
|
use juniorb2ss\DeathByCaptcha\Exceptions\AccessDeniedException; |
|
7
|
|
|
use juniorb2ss\DeathByCaptcha\Exceptions\CaptchaNotFoundException; |
|
8
|
|
|
use juniorb2ss\DeathByCaptcha\Exceptions\InternalServiceException; |
|
9
|
|
|
use juniorb2ss\DeathByCaptcha\Exceptions\InvalidCaptchaException; |
|
10
|
|
|
use juniorb2ss\DeathByCaptcha\Exceptions\InvalidServiceResponseException; |
|
11
|
|
|
use juniorb2ss\DeathByCaptcha\Exceptions\ServiceOverloadException; |
|
12
|
|
|
|
|
13
|
|
|
abstract class HttpHandlerResponseAbstract |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Define o content que deverá ser a resposta |
|
18
|
|
|
* do serviço |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $contentTypeOk = 'application/json; charset=utf8'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Executa verificação da integridade da resposta |
|
25
|
|
|
* desde do cabeçalho do response até o conteúdo |
|
26
|
|
|
* @return void|ClientException |
|
27
|
|
|
*/ |
|
28
|
78 |
|
protected function handlerHttpResponseIsOk() |
|
29
|
|
|
{ |
|
30
|
78 |
|
$response = $this->response; |
|
|
|
|
|
|
31
|
78 |
|
$contentType = $response->getHeaderLine('Content-Type'); |
|
32
|
|
|
|
|
33
|
78 |
|
$statusCode = $response->getStatusCode(); |
|
34
|
|
|
|
|
35
|
|
|
// Efetua o tratamento do status code do response |
|
36
|
|
|
switch ($statusCode) { |
|
37
|
78 |
|
case 400: |
|
38
|
3 |
|
throw new InvalidCaptchaException; |
|
39
|
|
|
break; |
|
|
|
|
|
|
40
|
75 |
|
case 403: |
|
41
|
3 |
|
throw new AccessDeniedException; |
|
42
|
|
|
break; |
|
|
|
|
|
|
43
|
72 |
|
case 404: |
|
44
|
3 |
|
throw new CaptchaNotFoundException; |
|
45
|
|
|
break; |
|
|
|
|
|
|
46
|
69 |
|
case 413: |
|
47
|
3 |
|
throw new InvalidCaptchaException; |
|
48
|
|
|
break; |
|
|
|
|
|
|
49
|
66 |
|
case 500: |
|
50
|
3 |
|
throw new InternalServiceException; |
|
51
|
63 |
|
case 501: |
|
52
|
3 |
|
throw new InvalidCaptchaException; |
|
53
|
|
|
break; |
|
|
|
|
|
|
54
|
60 |
|
case 503: |
|
55
|
3 |
|
throw new ServiceOverloadException; |
|
56
|
|
|
break; |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// Uma coisa básica é verificar o tipo do conteúdo |
|
60
|
|
|
// da resposta HTTP. |
|
61
|
|
|
// Parece a principio algo meio "desnecessário" |
|
62
|
|
|
// já que se a resposta for ok e for passado o |
|
63
|
|
|
// accept de json, técnicamente deverá retornar |
|
64
|
|
|
// o content type como json |
|
65
|
|
|
// mas... nunca se sabe, né? |
|
66
|
57 |
|
if (($contentType !== $this->contentTypeOk) or (empty((string)$response->getBody()))) { |
|
|
|
|
|
|
67
|
6 |
|
throw new InvalidServiceResponseException; |
|
68
|
|
|
} |
|
69
|
51 |
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: