Passed
Push — master ( 302539...3c5df9 )
by Nico
10:30 queued 05:14
created

EffectHandling::handleSpacecraftTick()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

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