Passed
Push — master ( 6b02b9...a180a6 )
by Nico
26:39 queued 18:43
created

ColonyCreation   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 40
ccs 0
cts 20
cp 0
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A create() 0 22 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Colony;
6
7
use RuntimeException;
8
use Stu\Module\Control\StuRandom;
9
use Stu\Orm\Entity\ColonyInterface;
10
use Stu\Orm\Entity\StarSystemMapInterface;
11
use Stu\Orm\Repository\ColonyRepositoryInterface;
12
use Stu\Orm\Repository\UserRepositoryInterface;
13
14
//TODO unit tests
15
final class ColonyCreation implements ColonyCreationInterface
16
{
17
    private ColonyRepositoryInterface $colonyRepository;
18
19
    private UserRepositoryInterface $userRepository;
20
21
    private StuRandom $stuRandom;
22
23
    public function __construct(
24
        ColonyRepositoryInterface $colonyRepository,
25
        UserRepositoryInterface $userRepository,
26
        StuRandom $stuRandom
27
    ) {
28
        $this->colonyRepository = $colonyRepository;
29
        $this->userRepository = $userRepository;
30
        $this->stuRandom = $stuRandom;
31
    }
32
33
    public function create(StarSystemMapInterface $systemMap, string $name): ColonyInterface
34
    {
35
        $colonyClass = $systemMap->getFieldType()->getColonyClass();
36
        if ($colonyClass === null) {
37
            throw new RuntimeException('colony class can not be null');
38
        }
39
40
        $colony = $this->colonyRepository->prototype();
41
42
        $colony->setColonyClass($colonyClass);
43
        $colony->setUser($this->userRepository->getFallbackUser());
44
        $colony->setStarsystemMap($systemMap);
45
        $colony->setPlanetName($name);
46
        $colony->setRotationFactor($this->stuRandom->rand(
47
            $colonyClass->getMinRotation(),
48
            $colonyClass->getMaxRotation(),
49
            true
50
        ));
51
52
        $this->colonyRepository->save($colony);
53
54
        return $colony;
55
    }
56
}
57