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

ChallengeParser::parseArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 2
dl 0
loc 15
ccs 10
cts 10
cp 1
crap 3
rs 9.9666
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\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