Passed
Push — dev ( 840a26...5b15b7 )
by Janko
18:04
created

AnomalyCreation::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 23
ccs 12
cts 12
cp 1
crap 3
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Anomaly;
6
7
use RuntimeException;
8
use Stu\Orm\Entity\AnomalyInterface;
9
use Stu\Orm\Entity\MapInterface;
10
use Stu\Orm\Entity\StarSystemMapInterface;
11
use Stu\Orm\Repository\AnomalyRepositoryInterface;
12
use Stu\Orm\Repository\AnomalyTypeRepositoryInterface;
13
14
final class AnomalyCreation implements AnomalyCreationInterface
15
{
16
    private AnomalyRepositoryInterface $anomalyRepository;
17
18
    private AnomalyTypeRepositoryInterface $anomalyTypeRepository;
19
20 3
    public function __construct(
21
        AnomalyRepositoryInterface $anomalyRepository,
22
        AnomalyTypeRepositoryInterface $anomalyTypeRepository
23
    ) {
24 3
        $this->anomalyRepository = $anomalyRepository;
25 3
        $this->anomalyTypeRepository = $anomalyTypeRepository;
26
    }
27
28 3
    public function create(
29
        int $anomalyType,
30
        MapInterface|StarSystemMapInterface $map
31
    ): AnomalyInterface {
32 3
        $type = $this->anomalyTypeRepository->find($anomalyType);
33
34 3
        if ($type === null) {
35 1
            throw new RuntimeException(sprintf('no anomaly type defined for: %d', $anomalyType));
36
        }
37
38 2
        $anomaly = $this->anomalyRepository->prototype();
39 2
        $anomaly->setAnomalyType($type);
40 2
        $anomaly->setRemainingTicks($type->getLifespanInTicks());
41
42 2
        if ($map instanceof MapInterface) {
43 1
            $anomaly->setMap($map);
44
        } else {
45 1
            $anomaly->setStarsystemMap($map);
46
        }
47
48 2
        $this->anomalyRepository->save($anomaly);
49
50 2
        return $anomaly;
51
    }
52
}
53