Passed
Push — dev ( 7dee43...836565 )
by Nico
06:34
created

EffectHandling::handleSpacecraftTick()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Map\Effects;
6
7
use RuntimeException;
8
use Stu\Component\Map\Effects\Type\EffectHandlerInterface;
9
use Stu\Lib\Information\InformationInterface;
10
use Stu\Lib\Map\FieldTypeEffectEnum;
11
use Stu\Module\Spacecraft\Lib\Message\MessageCollectionInterface;
12
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
13
use Stu\Orm\Entity\Location;
14
15
final class EffectHandling implements EffectHandlingInterface
16
{
17
    /**
18
     * @param array<string, EffectHandlerInterface> $handlerList
19
     */
20 1
    public function __construct(
21
        private array $handlerList
22 1
    ) {}
23
24 1
    #[\Override]
25
    public function handleSpacecraftTick(SpacecraftWrapperInterface $wrapper, InformationInterface $information): void
26
    {
27 1
        $this->walkEffects($wrapper->get()->getLocation(), function (EffectHandlerInterface $handler) use ($wrapper, $information): void {
28 1
            $handler->handleSpacecraftTick($wrapper, $information);
29 1
        });
30
    }
31
32 4
    #[\Override]
33
    public function addFlightInformationForActiveEffects(Location $location, MessageCollectionInterface $messages): void
34
    {
35 4
        $this->walkEffects($location, function (EffectHandlerInterface $handler) use ($location, $messages): void {
36
            $handler->addFlightInformation($location, $messages);
37 4
        });
38
    }
39
40 4
    #[\Override]
41
    public function handleIncomingSpacecraft(SpacecraftWrapperInterface $wrapper, MessageCollectionInterface $messages): void
42
    {
43 4
        $this->walkEffects($wrapper->get()->getLocation(), function (EffectHandlerInterface $handler) use ($wrapper, $messages): void {
44
            $handler->handleIncomingSpacecraft($wrapper, $messages);
45 4
        });
46
    }
47
48 5
    private function walkEffects(Location $location, callable $func): void
49
    {
50 5
        foreach ($location->getFieldType()->getEffects() as $effect) {
51 2
            if ($effect->hasHandler()) {
52 1
                $func($this->getHandler($effect));
53
            }
54
        }
55
    }
56
57 1
    private function getHandler(FieldTypeEffectEnum $effect): EffectHandlerInterface
58
    {
59 1
        if (!array_key_exists($effect->value, $this->handlerList)) {
60
            throw new RuntimeException(sprintf('no handler defined for type: %d', $effect->value));
61
        }
62
63 1
        return $this->handlerList[$effect->value];
64
    }
65
}
66