Passed
Push — master ( 1107f1...a96017 )
by Peter
34s
created

DetailedMatch::hasPlayerWon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace PtrTn\Battlerite\Dto\Match;
4
5
use DateTime;
6
use PtrTn\Battlerite\Assert\Assert;
7
8
/**
9
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
10
 */
11
class DetailedMatch
12
{
13
    /**
14
     * @var string
15
     */
16
    public $type;
17
    
18
    /**
19
     * @var string
20
     */
21
    public $id;
22
    
23
    /**
24
     * @var DateTime
25
     */
26
    public $createdAt;
27
    
28
    /**
29
     * @var int
30
     */
31
    public $duration;
32
    
33
    /**
34
     * @var string
35
     */
36
    public $gameMode;
37
    
38
    /**
39
     * @var string
40
     */
41
    public $patchVersion;
42
    
43
    /**
44
     * @var string
45
     */
46
    public $shardId;
47
    
48
    /**
49
     * @var string
50
     */
51
    public $titleId;
52
    
53
    /**
54
     * @var Map
55
     */
56
    public $map;
57
    
58
    /**
59
     * @var Assets
60
     */
61
    public $assets;
62
63
    /**
64
     * @var Spectators
65
     */
66
    public $spectators;
67
68
    /**
69
     * @var Rosters
70
     */
71
    public $rosters;
72
73
    /**
74
     * @var Rounds
75
     */
76
    public $rounds;
77
78
    /**
79
     * @var Participants
80
     */
81
    public $participants;
82
    
83
    /**
84
     * @var Players
85
     */
86
    public $players;
87
88
    /**
89
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
90
     */
91 6
    private function __construct(
92
        string $type,
93
        string $id,
94
        DateTime $createdAt,
95
        int $duration,
96
        string $gameMode,
97
        string $patchVersion,
98
        string $shardId,
99
        string $titleId,
100
        Map $map,
101
        Assets $assets,
102
        Spectators $spectators,
103
        Rosters $rosters,
104
        Rounds $rounds,
105
        Participants $participants,
106
        Players $players
107
    ) {
108 6
        $this->type = $type;
109 6
        $this->id = $id;
110 6
        $this->createdAt = $createdAt;
111 6
        $this->duration = $duration;
112 6
        $this->gameMode = $gameMode;
113 6
        $this->patchVersion = $patchVersion;
114 6
        $this->shardId = $shardId;
115 6
        $this->titleId = $titleId;
116 6
        $this->map = $map;
117 6
        $this->assets = $assets;
118 6
        $this->spectators = $spectators;
119 6
        $this->rosters = $rosters;
120 6
        $this->rounds = $rounds;
121 6
        $this->participants = $participants;
122 6
        $this->players = $players;
123 6
    }
124
125 6
    public static function createFromArray(array $match): self
126
    {
127 6
        $matchData = $match['data'];
128 6
        Assert::string($matchData['type']);
129 6
        Assert::string($matchData['id']);
130 6
        Assert::date($matchData['attributes']['createdAt'], DateTime::ISO8601);
131 6
        Assert::integer($matchData['attributes']['duration']);
132 6
        Assert::string($matchData['attributes']['gameMode']);
133 6
        Assert::string($matchData['attributes']['patchVersion']);
134 6
        Assert::string($matchData['attributes']['shardId']);
135 6
        Assert::isArray($matchData['attributes']['stats']);
136 6
        Assert::string($matchData['attributes']['titleId']);
137 6
        Assert::isArray($matchData['relationships']['assets']['data']);
138 6
        Assert::isArray($matchData['relationships']['rosters']['data']);
139 6
        Assert::isArray($matchData['relationships']['rounds']['data']);
140 6
        Assert::isArray($matchData['relationships']['spectators']['data']);
141
142 6
        $matchIncludes = $match['included'];
143
144 6
        $participants = [];
145 6
        $rosters = [];
146 6
        $rounds = [];
147 6
        $players = [];
148 6
        foreach ($matchIncludes as $include) {
149 6
            Assert::string($include['type']);
150 6
            switch ($include['type']) {
151 6
                case 'participant':
152 6
                    $participants[] = Participant::createFromArray($include);
153 6
                    break;
154 6
                case 'roster':
155 6
                    $rosters[] = Roster::createFromArray($include);
156 6
                    break;
157 6
                case 'round':
158 6
                    $rounds[] = Round::createFromArray($include);
159 6
                    break;
160 6
                case 'player':
161 6
                    $players[] = Player::createFromArray($include);
162 6
                    break;
163
            }
164
        }
165
166 6
        return new self(
167 6
            $matchData['type'],
168 6
            $matchData['id'],
169 6
            DateTime::createFromFormat(DateTime::ISO8601, $matchData['attributes']['createdAt']),
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor...ributes']['createdAt']) targeting DateTime::createFromFormat() can also be of type false; however, PtrTn\Battlerite\Dto\Mat...ledMatch::__construct() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
170 6
            $matchData['attributes']['duration'],
171 6
            $matchData['attributes']['gameMode'],
172 6
            $matchData['attributes']['patchVersion'],
173 6
            $matchData['attributes']['shardId'],
174 6
            $matchData['attributes']['titleId'],
175 6
            Map::createFromArray($matchData['attributes']['stats']),
176 6
            Assets::createFromArray($matchData['relationships']['assets']['data']),
177 6
            Spectators::createFromArray($matchData['relationships']['spectators']['data']),
178 6
            new Rosters($rosters),
179 6
            new Rounds($rounds),
180 6
            new Participants($participants),
181 6
            new Players($players)
182
        );
183
    }
184
185 3
    public function getParticipantByPlayerId(string $playerId): ?Participant
186
    {
187 3
        foreach ($this->participants as $participant) {
188 3
            if ($participant->userID === $playerId) {
189 3
                return $participant;
190
            }
191
        }
192
        return null;
193
    }
194
195 1
    public function hasPlayerWon(string $playerId): bool
196
    {
197 1
        $participant = $this->getParticipantByPlayerId($playerId);
198 1
        return $this->rosters->hasParticipantWon($participant);
199
    }
200
}
201