Passed
Pull Request — master (#1821)
by Nico
52:02 queued 25:23
created

ShipWrapperFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 9
dl 0
loc 11
ccs 1
cts 1
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\Ship\Lib;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use InvalidArgumentException;
10
use JBBCode\Parser;
11
use RuntimeException;
12
use Stu\Component\Ship\Repair\RepairUtilInterface;
13
use Stu\Component\Ship\System\ShipSystemManagerInterface;
14
use Stu\Component\Ship\System\SystemDataDeserializerInterface;
15
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
16
use Stu\Module\Control\GameControllerInterface;
17
use Stu\Orm\Entity\Fleet;
18
use Stu\Orm\Entity\FleetInterface;
19
use Stu\Orm\Entity\ShipInterface;
20
use Stu\Orm\Repository\TorpedoTypeRepositoryInterface;
21
use Stu\Orm\Repository\UserRepositoryInterface;
22
23
final class ShipWrapperFactory implements ShipWrapperFactoryInterface
24
{
25
26 5
    public function __construct(
27
        private ShipSystemManagerInterface $shipSystemManager,
28
        private ColonyLibFactoryInterface $colonyLibFactory,
29
        private TorpedoTypeRepositoryInterface $torpedoTypeRepository,
30
        private GameControllerInterface $game,
31
        private ShipStateChangerInterface $shipStateChanger,
32
        private RepairUtilInterface $repairUtil,
33
        private UserRepositoryInterface $userRepository,
34
        private Parser $bbCodeParser,
35
        private SystemDataDeserializerInterface $systemDataDeserializer
36
    ) {
37 5
    }
38
39 3
    public function wrapShip(ShipInterface $ship): ShipWrapperInterface
40
    {
41 3
        return new ShipWrapper(
42 3
            $ship,
43 3
            $this->shipSystemManager,
44 3
            $this->systemDataDeserializer,
45 3
            $this->colonyLibFactory,
46 3
            $this->torpedoTypeRepository,
47 3
            $this->game,
48 3
            $this,
49 3
            $this->shipStateChanger,
50 3
            $this->repairUtil,
51 3
            $this->bbCodeParser
52 3
        );
53
    }
54
55 3
    public function wrapShips(array $ships): Collection
56
    {
57 3
        $result = new ArrayCollection();
58
59 3
        foreach ($ships as $key => $ship) {
60 3
            $result->set($key, $this->wrapShip($ship));
61
        }
62
63 3
        return $result;
64
    }
65
66 2
    public function wrapShipsAsFleet(array $ships, bool $isSingleShips = false): FleetWrapperInterface
67
    {
68 2
        if (empty($ships)) {
69
            throw new InvalidArgumentException('ship array should not be empty');
70
        }
71
72 2
        $fleet = new Fleet();
73 2
        foreach ($ships as $key => $value) {
74 2
            $fleet->getShips()->set($key, $value);
75
        }
76
77 2
        if ($isSingleShips) {
78 1
            $fleet->setName(_('Einzelschiffe'));
79 1
            $fleet->setUser($this->userRepository->getFallbackUser());
80 1
            $fleet->setSort(PHP_INT_MAX);
81
        } else {
82 1
            $originalFleet = current($ships)->getFleet();
83 1
            if ($originalFleet === null) {
84
                throw new RuntimeException('ship should have fleet');
85
            }
86
87 1
            $fleet->setName($originalFleet->getName());
88 1
            $fleet->setUser(current($ships)->getUser());
89 1
            $fleet->setSort($originalFleet->getSort());
90
        }
91
92 2
        return new FleetWrapper($fleet, $this, $this->game, $isSingleShips);
93
    }
94
95
    public function wrapFleet(FleetInterface $fleet): FleetWrapperInterface
96
    {
97
        return new FleetWrapper($fleet, $this, $this->game, false);
98
    }
99
100
    public function wrapFleets(array $fleets): array
101
    {
102
        return array_map(
103
            fn (FleetInterface $fleet): FleetWrapperInterface => $this->wrapFleet($fleet),
104
            $fleets
105
        );
106
    }
107
}
108