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

EffectHandling   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 8.69%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 51
ccs 2
cts 23
cp 0.0869
rs 10
c 1
b 0
f 0
wmc 9

6 Methods

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