Completed
Pull Request — master (#3)
by Peter
02:10
created

Rosters::createFromArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 6
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 4
    public function __construct(array $rosters)
19
    {
20 4
        parent::__construct(Roster::class, $rosters);
21 4
    }
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 2
    public function getWinningParticipants(): References
43
    {
44 2
        foreach ($this->items as $roster) {
45 2
            if ($roster->won === true) {
46 2
                return $roster->participants;
47
            }
48
        }
49
        throw new RuntimeException('Invalid data, match should always have a victor');
50
    }
51
}
52