Passed
Push — dev ( 25004f...be11ce )
by Janko
16:12
created

PlanetColonization::colonize()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 47
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 25
nc 5
nop 4
dl 0
loc 47
ccs 0
cts 26
cp 0
crap 20
rs 9.52
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Colony\Lib;
6
7
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use RuntimeException;
9
use Stu\Component\Building\BuildingManagerInterface;
10
use Stu\Component\Colony\ColonyEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Colony\ColonyEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Stu\Lib\Transfer\Storage\StorageManagerInterface;
12
use Stu\Module\Commodity\CommodityTypeEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\Commodity\CommodityTypeEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Stu\Orm\Entity\BuildingInterface;
14
use Stu\Orm\Entity\ColonyInterface;
15
use Stu\Orm\Entity\PlanetFieldInterface;
16
use Stu\Orm\Repository\ColonyRepositoryInterface;
17
use Stu\Orm\Repository\CommodityRepositoryInterface;
18
use Stu\Orm\Repository\PlanetFieldRepositoryInterface;
19
use Stu\Orm\Repository\UserRepositoryInterface;
20
21
final class PlanetColonization implements PlanetColonizationInterface
22
{
23 1
    public function __construct(
24
        private readonly PlanetFieldRepositoryInterface $planetFieldRepository,
25
        private readonly CommodityRepositoryInterface $commodityRepository,
26
        private readonly StorageManagerInterface $storageManager,
27
        private readonly ColonyLibFactoryInterface $colonyLibFactory,
28
        private readonly ColonyRepositoryInterface $colonyRepository,
29
        private readonly UserRepositoryInterface $userRepository,
30
        private readonly BuildingManagerInterface $buildingManager
31 1
    ) {}
32
33
    #[Override]
34
    public function colonize(
35
        ColonyInterface $colony,
36
        int $userId,
37
        BuildingInterface $building,
38
        ?PlanetFieldInterface $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
                ColonyEnum::COLONY_FIELDTYPE_MEADOW
50
            );
51
52
            shuffle($list);
53
54
            /** @var PlanetFieldInterface $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($this->userRepository->find($userId));
0 ignored issues
show
Bug introduced by
It seems like $this->userRepository->find($userId) can also be of type null; however, parameter $user of Stu\Orm\Entity\ColonyInterface::setUser() does only seem to accept Stu\Orm\Entity\UserInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
        $colony->setUser(/** @scrutinizer ignore-type */ $this->userRepository->find($userId));
Loading history...
66
        $colony->setName(_('Kolonie'));
67
68
        $this->colonyRepository->save($colony);
69
        $this->planetFieldRepository->save($field);
70
71
        $commodity = $this->commodityRepository->find(CommodityTypeEnum::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