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

ColonyCreation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 1
rs 10
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