Passed
Push — dev ( 368876...ca533f )
by Janko
16:00
created

SpacecraftWrapperFactory::wrapShips()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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