Passed
Push — master ( 95eee2...124208 )
by Nico
15:21 queued 07:35
created

ColonyCreation   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 40
ccs 20
cts 20
cp 1
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
final class ColonyCreation implements ColonyCreationInterface
15
{
16
    private ColonyRepositoryInterface $colonyRepository;
17
18
    private UserRepositoryInterface $userRepository;
19
20
    private StuRandom $stuRandom;
21
22 2
    public function __construct(
23
        ColonyRepositoryInterface $colonyRepository,
24
        UserRepositoryInterface $userRepository,
25
        StuRandom $stuRandom
26
    ) {
27 2
        $this->colonyRepository = $colonyRepository;
28 2
        $this->userRepository = $userRepository;
29 2
        $this->stuRandom = $stuRandom;
30
    }
31
32 2
    public function create(StarSystemMapInterface $systemMap, string $name): ColonyInterface
33
    {
34 2
        $colonyClass = $systemMap->getFieldType()->getColonyClass();
35 2
        if ($colonyClass === null) {
36 1
            throw new RuntimeException('colony class can not be null');
37
        }
38
39 1
        $colony = $this->colonyRepository->prototype();
40
41 1
        $colony->setColonyClass($colonyClass);
42 1
        $colony->setUser($this->userRepository->getFallbackUser());
43 1
        $colony->setStarsystemMap($systemMap);
44 1
        $colony->setPlanetName($name);
45 1
        $colony->setRotationFactor($this->stuRandom->rand(
46 1
            $colonyClass->getMinRotation(),
47 1
            $colonyClass->getMaxRotation(),
48 1
            true
49 1
        ));
50
51 1
        $this->colonyRepository->save($colony);
52
53 1
        return $colony;
54
    }
55
}
56