Passed
Pull Request — master (#2027)
by Janko
10:08
created

EffectHandling   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 13.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 38
ccs 2
cts 15
cp 0.1333
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getHandler() 0 9 2
A handleSpacecraftTick() 0 6 3
A handleIncomingSpacecraft() 0 6 3
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
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
    #[Override]
25
    public function handleSpacecraftTick(SpacecraftWrapperInterface $wrapper, InformationInterface $information): void
26
    {
27
        foreach ($wrapper->get()->getLocation()->getFieldType()->getEffects() as $effect) {
28
            if ($effect->hasHandler()) {
29
                $this->getHandler($effect)->handleSpacecraftTick($wrapper, $information);
30
            }
31
        }
32
    }
33
34
    #[Override]
35
    public function handleIncomingSpacecraft(SpacecraftWrapperInterface $wrapper, MessageCollectionInterface $messages): void
36
    {
37
        foreach ($wrapper->get()->getLocation()->getFieldType()->getEffects() as $effect) {
38
            if ($effect->hasHandler()) {
39
                $this->getHandler($effect)->handleIncomingSpacecraft($wrapper, $messages);
40
            }
41
        }
42
    }
43
44
    private function getHandler(FieldTypeEffectEnum $effect): EffectHandlerInterface
45
    {
46
        if (!array_key_exists($effect->value, $this->handlerList)) {
47
            throw new RuntimeException(sprintf('no handler defined for type: %d', $effect->value));
48
        }
49
50
        $handler = $this->handlerList[$effect->value];
51
52
        return $handler;
53
    }
54
}
55