Completed
Push — issue#804 ( 62fcb8 )
by Guilherme
04:14
created

ChallengeParser   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 24
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parseArray() 0 15 3
A parseJson() 0 5 1
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\Parser;
12
13
use PROCERGS\LoginCidadao\CpfVerificationBundle\Model\ChallengeFactory;
14
use PROCERGS\LoginCidadao\CpfVerificationBundle\Model\ChallengeInterface;
15
16
class ChallengeParser
17
{
18 6
    public static function parseJson(string $json, string $cpf = null): ChallengeInterface
19
    {
20 6
        $decoded = json_decode($json, true);
21
22 6
        return self::parseArray($decoded, $cpf);
23
    }
24
25 7
    public static function parseArray(array $serialized, string $cpf = null): ChallengeInterface
26
    {
27 7
        if (!array_key_exists('challenge', $serialized)) {
28 1
            throw new \RuntimeException();
29
        }
30 6
        if (!array_key_exists('attempts_left', $serialized)) {
31 1
            throw new \RuntimeException();
32
        }
33
34 5
        $challengeName = $serialized['challenge'];
35 5
        $attemptsLeft = $serialized['attempts_left'];
36 5
        $cpf = $cpf ?? $serialized['cpf'] ?? null;
37 5
        $choices = $serialized['choices'] ?? [];
38
39 5
        return ChallengeFactory::create($challengeName, $attemptsLeft, $cpf, $choices);
0 ignored issues
show
Bug introduced by
It seems like $cpf can also be of type null; however, parameter $cpf of PROCERGS\LoginCidadao\Cp...llengeFactory::create() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
        return ChallengeFactory::create($challengeName, $attemptsLeft, /** @scrutinizer ignore-type */ $cpf, $choices);
Loading history...
40
    }
41
}
42