Passed
Push — master ( c9be78...243230 )
by Nico
22:39
created

CrewCreator   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Test Coverage

Coverage 2.53%

Importance

Changes 0
Metric Value
eloc 72
dl 0
loc 138
ccs 2
cts 79
cp 0.0253
rs 10
c 0
b 0
f 0
wmc 20

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A create() 0 45 5
A getCrewByType() 0 15 3
B createCrewAssignment() 0 46 9
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\Orm\Entity\ColonyInterface;
12
use Stu\Orm\Entity\CrewAssignmentInterface;
13
use Stu\Orm\Entity\SpacecraftInterface;
14
use Stu\Orm\Repository\CrewRaceRepositoryInterface;
15
use Stu\Orm\Repository\CrewRepositoryInterface;
16
use Stu\Orm\Repository\CrewAssignmentRepositoryInterface;
17
use Stu\Orm\Repository\ShipRumpCategoryRoleCrewRepositoryInterface;
18
use Stu\Orm\Repository\UserRepositoryInterface;
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
        private UserRepositoryInterface $userRepository
28 1
    ) {}
29
30
    #[Override]
31
    public function create(int $userId, ?ColonyInterface $colony = null): CrewAssignmentInterface
32
    {
33
        $user = $this->userRepository->find($userId);
34
35
        if ($user === null) {
36
            throw new RuntimeException('user not found1');
37
        }
38
39
        $arr = [];
40
        $raceList = $this->crewRaceRepository->getByFaction((int)$user->getFactionId());
41
        foreach ($raceList as $obj) {
42
            $min = key($arr) + 1;
43
            $amount = range($min, $min + $obj->getChance());
44
            array_walk(
45
                $amount,
46
                function (&$value) use ($obj): void {
47
                    $value = $obj->getId();
48
                }
49
            );
50
            $arr = [...$arr, ...$amount];
51
        }
52
        $race = $this->crewRaceRepository->find($arr[array_rand($arr)]);
53
54
        $gender = random_int(1, 100) > $race->getMaleRatio() ? CrewEnum::CREW_GENDER_FEMALE : CrewEnum::CREW_GENDER_MALE;
55
56
        $crew = $this->crewRepository->prototype();
57
58
        $crew->setUser($user);
59
        $crew->setName('Crew');
60
        $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

60
        $crew->setRace(/** @scrutinizer ignore-type */ $race);
Loading history...
61
        $crew->setGender($gender);
62
        $crew->setType(CrewEnum::CREW_TYPE_CREWMAN);
63
        $this->crewRepository->save($crew);
64
65
        $crewAssignment = $this->shipCrewRepository->prototype();
66
        $crewAssignment->setUser($user);
67
        $crewAssignment->setCrew($crew);
68
        if ($colony !== null) {
69
            $crewAssignment->setColony($colony);
70
            $colony->getCrewAssignments()->add($crewAssignment);
71
        }
72
        $this->shipCrewRepository->save($crewAssignment);
73
74
        return $crewAssignment;
75
    }
76
77
    #[Override]
78
    public function createCrewAssignment(
79
        SpacecraftInterface $spacecraft,
80
        ColonyInterface|SpacecraftInterface $crewProvider,
81
        ?int $amount = null
82
    ): void {
83
        $crewToSetup = $amount ?? $spacecraft->getBuildPlan()->getCrew();
84
85
        foreach (CrewEnum::CREW_ORDER as $crewType) {
86
            $createdcount = 1;
87
            $slot = $crewType == CrewEnum::CREW_TYPE_CREWMAN ? 'getJob6Crew' : 'getJob' . $crewType . 'Crew';
88
            $config = $this->shipRumpCategoryRoleCrewRepository->getByShipRumpCategoryAndRole(
89
                $spacecraft->getRump()->getShipRumpCategory()->getId(),
90
                $spacecraft->getRump()->getShipRumpRole()->getId()
91
            );
92
            if ($config === null) {
93
                throw new RuntimeException(sprintf(
94
                    'no rump category role crew for rumpCategoryId: %d, rumpRoleId: %d',
95
                    $spacecraft->getRump()->getShipRumpCategory()->getId(),
96
                    $spacecraft->getRump()->getShipRumpRole()->getId()
97
                ));
98
            }
99
100
            while ($crewToSetup > 0 && ($crewType == CrewEnum::CREW_TYPE_CREWMAN || $createdcount <= $config->$slot())) {
101
                $createdcount++;
102
                $crewToSetup--;
103
104
                $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...
105
                if ($crewAssignment === null) {
106
                    $crewAssignment = $this->getCrew($crewProvider);
107
                }
108
109
                if ($crewAssignment === null) {
110
                    throw new CrewOriginException('no assignable crew found');
111
                }
112
113
                $crewAssignment->setSpacecraft($spacecraft);
114
                $crewAssignment->setColony(null);
115
                $crewAssignment->setTradepost(null);
116
                //TODO set both ship and crew user
117
                $crewAssignment->setUser($spacecraft->getUser());
118
                $crewAssignment->setSlot($crewType);
119
120
                $spacecraft->getCrewAssignments()->add($crewAssignment);
121
122
                $this->shipCrewRepository->save($crewAssignment);
123
            }
124
        }
125
    }
126
127
    private function getCrewByType(
128
        int $crewType,
129
        ColonyInterface|SpacecraftInterface $crewProvider
130
    ): ?CrewAssignmentInterface {
131
132
        foreach ($crewProvider->getCrewAssignments() as $crewAssignment) {
133
            $crew = $crewAssignment->getCrew();
134
            if ($crew->getType() === $crewType) {
135
                $crewProvider->getCrewAssignments()->removeElement($crewAssignment);
136
137
                return $crewAssignment;
138
            }
139
        }
140
141
        return null;
142
    }
143
144
    private function getCrew(ColonyInterface|SpacecraftInterface $crewProvider): ?CrewAssignmentInterface
145
    {
146
        $crewAssignments = $crewProvider->getCrewAssignments();
147
148
        if ($crewAssignments->isEmpty()) {
149
            return null;
150
        }
151
152
        /** @var CrewAssignmentInterface $random */
153
        $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

153
        $random = $crewAssignments->get(/** @scrutinizer ignore-type */ array_rand($crewAssignments->toArray()));
Loading history...
154
155
        $crewAssignments->removeElement($random);
156
157
        return $random;
158
    }
159
}
160