Passed
Push — dev ( 80c162...225d4f )
by Janko
09:12
created

SpacecraftWrapperFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 8
dl 0
loc 10
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Spacecraft\Lib;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
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...
10
use RuntimeException;
11
use Stu\Component\Spacecraft\Repair\RepairUtilInterface;
12
use Stu\Component\Spacecraft\System\SpacecraftSystemManagerInterface;
13
use Stu\Component\Spacecraft\System\SystemDataDeserializerInterface;
14
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
15
use Stu\Module\Control\GameControllerInterface;
16
use Stu\Module\Ship\Lib\FleetWrapper;
17
use Stu\Module\Ship\Lib\FleetWrapperInterface;
18
use Stu\Module\Ship\Lib\ShipWrapper;
19
use Stu\Module\Spacecraft\Lib\SpacecraftStateChangerInterface;
20
use Stu\Module\Ship\Lib\ShipWrapperInterface;
21
use Stu\Module\Spacecraft\Lib\SpacecraftGroup;
22
use Stu\Module\Spacecraft\Lib\SpacecraftGroupInterface;
23
use Stu\Module\Spacecraft\Lib\Ui\StateIconAndTitle;
24
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
25
use Stu\Module\Station\Lib\StationWrapper;
26
use Stu\Module\Station\Lib\StationWrapperInterface;
27
use Stu\Orm\Entity\FleetInterface;
28
use Stu\Orm\Entity\ShipInterface;
29
use Stu\Orm\Entity\SpacecraftInterface;
30
use Stu\Orm\Entity\StationInterface;
31
use Stu\Orm\Entity\TholianWebInterface;
32
use Stu\Orm\Repository\TorpedoTypeRepositoryInterface;
33
34
final class SpacecraftWrapperFactory implements SpacecraftWrapperFactoryInterface
35
{
36 3
    public function __construct(
37
        private SpacecraftSystemManagerInterface $spacecraftSystemManager,
38
        private ColonyLibFactoryInterface $colonyLibFactory,
39
        private TorpedoTypeRepositoryInterface $torpedoTypeRepository,
40
        private GameControllerInterface $game,
41
        private SpacecraftStateChangerInterface $spacecraftStateChanger,
42
        private RepairUtilInterface $repairUtil,
43
        private StateIconAndTitle $stateIconAndTitle,
44
        private SystemDataDeserializerInterface $systemDataDeserializer
45 3
    ) {}
46
47 43
    #[Override]
48
    public function wrapSpacecraft(SpacecraftInterface $spacecraft): SpacecraftWrapperInterface
49
    {
50 43
        if ($spacecraft instanceof ShipInterface) {
51 36
            return $this->wrapShip($spacecraft);
52
        }
53
54 18
        if ($spacecraft instanceof StationInterface) {
55 18
            return $this->wrapStation($spacecraft);
56
        }
57
58
        if ($spacecraft instanceof TholianWebInterface) {
59
            return $this->wrapTholianWeb($spacecraft);
60
        }
61
62
        throw new RuntimeException('unknown spacecraft type');
63
    }
64
65 19
    #[Override]
66
    public function wrapStation(StationInterface $station): StationWrapperInterface
67
    {
68 19
        return new StationWrapper(
69 19
            $station,
70 19
            $this->spacecraftSystemManager,
71 19
            $this->systemDataDeserializer,
72 19
            $this->torpedoTypeRepository,
73 19
            $this->game,
74 19
            $this,
75 19
            $this->spacecraftStateChanger,
76 19
            $this->repairUtil,
77 19
            $this->stateIconAndTitle
78 19
        );
79
    }
80
81 43
    #[Override]
82
    public function wrapShip(ShipInterface $ship): ShipWrapperInterface
83
    {
84 43
        return new ShipWrapper(
85 43
            $ship,
86 43
            $this->spacecraftSystemManager,
87 43
            $this->systemDataDeserializer,
88 43
            $this->torpedoTypeRepository,
89 43
            $this->game,
90 43
            $this,
91 43
            $this->spacecraftStateChanger,
92 43
            $this->repairUtil,
93 43
            $this->stateIconAndTitle,
94 43
            $this->colonyLibFactory
95 43
        );
96
    }
97
98
    private function wrapTholianWeb(TholianWebInterface $tholianWeb): TholianWebWrapper
99
    {
100
        return new TholianWebWrapper(
101
            $tholianWeb,
102
            $this->spacecraftSystemManager,
103
            $this->systemDataDeserializer,
104
            $this->torpedoTypeRepository,
105
            $this->game,
106
            $this,
107
            $this->spacecraftStateChanger,
108
            $this->repairUtil,
109
            $this->stateIconAndTitle
110
        );
111
    }
112
113 4
    #[Override]
114
    public function wrapShips(array $ships): Collection
115
    {
116 4
        $result = new ArrayCollection();
117
118 4
        foreach ($ships as $key => $ship) {
119 4
            $result->set($key, $this->wrapShip($ship));
120
        }
121
122 4
        return $result;
123
    }
124
125 1
    #[Override]
126
    public function wrapSpacecrafts(array $spacecrafts): Collection
127
    {
128 1
        return (new ArrayCollection($spacecrafts))
129 1
            ->map(fn(SpacecraftInterface $spacecraft): SpacecraftWrapperInterface => $this->wrapSpacecraft($spacecraft));
130
    }
131
132 7
    #[Override]
133
    public function wrapSpacecraftsAsGroups(
134
        Collection $spacecrafts
135
    ): Collection {
136
137
        /** @var Collection<string, SpacecraftGroupInterface> */
138 7
        $groups = new ArrayCollection();
139
140 7
        foreach (SpacecraftGroup::sortSpacecraftCollection($spacecrafts) as $spacecraft) {
141 7
            $fleet = $spacecraft instanceof ShipInterface ? $spacecraft->getFleet() : null;
142 7
            $fleetId = $fleet === null ? 0 : $fleet->getId();
143 7
            $sort = $fleet === null ? PHP_INT_MAX : $fleet->getSort();
144 7
            $groupKey = sprintf('%d_%d', $sort, $fleetId);
145
146 7
            if (!$groups->containsKey($groupKey)) {
147 7
                $groups->set($groupKey, new SpacecraftGroup(
148 7
                    $fleet === null ? 'Einzelschiffe' : $fleet->getName(),
149 7
                    $fleet === null ? null : $fleet->getUser()
150 7
                ));
151
            }
152
            /** @var SpacecraftGroupInterface */
153 7
            $group = $groups->get($groupKey);
154 7
            $group->addSpacecraftWrapper($this->wrapSpacecraft($spacecraft));
155
        }
156
157 7
        return $groups;
158
    }
159
160 2
    #[Override]
161
    public function wrapFleet(FleetInterface $fleet): FleetWrapperInterface
162
    {
163 2
        return new FleetWrapper($fleet, $this, $this->game, false);
164
    }
165
166 1
    #[Override]
167
    public function wrapFleets(array $fleets): array
168
    {
169 1
        return array_map(
170 1
            fn(FleetInterface $fleet): FleetWrapperInterface => $this->wrapFleet($fleet),
171 1
            $fleets
172 1
        );
173
    }
174
}
175