Roster::createFromArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 14
cts 14
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 14
nc 2
nop 1
crap 2
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 string
21
     */
22
    public $shardId;
23
24
    /**
25
     * @var int
26
     */
27
    public $score;
28
29
    /**
30
     * @var bool
31
     */
32
    public $won;
33
34
    /**
35
     * @var References
36
     */
37
    public $participants;
38
39 12
    private function __construct(
40
        string $type,
41
        string $id,
42
        string $shardId,
43
        int $score,
44
        bool $won,
45
        References $participants
46
    ) {
47 12
        $this->type = $type;
48 12
        $this->id = $id;
49 12
        $this->shardId = $shardId;
50 12
        $this->score = $score;
51 12
        $this->won = $won;
52 12
        $this->participants = $participants;
53 12
    }
54
55 12
    public static function createFromArray(array $roster): self
56
    {
57 12
        Assert::string($roster['type']);
58 12
        Assert::string($roster['id']);
59 12
        Assert::string($roster['attributes']['shardId']);
60 12
        Assert::integer($roster['attributes']['stats']['score']);
61 12
        Assert::string($roster['attributes']['won']);
62
63
        // Todo, this should probably be a boolean in json.
64 12
        $won = $roster['attributes']['won'] ?: filter_var($roster['attributes']['won'], FILTER_VALIDATE_BOOLEAN);
65
66 12
        return new self(
67 12
            $roster['type'],
68 12
            $roster['id'],
69 12
            $roster['attributes']['shardId'] ?? null,
70 12
            $roster['attributes']['stats']['score'] ?? null,
71 12
            $won,
72 12
            References::createFromArray($roster['relationships']['participants']['data'])
73
        );
74
    }
75
}
76