Completed
Push — issue#804 ( 62fcb8...6613b4 )
by Guilherme
04:35
created

CpfVerificationService::selectChallenge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 PROCERGS\LoginCidadao\CpfVerificationBundle\Exception\CpfVerificationException;
14
use PROCERGS\LoginCidadao\CpfVerificationBundle\Model\ChallengeInterface;
15
use PROCERGS\LoginCidadao\CpfVerificationBundle\Parser\ChallengeParser;
16
17
class CpfVerificationService
18
{
19
    /** @var CpfVerificationHttpService */
20
    private $httpService;
21
22
    /**
23
     * CpfVerificationService constructor.
24
     * @param CpfVerificationHttpService $httpService
25
     */
26 3
    public function __construct(CpfVerificationHttpService $httpService)
27
    {
28 3
        $this->httpService = $httpService;
29 3
    }
30
31
    /**
32
     * @param string $cpf
33
     * @return ChallengeInterface[]
34
     * @throws CpfVerificationException
35
     */
36 1
    public function listAvailableChallenges(string $cpf): array
37
    {
38 1
        $body = $this->httpService->sendGetRequest($this->httpService->getListChallengesPath($cpf));
39
40 1
        return $this->parseChallengesList($body);
41
    }
42
43
    /**
44
     * @param ChallengeInterface $challenge
45
     * @return ChallengeInterface
46
     * @throws CpfVerificationException
47
     */
48 1
    public function selectChallenge(ChallengeInterface $challenge): ChallengeInterface
49
    {
50 1
        $body = $this->httpService->sendGetRequest($this->httpService->getChallengePath($challenge));
51
52 1
        return ChallengeParser::parseJson($body);
53
    }
54
55
    /**
56
     * @param ChallengeInterface $challenge
57
     * @param $answer
58
     * @return bool
59
     * @throws CpfVerificationException
60
     */
61 1
    public function answerChallenge(ChallengeInterface $challenge, $answer): bool
62
    {
63 1
        return $this->httpService->submitAnswer($challenge, $answer);
64
    }
65
66
    /**
67
     * @param string $json
68
     * @return ChallengeInterface[]
69
     */
70 1
    private function parseChallengesList(string $json): array
71
    {
72 1
        $response = json_decode($json, true);
73 1
        $challenges = [];
74 1
        if (array_key_exists('challenges', $response)) {
75 1
            $cpf = $response['cpf'];
76 1
            foreach ($response['challenges'] as $challenge) {
77 1
                $challenges[] = ChallengeParser::parseArray($challenge, $cpf);
78
            }
79
        }
80
81 1
        return $challenges;
82
    }
83
}
84