CalcService::findStartIndex()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 1
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PanicLabCore\Services;
6
7
use function array_reverse;
8
use function count;
9
use PanicLabCore\Services\Exceptions\EntryTileMissingException;
10
use PanicLabCore\Services\Handlers\TileHandlerCollectorInterface;
11
use PanicLabCore\Structs\Dice;
12
use PanicLabCore\Structs\EntryTile;
13
use PanicLabCore\Structs\GermTile;
14
use PanicLabCore\Structs\Step;
15
use PanicLabCore\Structs\Target;
16
use PanicLabCore\Structs\Tile;
17
use function sprintf;
18
19
class CalcService implements CalcServiceInterface
20
{
21
    /** @var Tile[] */
22
    public $tiles;
23
24
    /** @var Dice */
25
    public $diceResult;
26
27
    /** @var Target */
28
    public $target;
29
30
    /** @var int */
31
    public $maxSteps;
32
33
    /** @var TileHandlerCollectorInterface */
34
    public $tileHandlerCollector;
35
36
    /** @var Step */
37
    public $stepStruct;
38
39 23
    public function __construct(TileHandlerCollectorInterface $tileHandler, int $maxSteps)
40
    {
41 23
        $this->tileHandlerCollector = $tileHandler;
42 23
        $this->maxSteps = $maxSteps;
43 23
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 4
    public function calculate(array $tiles, Dice $diceResult): GermTile
49
    {
50 4
        $this->tiles = $tiles;
51
52 4
        $this->prepare($diceResult);
53
54 4
        return $this->step();
55
    }
56
57 9
    public function prepare(Dice $diceResult): void
58
    {
59 9
        if ($diceResult->getEntryDirection() === 'left') {
60 4
            $this->reverseTiles();
61
        }
62
63 9
        $this->target = new Target(
64 9
            $diceResult->getGermColor(),
65 9
            $diceResult->getGermSize(),
66 9
            $diceResult->getGermStyle()
67
        );
68 9
        $this->stepStruct = new Step($this->findStartIndex($diceResult->getEntryColor()), 0, $this->maxSteps);
69 9
    }
70
71 7
    public function reverseTiles(): void
72
    {
73 7
        $this->tiles = array_reverse($this->tiles);
74 7
    }
75
76 13
    public function findStartIndex(string $entryColor): int
77
    {
78 13
        foreach ($this->tiles as $tileIndex => $tile) {
79 13
            if (!$tile instanceof EntryTile) {
80 12
                continue;
81
            }
82
83 13
            if ($tile->getColor() !== $entryColor) {
84 2
                continue;
85
            }
86
87 12
            return (int) $tileIndex;
88
        }
89
90 1
        throw new EntryTileMissingException(sprintf('No entry tile with color \'%s\' in the set-up.', $entryColor));
91
    }
92
93 6
    public function step(): GermTile
94
    {
95 6
        $this->stepStruct->increase();
96 6
        $this->stepStruct->checkForOverstep(count($this->tiles));
97
98
        /** @var GermTile $currentTile */
99 6
        $currentTile = $this->tiles[$this->stepStruct->getCurrentIndex()];
100
101 6
        if ($this->isTargetTile($currentTile)) {
102 5
            return $currentTile;
103
        }
104
105 5
        $this->tileHandlerCollector->handle($this->tiles, $this->target, $this->stepStruct);
106
107 5
        return $this->step();
108
    }
109
110 11
    public function isTargetTile(Tile $tile): bool
111
    {
112 11
        if (!$tile instanceof GermTile) {
113 6
            return false;
114
        }
115
116 10
        return $this->target->checkIfSame($tile);
117
    }
118
}
119