VentTileHandler::supports()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PanicLabCore\Services\Handlers;
6
7
use function count;
8
use PanicLabCore\Structs\Step;
9
use PanicLabCore\Structs\Target;
10
use PanicLabCore\Structs\Tile;
11
use PanicLabCore\Structs\VentTile;
12
13
class VentTileHandler implements TileHandlerInterface
14
{
15 5
    public function supports(Tile $tile): bool
16
    {
17 5
        return $tile instanceof VentTile;
18
    }
19
20
    /**
21
     * @param Tile[] $tiles
22
     */
23 5
    public function handle(array $tiles, Target $target, Step $stepStruct): void
24
    {
25 5
        $allVents = [];
26
27 5
        foreach ($tiles as $key => $tile) {
28 5
            if (!($tile instanceof VentTile)) {
29 5
                continue;
30
            }
31
32 4
            $allVents[] = (int) $key;
33
        }
34
35 5
        $nextVentIndex = 0;
36 5
        $totalVents = count($allVents);
37 5
        foreach ($allVents as $ventKey => $ventIndex) {
38 4
            if ($ventIndex !== $stepStruct->getCurrentIndex()) {
39 4
                continue;
40
            }
41
42 4
            $ventKey = (int) $ventKey;
43
            // Currently active vent is last vent
44 4
            if ($ventKey + 1 === $totalVents) {
45 2
                $nextVentIndex = $allVents[0];
46
            } else {
47 4
                $nextVentIndex = $allVents[$ventKey + 1];
48
            }
49
        }
50
51 5
        $stepStruct->setCurrentIndex($nextVentIndex);
52 5
    }
53
}
54