Passed
Pull Request — dev (#2304)
by
unknown
05:16
created

EffectHandling   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 86.36%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 49
ccs 19
cts 22
cp 0.8636
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addFlightInformationForActiveEffects() 0 5 1
A handleIncomingSpacecraft() 0 5 1
A __construct() 0 3 1
A walkEffects() 0 5 3
A getHandler() 0 7 2
A handleSpacecraftTick() 0 5 1
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