Passed
Push — dev ( 45bf8d...5b8f46 )
by Janko
10:07
created

AnomalyHandling   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 63
ccs 24
cts 27
cp 0.8889
rs 10
c 0
b 0
f 0
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A handleIncomingSpacecraft() 0 5 2
A processExistingAnomalies() 0 8 2
A __construct() 0 4 1
A createNewAnomalies() 0 5 2
A decreaseLifespan() 0 14 3
A getHandler() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Anomaly;
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\Anomaly\Type\AnomalyHandlerInterface;
10
use Stu\Module\Spacecraft\Lib\Message\MessageCollectionInterface;
11
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
12
use Stu\Orm\Entity\AnomalyInterface;
13
use Stu\Orm\Repository\AnomalyRepositoryInterface;
14
15
final class AnomalyHandling implements AnomalyHandlingInterface
16
{
17
    /**
18
     * @param array<int, AnomalyHandlerInterface> $handlerList
19
     */
20 5
    public function __construct(
21
        private AnomalyRepositoryInterface $anomalyRepository,
22
        private array $handlerList
23 5
    ) {}
24
25 3
    #[Override]
26
    public function processExistingAnomalies(): void
27
    {
28 3
        foreach ($this->anomalyRepository->findAllRoot() as $root) {
29
30 3
            $handler = $this->getHandler($root);
31 2
            $handler->handleSpacecraftTick($root);
32 2
            $this->decreaseLifespan($root, $handler);
33
        }
34
    }
35
36
    #[Override]
37
    public function createNewAnomalies(): void
38
    {
39
        foreach ($this->handlerList as $handler) {
40
            $handler->checkForCreation();
41
        }
42
    }
43
44 1
    #[Override]
45
    public function handleIncomingSpacecraft(SpacecraftWrapperInterface $wrapper, MessageCollectionInterface $messages): void
46
    {
47 1
        foreach ($wrapper->get()->getLocation()->getAnomalies() as $anomaly) {
48 1
            $this->getHandler($anomaly)->handleIncomingSpacecraft($wrapper, $anomaly, $messages);
49
        }
50
    }
51
52 2
    private function decreaseLifespan(AnomalyInterface $anomaly, AnomalyHandlerInterface $handler): void
53
    {
54 2
        $remainingTicks = $anomaly->getRemainingTicks();
55
56 2
        if ($remainingTicks <= 1) {
57 1
            $handler->letAnomalyDisappear($anomaly);
58 1
            $this->anomalyRepository->delete($anomaly);
59
        } else {
60 1
            $anomaly->changeRemainingTicks(-1);
61 1
            $this->anomalyRepository->save($anomaly);
62
        }
63
64 2
        foreach ($anomaly->getChildren() as $child) {
65 1
            $this->decreaseLifespan($child, $handler);
66
        }
67
    }
68
69 4
    private function getHandler(AnomalyInterface $anomaly): AnomalyHandlerInterface
70
    {
71 4
        $type = $anomaly->getAnomalyType()->getId();
72
73 4
        if (!array_key_exists($type, $this->handlerList)) {
74 1
            throw new RuntimeException(sprintf('no handler defined for type: %d', $type));
75
        }
76
77 3
        return $this->handlerList[$type];
78
    }
79
}
80