SpecialDiceFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A handlesType() 0 7 1
A buildDice() 0 5 1
A normaliseType() 0 4 1
A buildNDice() 0 8 2
1
<?php
2
3
namespace MeadSteve\DiceApi\Dice\Factories;
4
5
use MeadSteve\DiceApi\Dice;
6
7
class SpecialDiceFactory implements DiceFactory
8
{
9
    /**
10
     * @var array
11
     */
12
    private $diceTypeMappings;
13
14
    public function __construct()
15
    {
16
        $this->diceTypeMappings = [
17
            'steve' => function (): Dice\SteveDice {
18
                return new Dice\SteveDice();
19
            },
20
            'fate'  => function (): Dice\FateDice {
21
                return new Dice\FateDice();
22
            },
23
        ];
24
    }
25
26
27
    public function handlesType(string $type) : bool
28
    {
29
        return array_key_exists(
30
            $this->normaliseType($type),
31
            $this->diceTypeMappings
32
        );
33
    }
34
35
    /**
36
     * @param string $type
37
     * @param int $number
38
     * @return Dice[]
39
     */
40
    public function buildDice(string $type, int $number) : array
41
    {
42
        $diceConstructor = $this->diceTypeMappings[$this->normaliseType($type)];
43
        return $this->buildNDice($number, $diceConstructor);
44
    }
45
46
    private function normaliseType(string $type): string
47
    {
48
        return strtolower($type);
49
    }
50
51
    /**
52
     * @return Dice[]
53
     */
54
    private function buildNDice(int $diceCount, callable $constructorFunction)
55
    {
56
        $newDice = [];
57
        for ($i = 0; $i < $diceCount; $i++) {
58
            $newDice[] = $constructorFunction();
59
        }
60
        return $newDice;
61
    }
62
}
63