Test Failed
Pull Request — dev (#1952)
by Janko
02:59
created

AnomalyCreation::create()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 36
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 9
nop 4
dl 0
loc 36
ccs 19
cts 19
cp 1
crap 6
rs 9.0111
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\AnomalyTypeEnum;
10
use Stu\Orm\Entity\Anomaly;
11
use Stu\Orm\Entity\Location;
12
use Stu\Orm\Repository\AnomalyRepositoryInterface;
13
use Stu\Orm\Repository\AnomalyTypeRepositoryInterface;
14
15
final class AnomalyCreation implements AnomalyCreationInterface
16
{
17
    public function __construct(
18
        private AnomalyRepositoryInterface $anomalyRepository,
19 4
        private AnomalyTypeRepositoryInterface $anomalyTypeRepository
20
    ) {}
21
22 4
    #[Override]
23
    public function create(
24 4
        AnomalyTypeEnum $type,
25
        ?Location $location,
26
        ?Anomaly $parent = null,
27
        ?Object $dataObject = null
28
    ): Anomaly {
29
30
        $anomalyType = $this->anomalyTypeRepository->find($type->value);
31
        if ($anomalyType === null) {
32 4
            throw new RuntimeException(sprintf('no anomaly in database for type: %d', $type->value));
33 4
        }
34 1
35
        $anomaly = $this->anomalyRepository
36
            ->prototype()
37 3
            ->setAnomalyType($anomalyType)
38 3
            ->setRemainingTicks($anomalyType->getLifespanInTicks())
39 3
            ->setLocation($location);
40 3
41 3
        if ($parent !== null && $location !== null) {
42
            $parent->getChildren()->set($location->getId(), $anomaly);
43 3
            $anomaly->setParent($parent);
44 2
        }
45 2
46
        if ($dataObject !== null) {
47
            $json = json_encode($dataObject, JSON_THROW_ON_ERROR);
48 3
            $anomaly->setData($json);
49 2
        }
50 2
51
        if ($location !== null) {
52
            $location->addAnomaly($anomaly);
53 3
        }
54
55 3
        $this->anomalyRepository->save($anomaly);
56
57 3
        return $anomaly;
58
    }
59
}
60