Passed
Pull Request — master (#2027)
by Nico
21:19 queued 10:33
created

EffectHandling::walkEffects()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 2
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 12
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 Closure;
8
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use RuntimeException;
10
use Stu\Component\Map\Effects\Type\EffectHandlerInterface;
11
use Stu\Lib\Information\InformationInterface;
12
use Stu\Lib\Map\FieldTypeEffectEnum;
13
use Stu\Module\Spacecraft\Lib\Message\MessageCollectionInterface;
14
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
15
use Stu\Orm\Entity\LocationInterface;
16
17
final class EffectHandling implements EffectHandlingInterface
18
{
19
    /**
20
     * @param array<string, EffectHandlerInterface> $handlerList
21
     */
22 1
    public function __construct(
23
        private array $handlerList
24 1
    ) {}
25
26
    #[Override]
27
    public function handleSpacecraftTick(SpacecraftWrapperInterface $wrapper, InformationInterface $information): void
28
    {
29
        $this->walkEffects($wrapper->get()->getLocation(), function (EffectHandlerInterface $handler) use ($wrapper, $information): void {
30
            $handler->handleSpacecraftTick($wrapper, $information);
31
        });
32
    }
33
34
    #[Override]
35
    public function addFlightInformationForActiveEffects(LocationInterface $location, MessageCollectionInterface $messages): void
36
    {
37
        $this->walkEffects($location, function (EffectHandlerInterface $handler) use ($location, $messages): void {
38
            $handler->addFlightInformation($location, $messages);
39
        });
40
    }
41
42
    #[Override]
43
    public function handleIncomingSpacecraft(SpacecraftWrapperInterface $wrapper, MessageCollectionInterface $messages): void
44
    {
45
        $this->walkEffects($wrapper->get()->getLocation(), function (EffectHandlerInterface $handler) use ($wrapper, $messages): void {
46
            $handler->handleIncomingSpacecraft($wrapper, $messages);
47
        });
48
    }
49
50
    private function walkEffects(LocationInterface $location, callable $func): void
51
    {
52
        foreach ($location->getFieldType()->getEffects() as $effect) {
53
            if ($effect->hasHandler()) {
54
                $func($this->getHandler($effect));
55
            }
56
        }
57
    }
58
59
    private function getHandler(FieldTypeEffectEnum $effect): EffectHandlerInterface
60
    {
61
        if (!array_key_exists($effect->value, $this->handlerList)) {
62
            throw new RuntimeException(sprintf('no handler defined for type: %d', $effect->value));
63
        }
64
65
        $handler = $this->handlerList[$effect->value];
66
67
        return $handler;
68
    }
69
}
70