Passed
Push — dev ( 896d5d...6a73c6 )
by Janko
07:03
created

AnomalyCreation::create()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 39
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 20
nc 9
nop 4
dl 0
loc 39
c 0
b 0
f 0
cc 6
ccs 20
cts 20
cp 1
crap 6
rs 8.9777
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\AnomalyTypeEnum;
10
use Stu\Module\Logging\LogTypeEnum;
11
use Stu\Module\Logging\StuLogger;
12
use Stu\Orm\Entity\Anomaly;
13
use Stu\Orm\Entity\Location;
14
use Stu\Orm\Repository\AnomalyRepositoryInterface;
15
use Stu\Orm\Repository\AnomalyTypeRepositoryInterface;
16
17
final class AnomalyCreation implements AnomalyCreationInterface
18
{
19 4
    public function __construct(
20
        private readonly AnomalyRepositoryInterface $anomalyRepository,
21
        private readonly AnomalyTypeRepositoryInterface $anomalyTypeRepository
22 4
    ) {}
23
24 3
    #[Override]
25
    public function create(
26
        AnomalyTypeEnum $type,
27
        ?Location $location,
28
        ?Anomaly $parent = null,
29
        ?Object $dataObject = null
30
    ): Anomaly {
31
32 3
        $anomalyType = $this->anomalyTypeRepository->find($type->value);
33 3
        if ($anomalyType === null) {
34 1
            throw new RuntimeException(sprintf('no anomaly in database for type: %d', $type->value));
35
        }
36
37 2
        $anomaly = $this->anomalyRepository
38 2
            ->prototype()
39 2
            ->setAnomalyType($anomalyType)
40 2
            ->setRemainingTicks($anomalyType->getLifespanInTicks())
41 2
            ->setLocation($location);
42
43 2
        if ($parent !== null && $location !== null) {
44 1
            $parent->getChildren()->set($location->getId(), $anomaly);
45 1
            $anomaly->setParent($parent);
46
        }
47
48 2
        if ($dataObject !== null) {
49 1
            $json = json_encode($dataObject, JSON_THROW_ON_ERROR);
50 1
            $anomaly->setData($json);
51
        }
52
53 2
        if ($location !== null) {
54
55 2
            StuLogger::log(sprintf('created %s at %s', $type->name, $location->getSectorString()), LogTypeEnum::ANOMALY);
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Stu\Component\Anomaly\Type\AnomalyTypeEnum.
Loading history...
56
57 2
            $location->addAnomaly($anomaly);
58
        }
59
60 2
        $this->anomalyRepository->save($anomaly);
61
62 2
        return $anomaly;
63
    }
64
}
65