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

AnomalyCreation   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 37
ccs 15
cts 15
cp 1
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A create() 0 23 3
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