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

DiceFactoryCollection   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 37
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handlesType() 0 9 3
A buildDice() 0 9 3
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