Passed
Push — dev ( a5a844...c2033c )
by Janko
07:10
created

ShipWrapperFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 11
dl 0
loc 24
ccs 12
cts 12
cp 1
crap 1
rs 9.9
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\Ship\Lib;
6
7
use InvalidArgumentException;
8
use JsonMapper\JsonMapperInterface;
9
use RuntimeException;
10
use Stu\Component\Colony\ColonyFunctionManagerInterface;
11
use Stu\Component\Ship\System\Data\ShipSystemDataFactoryInterface;
12
use Stu\Component\Ship\System\ShipSystemManagerInterface;
13
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
14
use Stu\Module\Control\GameControllerInterface;
15
use Stu\Orm\Entity\Fleet;
16
use Stu\Orm\Entity\FleetInterface;
17
use Stu\Orm\Entity\ShipInterface;
18
use Stu\Orm\Repository\ColonyShipRepairRepositoryInterface;
19
use Stu\Orm\Repository\ShipRepositoryInterface;
20
use Stu\Orm\Repository\TorpedoTypeRepositoryInterface;
21
use Stu\Orm\Repository\UserRepositoryInterface;
22
23
final class ShipWrapperFactory implements ShipWrapperFactoryInterface
24
{
25
    private ShipSystemManagerInterface $shipSystemManager;
26
27
    private ShipRepositoryInterface $shipRepository;
28
29
    private ColonyShipRepairRepositoryInterface $colonyShipRepairRepository;
30
31
    private ColonyLibFactoryInterface $colonyLibFactory;
32
33
    private TorpedoTypeRepositoryInterface $torpedoTypeRepository;
34
35
    private GameControllerInterface $game;
36
37
    private JsonMapperInterface $jsonMapper;
38
39
    private ShipSystemDataFactoryInterface $shipSystemDataFactory;
40
41
    private ColonyFunctionManagerInterface $colonyFunctionManager;
42
43
    private ShipStateChangerInterface $shipStateChanger;
44
45
    private UserRepositoryInterface $userRepository;
46
47 3
    public function __construct(
48
        ColonyFunctionManagerInterface $colonyFunctionManager,
49
        ShipSystemManagerInterface $shipSystemManager,
50
        ShipRepositoryInterface $shipRepository,
51
        ColonyShipRepairRepositoryInterface $colonyShipRepairRepository,
52
        ColonyLibFactoryInterface $colonyLibFactory,
53
        TorpedoTypeRepositoryInterface $torpedoTypeRepository,
54
        GameControllerInterface $game,
55
        JsonMapperInterface $jsonMapper,
56
        ShipSystemDataFactoryInterface $shipSystemDataFactory,
57
        ShipStateChangerInterface $shipStateChanger,
58
        UserRepositoryInterface $userRepository
59
    ) {
60 3
        $this->shipSystemManager = $shipSystemManager;
61 3
        $this->shipRepository = $shipRepository;
62 3
        $this->colonyShipRepairRepository = $colonyShipRepairRepository;
63 3
        $this->colonyLibFactory = $colonyLibFactory;
64 3
        $this->torpedoTypeRepository = $torpedoTypeRepository;
65 3
        $this->game = $game;
66 3
        $this->jsonMapper = $jsonMapper;
67 3
        $this->shipSystemDataFactory = $shipSystemDataFactory;
68 3
        $this->colonyFunctionManager = $colonyFunctionManager;
69 3
        $this->shipStateChanger = $shipStateChanger;
70 3
        $this->userRepository = $userRepository;
71
    }
72
73 3
    public function wrapShip(ShipInterface $ship): ShipWrapperInterface
74
    {
75 3
        return new ShipWrapper(
76 3
            $this->colonyFunctionManager,
77 3
            $ship,
78 3
            $this->shipSystemManager,
79 3
            $this->shipRepository,
80 3
            $this->colonyShipRepairRepository,
81 3
            $this->colonyLibFactory,
82 3
            $this->torpedoTypeRepository,
83 3
            $this->game,
84 3
            $this->jsonMapper,
85 3
            $this,
86 3
            $this->shipSystemDataFactory,
87 3
            $this->shipStateChanger
88 3
        );
89
    }
90
91 3
    public function wrapShips(array $ships): array
92
    {
93 3
        $result = [];
94
95 3
        foreach ($ships as $key => $ship) {
96 3
            $result[$key] = $this->wrapShip($ship);
97
        }
98
99 3
        return $result;
100
    }
101
102 2
    public function wrapShipsAsFleet(array $ships, bool $isSingleShips = false): FleetWrapperInterface
103
    {
104 2
        if (empty($ships)) {
105
            throw new InvalidArgumentException('ship array should not be empty');
106
        }
107
108 2
        $fleet = new Fleet();
109 2
        foreach ($ships as $key => $value) {
110 2
            $fleet->getShips()->set($key, $value);
111
        }
112
113 2
        if ($isSingleShips) {
114 1
            $fleet->setName(_('Einzelschiffe'));
115 1
            $fleet->setUser($this->userRepository->getFallbackUser());
116 1
            $fleet->setSort(PHP_INT_MAX);
117
        } else {
118 1
            $originalFleet = current($ships)->getFleet();
119 1
            if ($originalFleet === null) {
120
                throw new RuntimeException('ship should have fleet');
121
            }
122
123 1
            $fleet->setName($originalFleet->getName());
124 1
            $fleet->setUser(current($ships)->getUser());
125 1
            $fleet->setSort($originalFleet->getSort());
126
        }
127
128 2
        return new FleetWrapper($fleet, $this, $this->game, $isSingleShips);
129
    }
130
131
    public function wrapFleet(FleetInterface $fleet): FleetWrapperInterface
132
    {
133
        return new FleetWrapper($fleet, $this, $this->game, false);
134
    }
135
136
    public function wrapFleets(array $fleets): array
137
    {
138
        return array_map(
139
            function (FleetInterface $fleet): FleetWrapperInterface {
140
                return $this->wrapFleet($fleet);
141
            },
142
            $fleets
143
        );
144
    }
145
}
146