Passed
Pull Request — dev (#2038)
by Janko
09:06
created

CrewCreator::create()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 38
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 27
nc 8
nop 2
dl 0
loc 38
ccs 0
cts 29
cp 0
crap 20
rs 9.488
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Crew\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 Stu\Component\Crew\CrewGenderEnum;
9
use Stu\Component\Crew\CrewOriginException;
10
use Stu\Component\Crew\CrewPositionEnum;
11
use Stu\Orm\Entity\ColonyInterface;
12
use Stu\Orm\Entity\CrewAssignmentInterface;
13
use Stu\Orm\Entity\SpacecraftInterface;
14
use Stu\Orm\Entity\UserInterface;
15
use Stu\Orm\Repository\CrewRaceRepositoryInterface;
16
use Stu\Orm\Repository\CrewRepositoryInterface;
17
use Stu\Orm\Repository\CrewAssignmentRepositoryInterface;
18
use Stu\Orm\Repository\ShipRumpCategoryRoleCrewRepositoryInterface;
19
20
final class CrewCreator implements CrewCreatorInterface
21
{
22 1
    public function __construct(
23
        private CrewRaceRepositoryInterface $crewRaceRepository,
24
        private ShipRumpCategoryRoleCrewRepositoryInterface $shipRumpCategoryRoleCrewRepository,
25
        private CrewAssignmentRepositoryInterface $shipCrewRepository,
26
        private CrewRepositoryInterface $crewRepository
27 1
    ) {}
28
29
    #[Override]
30
    public function create(UserInterface $user, ?ColonyInterface $colony = null): CrewAssignmentInterface
31
    {
32
        $arr = [];
33
        $raceList = $this->crewRaceRepository->getByFaction((int)$user->getFactionId());
34
        foreach ($raceList as $obj) {
35
            $min = key($arr) + 1;
36
            $amount = range($min, $min + $obj->getChance());
37
            array_walk(
38
                $amount,
39
                function (&$value) use ($obj): void {
40
                    $value = $obj->getId();
41
                }
42
            );
43
            $arr = [...$arr, ...$amount];
44
        }
45
        $race = $this->crewRaceRepository->find($arr[array_rand($arr)]);
46
47
        $gender = random_int(1, 100) > $race->getMaleRatio() ? CrewGenderEnum::FEMALE : CrewGenderEnum::MALE;
48
49
        $crew = $this->crewRepository->prototype();
50
51
        $crew->setUser($user);
52
        $crew->setName('Crew');
53
        $crew->setRace($race);
0 ignored issues
show
Bug introduced by
It seems like $race can also be of type null; however, parameter $crewRace of Stu\Orm\Entity\CrewInterface::setRace() does only seem to accept Stu\Orm\Entity\CrewRaceInterface, 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

53
        $crew->setRace(/** @scrutinizer ignore-type */ $race);
Loading history...
54
        $crew->setGender($gender);
55
        $this->crewRepository->save($crew);
56
57
        $crewAssignment = $this->shipCrewRepository->prototype();
58
        $crewAssignment->setUser($user);
59
        $crewAssignment->setCrew($crew);
60
        if ($colony !== null) {
61
            $crewAssignment->setColony($colony);
62
            $colony->getCrewAssignments()->add($crewAssignment);
63
        }
64
        $this->shipCrewRepository->save($crewAssignment);
65
66
        return $crewAssignment;
67
    }
68
69
    #[Override]
70
    public function createCrewAssignments(
71
        SpacecraftInterface $spacecraft,
72
        ColonyInterface|SpacecraftInterface $crewProvider,
73
        ?int $amount = null
74
    ): void {
75
        $crewToSetup = $amount ?? $spacecraft->getBuildPlan()->getCrew();
76
77
        foreach (CrewPositionEnum::getOrder() as $position) {
78
            $createdcount = 1;
79
            $slot = $position === CrewPositionEnum::CREWMAN ? 'getJob6Crew' : 'getJob' . $position->value . 'Crew';
80
            $config = $this->shipRumpCategoryRoleCrewRepository->getByShipRumpCategoryAndRole(
81
                $spacecraft->getRump()->getShipRumpCategory()->getId(),
82
                $spacecraft->getRump()->getShipRumpRole()->getId()
83
            );
84
85
            while ($crewToSetup > 0 && ($position === CrewPositionEnum::CREWMAN || $createdcount <= $config->$slot())) {
86
                $createdcount++;
87
                $crewToSetup--;
88
89
                $crewAssignment = $this->getCrewByType($position, $crewProvider);
90
                if ($crewAssignment === null) {
91
                    $crewAssignment = $this->getCrew($crewProvider);
92
                }
93
94
                if ($crewAssignment === null) {
95
                    throw new CrewOriginException('no assignable crew found');
96
                }
97
98
                $crewAssignment->setSpacecraft($spacecraft);
99
                $crewAssignment->setColony(null);
100
                $crewAssignment->setTradepost(null);
101
                $crewAssignment->setUser($spacecraft->getUser());
102
                $crewAssignment->setPosition($position);
103
104
                $spacecraft->getCrewAssignments()->add($crewAssignment);
105
106
                $this->shipCrewRepository->save($crewAssignment);
107
            }
108
        }
109
    }
110
111
    private function getCrewByType(
112
        CrewPositionEnum $position,
113
        ColonyInterface|SpacecraftInterface $crewProvider
114
    ): ?CrewAssignmentInterface {
115
116
        $filteredCrewAssignments = $crewProvider
117
            ->getCrewAssignments()
118
            ->filter(fn(CrewAssignmentInterface $assignment): bool => $assignment->getCrew()->isSkilledAt($position))
119
            ->toArray();
120
121
        if ($filteredCrewAssignments === []) {
122
            return null;
123
        }
124
125
        usort(
126
            $filteredCrewAssignments,
127
            fn(CrewAssignmentInterface $a, CrewAssignmentInterface $b): int =>
128
            $b->getCrew()->getSkills()->get($position->value)->getExpertise() - $a->getCrew()->getSkills()->get($position->value)->getExpertise()
129
        );
130
131
132
        $crewAssignment = current($filteredCrewAssignments);
133
        $crewProvider->getCrewAssignments()->removeElement($crewAssignment);
134
135
        return $crewAssignment;
136
    }
137
138
    private function getCrew(ColonyInterface|SpacecraftInterface $crewProvider): ?CrewAssignmentInterface
139
    {
140
        $crewAssignments = $crewProvider->getCrewAssignments();
141
        if ($crewAssignments->isEmpty()) {
142
            return null;
143
        }
144
145
        /** @var CrewAssignmentInterface $random */
146
        $random = $crewAssignments->get(array_rand($crewAssignments->toArray()));
0 ignored issues
show
Bug introduced by
It seems like array_rand($crewAssignments->toArray()) can also be of type array; however, parameter $key of Doctrine\Common\Collecti...adableCollection::get() does only seem to accept integer|string, 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

146
        $random = $crewAssignments->get(/** @scrutinizer ignore-type */ array_rand($crewAssignments->toArray()));
Loading history...
147
148
        $crewAssignments->removeElement($random);
149
150
        return $random;
151
    }
152
}
153