Passed
Push — master ( bdc105...ec8122 )
by Peter
01:57
created

Roster::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 6
crap 1
1
<?php
2
3
namespace PtrTn\Battlerite\Dto\Match;
4
5
use Webmozart\Assert\Assert;
6
7
class Roster
8
{
9
    /**
10
     * @var string
11
     */
12
    public $type;
13
14
    /**
15
     * @var string
16
     */
17
    public $id;
18
19
    /**
20
     * @var null|string
21
     */
22
    private $shardId;
23
24
    /**
25
     * @var int|null
26
     */
27
    private $score;
28
29
    /**
30
     * @var bool|null
31
     */
32
    private $won;
33
34
    /**
35
     * @var null|Participants
36
     */
37
    private $participants;
38
39 1
    private function __construct(
40
        string $type,
41
        string $id,
42
        ?string $shardId,
43
        ?int $score,
44
        ?bool $won,
45
        ?Participants $participants
46
    ) {
47 1
        $this->type = $type;
48 1
        $this->id = $id;
49 1
        $this->shardId = $shardId;
50 1
        $this->score = $score;
51 1
        $this->won = $won;
52 1
        $this->participants = $participants;
53 1
    }
54
55 1
    public static function createFromArray(array $roster): self
56
    {
57 1
        Assert::string($roster['type']);
58 1
        Assert::string($roster['id']);
59 1
        if (isset($roster['attributes'])) {
60 1
            Assert::nullOrString($roster['attributes']['shardId']);
61 1
            Assert::nullOrInteger($roster['attributes']['stats']['score']);
62 1
            Assert::nullOrString($roster['attributes']['won']);
63
        }
64 1
        if (isset($roster['relationships']['participants'])) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
65
            // Todo, make a reference / detailed dto for paticipants?
66
        }
67
68
        // Todo, this should probably be a boolean in json.
69 1
        $won = $roster['attributes']['won'] ? filter_var($roster['attributes']['won'], FILTER_VALIDATE_BOOLEAN) : null;
70
71 1
        return new self(
72 1
            $roster['type'],
73 1
            $roster['id'],
74 1
            $roster['attributes']['shardId'] ?? null,
75 1
            $roster['attributes']['stats']['score'] ?? null,
76 1
            $won,
77 1
            null
78
        );
79
    }
80
}
81