Passed
Push — master ( 66afc8...ff3c83 )
by Janko
09:01
created

CrewCreator   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Test Coverage

Coverage 2.38%

Importance

Changes 0
Metric Value
eloc 77
c 0
b 0
f 0
dl 0
loc 145
ccs 2
cts 84
cp 0.0238
rs 10
wmc 21

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getCrewByType() 0 15 3
A getCrew() 0 14 2
A __construct() 0 7 1
B create() 0 49 6
B createCrewAssignment() 0 49 9
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\CrewTypeEnum;
10
use Stu\Component\Crew\CrewOriginException;
11
use Stu\Exception\SanityCheckException;
12
use Stu\Orm\Entity\Colony;
13
use Stu\Orm\Entity\Crew;
14
use Stu\Orm\Entity\CrewAssignment;
15
use Stu\Orm\Entity\Spacecraft;
16
use Stu\Orm\Repository\CrewRaceRepositoryInterface;
17
use Stu\Orm\Repository\CrewRepositoryInterface;
18
use Stu\Orm\Repository\CrewAssignmentRepositoryInterface;
19
use Stu\Orm\Repository\ShipRumpCategoryRoleCrewRepositoryInterface;
20
use Stu\Orm\Repository\UserRepositoryInterface;
21
22
final class CrewCreator implements CrewCreatorInterface
23
{
24 1
    public function __construct(
25
        private CrewRaceRepositoryInterface $crewRaceRepository,
26
        private ShipRumpCategoryRoleCrewRepositoryInterface $shipRumpCategoryRoleCrewRepository,
27
        private CrewAssignmentRepositoryInterface $shipCrewRepository,
28
        private CrewRepositoryInterface $crewRepository,
29
        private UserRepositoryInterface $userRepository
30 1
    ) {}
31
32
    #[Override]
33
    public function create(int $userId, ?Colony $colony = null): CrewAssignment
34
    {
35
        $user = $this->userRepository->find($userId);
36
37
        if ($user === null) {
38
            throw new RuntimeException('user not found1');
39
        }
40
41
        $arr = [];
42
        $raceList = $this->crewRaceRepository->getByFaction($user->getFactionId());
43
        foreach ($raceList as $obj) {
44
            $min = key($arr) + 1;
45
            $amount = range($min, $min + $obj->getChance());
46
            array_walk(
47
                $amount,
48
                function (&$value) use ($obj): void {
49
                    $value = $obj->getId();
50
                }
51
            );
52
            $arr = [...$arr, ...$amount];
53
        }
54
        $randomRaceId = $arr[array_rand($arr)];
55
        $race = $this->crewRaceRepository->find($randomRaceId);
56
        if ($race === null) {
57
            throw new SanityCheckException(sprintf('raceId %d does not exist', $randomRaceId));
58
        }
59
60
        $gender = random_int(1, 100) > $race->getMaleRatio() ? Crew::CREW_GENDER_FEMALE : Crew::CREW_GENDER_MALE;
61
62
        $crew = $this->crewRepository->prototype();
63
64
        $crew->setUser($user);
65
        $crew->setName('Crew');
66
        $crew->setRace($race);
67
        $crew->setGender($gender);
68
        $crew->setType(CrewTypeEnum::CREWMAN);
69
        $this->crewRepository->save($crew);
70
71
        $crewAssignment = $this->shipCrewRepository->prototype();
72
        $crewAssignment->setUser($user);
73
        $crewAssignment->setCrew($crew);
74
        if ($colony !== null) {
75
            $crewAssignment->setColony($colony);
76
            $colony->getCrewAssignments()->add($crewAssignment);
77
        }
78
        $this->shipCrewRepository->save($crewAssignment);
79
80
        return $crewAssignment;
81
    }
82
83
    #[Override]
84
    public function createCrewAssignment(
85
        Spacecraft $spacecraft,
86
        Colony|Spacecraft $crewProvider,
87
        ?int $amount = null
88
    ): void {
89
        $crewToSetup = $amount ?? $spacecraft->getBuildPlan()?->getCrew() ?? 0;
90
        $shipRumpRole = $spacecraft->getRump()->getShipRumpRole();
91
        if ($shipRumpRole === null) {
92
            throw new SanityCheckException(sprintf('rumpId %d does not have rump role', $spacecraft->getRump()->getId()));
93
        }
94
95
        foreach (CrewTypeEnum::getOrder() as $crewType) {
96
            $createdcount = 1;
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 == CrewTypeEnum::CREWMAN || $createdcount <= $config->getCrewForPosition($crewType))) {
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
        CrewTypeEnum $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