Completed
Push — master ( bf0f99...8e98a1 )
by Steve
01:57
created

DiceFactoryCollection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace MeadSteve\DiceApi\Dice\Factories;
4
5
use MeadSteve\DiceApi\Dice;
6
use MeadSteve\DiceApi\Dice\UncreatableDiceException;
7
8
class DiceFactoryCollection implements DiceFactory
9
{
10
    /**
11
     * @var DiceFactory[]
12
     */
13
    private $factories;
14
15
    public function __construct(array $factories)
16
    {
17
        $this->factories = $factories;
18
    }
19
20
    public function handlesType(string $type): bool
21
    {
22
        foreach ($this->factories as $factory) {
23
            if ($factory->handlesType($type)) {
24
                return true;
25
            }
26
        }
27
        return false;
28
    }
29
30
    /**
31
     * @param string $type
32
     * @param int $number
33
     * @return Dice[]
34
     */
35
    public function buildDice(string $type, int $number): array
36
    {
37
        foreach ($this->factories as $factory) {
38
            if ($factory->handlesType($type)) {
39
                return $factory->buildDice($type, $number);
40
            }
41
        }
42
        throw new UncreatableDiceException("No idea how to make a d{$type}");
43
    }
44
}
45