Passed
Push — master ( 5a11dc...578008 )
by Janko
08:39
created

PlanetColonization   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 7.14%

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 58
ccs 2
cts 28
cp 0.0714
rs 10
c 0
b 0
f 0
wmc 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Colony\Lib;
6
7
use Override;
8
use RuntimeException;
9
use Stu\Component\Building\BuildingManagerInterface;
10
use Stu\Lib\Transfer\Storage\StorageManagerInterface;
11
use Stu\Module\Commodity\CommodityTypeConstants;
12
use Stu\Orm\Entity\Building;
13
use Stu\Orm\Entity\Colony;
14
use Stu\Orm\Entity\PlanetField;
15
use Stu\Orm\Entity\User;
16
use Stu\Orm\Repository\ColonyRepositoryInterface;
17
use Stu\Orm\Repository\CommodityRepositoryInterface;
18
use Stu\Orm\Repository\PlanetFieldRepositoryInterface;
19
20
final class PlanetColonization implements PlanetColonizationInterface
21
{
22
    private const int COLONY_FIELDTYPE_MEADOW = 101;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 22 at column 22
Loading history...
23
24 1
    public function __construct(
25
        private readonly PlanetFieldRepositoryInterface $planetFieldRepository,
26
        private readonly CommodityRepositoryInterface $commodityRepository,
27
        private readonly StorageManagerInterface $storageManager,
28
        private readonly ColonyLibFactoryInterface $colonyLibFactory,
29
        private readonly ColonyRepositoryInterface $colonyRepository,
30
        private readonly BuildingManagerInterface $buildingManager
31 1
    ) {}
32
33
    #[Override]
34
    public function colonize(
35
        Colony $colony,
36
        User $user,
37
        Building $building,
38
        ?PlanetField $field = null
39
    ): void {
40
        if (!$colony->isFree()) {
41
            return;
42
        }
43
44
        $this->colonyLibFactory->createColonySurface($colony)->updateSurface();
45
46
        if ($field === null) {
47
            $list = $this->planetFieldRepository->getByColonyAndType(
48
                $colony->getId(),
49
                self::COLONY_FIELDTYPE_MEADOW
50
            );
51
52
            shuffle($list);
53
54
            /** @var PlanetField $field */
55
            $field = current($list);
56
        }
57
58
        $field->setBuilding($building);
59
60
        $colony->getChangeable()->setWorkless($building->getHousing());
61
        $colony->getChangeable()->setEps($building->getEpsStorage());
62
63
        $this->buildingManager->finish($field, true);
64
65
        $colony->setUser($user);
66
        $colony->setName(_('Kolonie'));
67
68
        $this->colonyRepository->save($colony);
69
        $this->planetFieldRepository->save($field);
70
71
        $commodity = $this->commodityRepository->find(CommodityTypeConstants::COMMODITY_BUILDING_MATERIALS);
72
        if ($commodity === null) {
73
            throw new RuntimeException('commodity does not exist');
74
        }
75
76
        $this->storageManager->upperStorage(
77
            $colony,
78
            $commodity,
79
            150
80
        );
81
    }
82
}
83