|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace PROCERGS\LoginCidadao\CpfVerificationBundle\Service; |
|
12
|
|
|
|
|
13
|
|
|
use GuzzleHttp\Client; |
|
14
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
15
|
|
|
use PROCERGS\LoginCidadao\CpfVerificationBundle\Exception\CpfNotSubscribedToNfgException; |
|
16
|
|
|
use PROCERGS\LoginCidadao\CpfVerificationBundle\Exception\CpfVerificationException; |
|
17
|
|
|
use PROCERGS\LoginCidadao\CpfVerificationBundle\Exception\WrongAnswerException; |
|
18
|
|
|
use PROCERGS\LoginCidadao\CpfVerificationBundle\Model\ChallengeInterface; |
|
19
|
|
|
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException; |
|
20
|
|
|
|
|
21
|
|
|
class CpfVerificationHttpService |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var Client */ |
|
24
|
|
|
private $client; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* CpfVerificationHttpService constructor. |
|
28
|
|
|
* @param Client $client |
|
29
|
|
|
*/ |
|
30
|
11 |
|
public function __construct(Client $client) |
|
31
|
|
|
{ |
|
32
|
11 |
|
$this->client = $client; |
|
33
|
11 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param ChallengeInterface $challenge |
|
37
|
|
|
* @param string $answer |
|
38
|
|
|
* @return bool |
|
39
|
|
|
* @throws CpfVerificationException |
|
40
|
|
|
*/ |
|
41
|
5 |
|
public function submitAnswer(ChallengeInterface $challenge, string $answer) |
|
42
|
|
|
{ |
|
43
|
|
|
try { |
|
44
|
5 |
|
$response = $this->client->post($this->getChallengePath($challenge), [ |
|
45
|
5 |
|
'form_params' => ['answer' => $answer], |
|
46
|
|
|
]); |
|
47
|
3 |
|
} catch (RequestException $e) { |
|
48
|
3 |
|
$response = $e->getResponse(); |
|
49
|
|
|
} |
|
50
|
5 |
|
$statusCode = $response->getStatusCode(); |
|
51
|
5 |
|
$body = (string)$response->getBody(); |
|
52
|
5 |
|
$response = json_decode($body, true); |
|
53
|
|
|
|
|
54
|
5 |
|
if ($statusCode === 200 || $statusCode === 204) { |
|
55
|
2 |
|
return true; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
3 |
|
if ($statusCode === 429) { |
|
59
|
1 |
|
throw $this->getTooManyRequestsException($response['message'] ?? null); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
2 |
|
if ($statusCode === 403) { |
|
63
|
2 |
|
if ($response['error'] === WrongAnswerException::ERROR_CODE) { |
|
64
|
1 |
|
throw new WrongAnswerException($challenge, $response['message'] ?? "Wrong answer: {$answer}"); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
throw $this->getInvalidResponseException($statusCode, $body); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string $uri |
|
73
|
|
|
* @return string |
|
74
|
|
|
* @throws CpfVerificationException |
|
75
|
|
|
*/ |
|
76
|
4 |
|
public function sendGetRequest(string $uri): string |
|
77
|
|
|
{ |
|
78
|
|
|
try { |
|
79
|
4 |
|
$response = $this->client->get($uri); |
|
80
|
3 |
|
} catch (RequestException $e) { |
|
81
|
3 |
|
$response = $e->getResponse(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
4 |
|
$statusCode = $response->getStatusCode(); |
|
85
|
4 |
|
$body = (string)$response->getBody(); |
|
86
|
4 |
|
if ($statusCode === 200) { |
|
87
|
1 |
|
return $body; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
3 |
|
$response = json_decode($body, true); |
|
91
|
3 |
|
if ($statusCode === 403) { |
|
92
|
2 |
|
if ($response['error'] === CpfNotSubscribedToNfgException::ERROR_CODE) { |
|
93
|
1 |
|
throw new CpfNotSubscribedToNfgException($response['cpf'], $response['message'] ?? ''); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
2 |
|
if ($statusCode === 429) { |
|
98
|
1 |
|
throw $this->getTooManyRequestsException($response['message'] ?? null); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
throw $this->getInvalidResponseException($statusCode, $body); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
2 |
|
private function getInvalidResponseException($statusCode, $body): \LogicException |
|
105
|
|
|
{ |
|
106
|
2 |
|
return new \LogicException("Invalid response code \"{$statusCode}\" with body \"{$body}\""); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
2 |
|
private function getTooManyRequestsException($message = null): TooManyRequestsHttpException |
|
110
|
|
|
{ |
|
111
|
2 |
|
return new TooManyRequestsHttpException(null, $message); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
1 |
|
public function getListChallengesPath(string $cpf): string |
|
115
|
|
|
{ |
|
116
|
1 |
|
return "cpf/{$cpf}/challenges"; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
6 |
|
public function getChallengePath(ChallengeInterface $challenge): string |
|
120
|
|
|
{ |
|
121
|
6 |
|
return "cpf/{$challenge->getCpf()}/challenges/{$challenge->getName()}"; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|