Passed
Pull Request — master (#1819)
by Nico
52:14 queued 25:19
created

ShipWrapperFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 81.39%

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 82
ccs 35
cts 43
cp 0.8139
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A wrapShipsAsFleet() 0 27 5
A wrapFleet() 0 3 1
A wrapFleets() 0 5 1
A wrapShip() 0 13 1
A wrapShips() 0 9 2
A __construct() 0 11 1
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