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
|
|
|
|