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

EffectHandling::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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