Passed
Push — master ( bf860f...1dd8f7 )
by Nico
57:55 queued 29:36
created

ShipRemover::orphanizeStorage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib;
6
7
use Stu\Component\Ship\ShipStateEnum;
8
use Stu\Component\Ship\System\ShipSystemManagerInterface;
9
use Stu\Component\Ship\System\ShipSystemTypeEnum;
10
use Stu\Module\Ship\Lib\Auxiliary\ShipShutdownInterface;
11
use Stu\Module\Ship\Lib\Torpedo\ClearTorpedoInterface;
12
use Stu\Orm\Entity\ShipInterface;
13
use Stu\Orm\Repository\CrewRepositoryInterface;
14
use Stu\Orm\Repository\ShipCrewRepositoryInterface;
15
use Stu\Orm\Repository\ShipRepositoryInterface;
16
use Stu\Orm\Repository\ShipSystemRepositoryInterface;
17
use Stu\Orm\Repository\StorageRepositoryInterface;
18
19
final class ShipRemover implements ShipRemoverInterface
20
{
21
    public function __construct(
22
        private ShipSystemRepositoryInterface $shipSystemRepository,
23
        private StorageRepositoryInterface $storageRepository,
24
        private CrewRepositoryInterface $crewRepository,
25
        private ShipCrewRepositoryInterface $shipCrewRepository,
26
        private ShipRepositoryInterface $shipRepository,
27
        private ShipSystemManagerInterface $shipSystemManager,
28
        private ClearTorpedoInterface $clearTorpedo,
29
        private ShipStateChangerInterface $shipStateChanger,
30
        private ShipWrapperFactoryInterface $shipWrapperFactory,
31
        private ShipShutdownInterface $shipShutdown,
32
    ) {
33
    }
34
35
    private function resetTrackerDevices(int $shipId): void
36
    {
37
        foreach ($this->shipSystemRepository->getTrackingShipSystems($shipId) as $system) {
38
            $wrapper = $this->shipWrapperFactory->wrapShip($system->getShip());
39
40
            $this->shipSystemManager->deactivate($wrapper, ShipSystemTypeEnum::SYSTEM_TRACKER, true);
41
        }
42
    }
43
44
    public function remove(ShipInterface $ship, ?bool $truncateCrew = false): void
45
    {
46
        $wrapper = $this->shipWrapperFactory->wrapShip($ship);
47
48
        $this->shipShutdown->shutdown($wrapper, true);
49
        $this->shipStateChanger->changeShipState($wrapper, ShipStateEnum::SHIP_STATE_NONE);
50
51
        //both sides have to be cleared, foreign key violation
52
        if ($ship->isTractoring()) {
53
            $this->shipSystemManager->deactivate($wrapper, ShipSystemTypeEnum::SYSTEM_TRACTOR_BEAM, true);
54
        } else {
55
            $tractoringShipWrapper = $wrapper->getTractoringShipWrapper();
56
            if ($tractoringShipWrapper !== null) {
57
                $this->shipSystemManager->deactivate($tractoringShipWrapper, ShipSystemTypeEnum::SYSTEM_TRACTOR_BEAM, true);
58
            }
59
        }
60
61
        foreach ($ship->getStorage() as $item) {
62
            $this->storageRepository->delete($item);
63
        }
64
65
        // delete torpedo storage
66
        $this->clearTorpedo->clearTorpedoStorage($wrapper);
67
68
        if ($truncateCrew) {
69
            $crewArray = [];
70
            foreach ($ship->getCrewAssignments() as $shipCrew) {
71
                $crewArray[] = $shipCrew->getCrew();
72
            }
73
74
            $this->shipCrewRepository->truncateByShip($ship->getId());
75
76
            foreach ($crewArray as $crew) {
77
                $this->crewRepository->delete($crew);
78
            }
79
80
            $ship->getCrewAssignments()->clear();
81
        }
82
83
        // reset tracker devices
84
        $this->resetTrackerDevices($ship->getId());
85
86
        foreach ($ship->getSystems() as $shipSystem) {
87
            $this->shipSystemRepository->delete($shipSystem);
88
        }
89
90
        $this->shipRepository->delete($ship);
91
    }
92
}
93