Passed
Push — dev ( c4f015...eeaa0f )
by Janko
27:51
created

AnomalyHandling::createNewAnomalies()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 6
rs 10
c 0
b 0
f 0
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\Orm\Entity\AnomalyInterface;
11
use Stu\Orm\Repository\AnomalyRepositoryInterface;
12
13
final class AnomalyHandling implements AnomalyHandlingInterface
14
{
15
    /**
16
     * @param array<int, AnomalyHandlerInterface> $handlerList
17
     */
18 3
    public function __construct(
19
        private AnomalyRepositoryInterface $anomalyRepository,
20
        private array $handlerList
21
    ) {
22 3
    }
23
24 3
    #[Override]
25
    public function processExistingAnomalies(): void
26
    {
27 3
        foreach ($this->anomalyRepository->findAllActive() as $anomaly) {
28 3
            $type = $anomaly->getAnomalyType()->getId();
29
30 3
            if (!array_key_exists($type, $this->handlerList)) {
31 1
                throw new RuntimeException(sprintf('no handler defined for type: %d', $type));
32
            }
33
34 2
            $handler = $this->handlerList[$type];
35
36 2
            $handler->handleShipTick($anomaly);
37 2
            $this->decreaseLifespan($anomaly, $handler);
38
        }
39
    }
40
41
    #[Override]
42
    public function createNewAnomalies(): void
43
    {
44
        foreach ($this->handlerList as $handler) {
45
            $handler->checkForCreation();
46
        }
47
    }
48
49 2
    private function decreaseLifespan(AnomalyInterface $anomaly, AnomalyHandlerInterface $handler): void
50
    {
51 2
        $remainingTicks = $anomaly->getRemainingTicks();
52
53 2
        if ($remainingTicks === 1) {
54 1
            $handler->letAnomalyDisappear($anomaly);
55
        }
56 2
        $anomaly->setRemainingTicks($remainingTicks - 1);
57 2
        $this->anomalyRepository->save($anomaly);
58
    }
59
}
60