Passed
Push — dev ( 7914d2...bbc331 )
by Janko
10:29
created

CrewCreator   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Test Coverage

Coverage 2.35%

Importance

Changes 0
Metric Value
eloc 78
dl 0
loc 146
ccs 2
cts 85
cp 0.0235
rs 10
c 0
b 0
f 0
wmc 22

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getCrewByType() 0 15 3
B create() 0 49 6
B createCrewAssignment() 0 50 10
A getCrew() 0 14 2
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 RuntimeException;
9
use Stu\Component\Crew\CrewEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Crew\CrewEnum 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...
10
use Stu\Component\Crew\CrewOriginException;
11
use Stu\Exception\SanityCheckException;
12
use Stu\Orm\Entity\Colony;
13
use Stu\Orm\Entity\CrewAssignment;
14
use Stu\Orm\Entity\Spacecraft;
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
use Stu\Orm\Repository\UserRepositoryInterface;
20
21
final class CrewCreator implements CrewCreatorInterface
22
{
23 1
    public function __construct(
24
        private CrewRaceRepositoryInterface $crewRaceRepository,
25
        private ShipRumpCategoryRoleCrewRepositoryInterface $shipRumpCategoryRoleCrewRepository,
26
        private CrewAssignmentRepositoryInterface $shipCrewRepository,
27
        private CrewRepositoryInterface $crewRepository,
28
        private UserRepositoryInterface $userRepository
29 1
    ) {}
30
31
    #[Override]
32
    public function create(int $userId, ?Colony $colony = null): CrewAssignment
33
    {
34
        $user = $this->userRepository->find($userId);
35
36
        if ($user === null) {
37
            throw new RuntimeException('user not found1');
38
        }
39
40
        $arr = [];
41
        $raceList = $this->crewRaceRepository->getByFaction($user->getFactionId());
42
        foreach ($raceList as $obj) {
43
            $min = key($arr) + 1;
44
            $amount = range($min, $min + $obj->getChance());
45
            array_walk(
46
                $amount,
47
                function (&$value) use ($obj): void {
48
                    $value = $obj->getId();
49
                }
50
            );
51
            $arr = [...$arr, ...$amount];
52
        }
53
        $randomRaceId = $arr[array_rand($arr)];
54
        $race = $this->crewRaceRepository->find($randomRaceId);
55
        if ($race === null) {
56
            throw new SanityCheckException(sprintf('raceId %d does not exist', $randomRaceId));
57
        }
58
59
        $gender = random_int(1, 100) > $race->getMaleRatio() ? CrewEnum::CREW_GENDER_FEMALE : CrewEnum::CREW_GENDER_MALE;
60
61
        $crew = $this->crewRepository->prototype();
62
63
        $crew->setUser($user);
64
        $crew->setName('Crew');
65
        $crew->setRace($race);
66
        $crew->setGender($gender);
67
        $crew->setType(CrewEnum::CREW_TYPE_CREWMAN);
68
        $this->crewRepository->save($crew);
69
70
        $crewAssignment = $this->shipCrewRepository->prototype();
71
        $crewAssignment->setUser($user);
72
        $crewAssignment->setCrew($crew);
73
        if ($colony !== null) {
74
            $crewAssignment->setColony($colony);
75
            $colony->getCrewAssignments()->add($crewAssignment);
76
        }
77
        $this->shipCrewRepository->save($crewAssignment);
78
79
        return $crewAssignment;
80
    }
81
82
    #[Override]
83
    public function createCrewAssignment(
84
        Spacecraft $spacecraft,
85
        Colony|Spacecraft $crewProvider,
86
        ?int $amount = null
87
    ): void {
88
        $crewToSetup = $amount ?? $spacecraft->getBuildPlan()?->getCrew() ?? 0;
89
        $shipRumpRole = $spacecraft->getRump()->getShipRumpRole();
90
        if ($shipRumpRole === null) {
91
            throw new SanityCheckException(sprintf('rumpId %d does not have rump role', $spacecraft->getRump()->getId()));
92
        }
93
94
        foreach (CrewEnum::CREW_ORDER as $crewType) {
95
            $createdcount = 1;
96
            $slot = $crewType == CrewEnum::CREW_TYPE_CREWMAN ? 'getJob6Crew' : 'getJob' . $crewType . 'Crew';
97
            $config = $this->shipRumpCategoryRoleCrewRepository->getByShipRumpCategoryAndRole(
98
                $spacecraft->getRump()->getShipRumpCategory()->getId(),
99
                $shipRumpRole->getId()
100
            );
101
            if ($config === null) {
102
                throw new RuntimeException(sprintf(
103
                    'no rump category role crew for rumpCategoryId: %d, rumpRoleId: %d',
104
                    $spacecraft->getRump()->getShipRumpCategory()->getId()->value,
105
                    $shipRumpRole->getId()->value
106
                ));
107
            }
108
109
            while ($crewToSetup > 0 && ($crewType == CrewEnum::CREW_TYPE_CREWMAN || $createdcount <= $config->$slot())) {
110
                $createdcount++;
111
                $crewToSetup--;
112
113
                $crewAssignment = $this->getCrewByType($crewType, $crewProvider);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $crewAssignment is correct as $this->getCrewByType($crewType, $crewProvider) targeting Stu\Module\Crew\Lib\CrewCreator::getCrewByType() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
114
                if ($crewAssignment === null) {
115
                    $crewAssignment = $this->getCrew($crewProvider);
116
                }
117
118
                if ($crewAssignment === null) {
119
                    throw new CrewOriginException('no assignable crew found');
120
                }
121
122
                $crewAssignment->setSpacecraft($spacecraft);
123
                $crewAssignment->setColony(null);
124
                $crewAssignment->setTradepost(null);
125
                //TODO set both ship and crew user
126
                $crewAssignment->setUser($spacecraft->getUser());
127
                $crewAssignment->setSlot($crewType);
128
129
                $spacecraft->getCrewAssignments()->add($crewAssignment);
130
131
                $this->shipCrewRepository->save($crewAssignment);
132
            }
133
        }
134
    }
135
136
    private function getCrewByType(
137
        int $crewType,
138
        Colony|Spacecraft $crewProvider
139
    ): ?CrewAssignment {
140
141
        foreach ($crewProvider->getCrewAssignments() as $crewAssignment) {
142
            $crew = $crewAssignment->getCrew();
143
            if ($crew->getType() === $crewType) {
144
                $crewProvider->getCrewAssignments()->removeElement($crewAssignment);
145
146
                return $crewAssignment;
147
            }
148
        }
149
150
        return null;
151
    }
152
153
    private function getCrew(Colony|Spacecraft $crewProvider): ?CrewAssignment
154
    {
155
        $crewAssignments = $crewProvider->getCrewAssignments();
156
157
        if ($crewAssignments->isEmpty()) {
158
            return null;
159
        }
160
161
        /** @var CrewAssignment $random */
162
        $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

162
        $random = $crewAssignments->get(/** @scrutinizer ignore-type */ array_rand($crewAssignments->toArray()));
Loading history...
163
164
        $crewAssignments->removeElement($random);
165
166
        return $random;
167
    }
168
}
169