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

Rosters::getWinningRoster()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 0
crap 3.072
1
<?php
2
3
namespace PtrTn\Battlerite\Dto\Match;
4
5
use ArrayIterator;
6
use PtrTn\Battlerite\Dto\CollectionDto;
7
use RuntimeException;
8
use Traversable;
9
use Webmozart\Assert\Assert;
10
11
class Rosters extends CollectionDto
12
{
13
    /**
14
     * @var Roster[]
15
     */
16
    public $items;
17
18 6
    public function __construct(array $rosters)
19
    {
20 6
        parent::__construct(Roster::class, $rosters);
21 6
    }
22
23
    public static function createFromArray(array $rosters): self
24
    {
25
        Assert::notNull($rosters);
26
27
        $createdRosters = [];
28
        foreach ($rosters as $roster) {
29
            $createdRosters[] = Roster::createFromArray($roster);
30
        }
31
        return new self($createdRosters);
32
    }
33
34
    /**
35
     * @return Traversable|Roster[]
36
     */
37
    public function getIterator(): Traversable
38
    {
39
        return new ArrayIterator($this->items);
40
    }
41
42 3
    public function getWinningRoster(): ?Roster
43
    {
44 3
        foreach ($this->items as $roster) {
45 3
            if ($roster->won === true) {
46 3
                return $roster;
47
            }
48
        }
49
        return null;
50
    }
51
52
    /**
53
     * Returns true if participant was victorious
54
     * Returns false if opponent was victorious
55
     * Returns null if nobody is victorious
56
     */
57 2
    public function hasParticipantWon(Participant $participant): ?bool
58
    {
59 2
        $winningRoster = $this->getWinningRoster();
60 2
        if ($winningRoster === null) {
61
            return null;
62
        }
63
64 2
        foreach ($winningRoster->participants as $winningParticipant) {
65 2
            if ($winningParticipant->id === $participant->id) {
66 2
                return true;
67
            }
68
        }
69 2
        return false;
70
    }
71
}
72