Passed
Push — master ( 1dedb1...8ec02d )
by Nico
14:34
created

SpacecraftDeletionHandler   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 97.22%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 31
dl 0
loc 77
rs 10
c 1
b 1
f 0
ccs 35
cts 36
cp 0.9722
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A delete() 0 23 6
A deleteConstructionProgress() 0 6 2
A unsetTractor() 0 12 2
A deleteMiningQueue() 0 5 2
A undockAllDockedShips() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Player\Deletion\Handler;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
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...
9
use Stu\Component\Spacecraft\System\SpacecraftSystemManagerInterface;
10
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
11
use Stu\Module\Spacecraft\Lib\Interaction\ShipUndockingInterface;
12
use Stu\Module\Spacecraft\Lib\SpacecraftRemoverInterface;
13
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperFactoryInterface;
14
use Stu\Orm\Entity\ShipInterface;
15
use Stu\Orm\Entity\SpacecraftInterface;
16
use Stu\Orm\Entity\StationInterface;
17
use Stu\Orm\Entity\UserInterface;
18
use Stu\Orm\Repository\ConstructionProgressRepositoryInterface;
19
use Stu\Orm\Repository\SpacecraftRepositoryInterface;
20
use Stu\Orm\Repository\MiningQueueRepositoryInterface;
21
22
final class SpacecraftDeletionHandler implements PlayerDeletionHandlerInterface
23
{
24 2
    public function __construct(
25
        private SpacecraftRepositoryInterface $spacecraftRepository,
26
        private ConstructionProgressRepositoryInterface $constructionProgressRepository,
27
        private SpacecraftRemoverInterface $spacecraftRemover,
28
        private SpacecraftSystemManagerInterface $spacecraftSystemManager,
29
        private SpacecraftWrapperFactoryInterface $spacecraftWrapperFactory,
30
        private ShipUndockingInterface $shipUndocking,
31
        private EntityManagerInterface $entityManager,
32
        private MiningQueueRepositoryInterface $miningQueueRepository
33 2
    ) {}
34
35 1
    #[Override]
36
    public function delete(UserInterface $user): void
37
    {
38 1
        foreach ($this->spacecraftRepository->getByUser($user) as $spacecraft) {
39
40
            // do nothing if tradepost, because it gets handled in TradepostDeletionHandler
41
            if (
42 1
                $spacecraft instanceof StationInterface
43 1
                && $spacecraft->getTradePost() !== null
44
            ) {
45 1
                continue;
46
            }
47
48 1
            if ($spacecraft instanceof StationInterface) {
49 1
                $this->deleteConstructionProgress($spacecraft);
50 1
                $this->undockAllDockedShips($spacecraft);
51
            }
52
53 1
            $this->unsetTractor($spacecraft);
54 1
            if ($spacecraft instanceof ShipInterface) {
55 1
                $this->deleteMiningQueue($spacecraft);
56
            }
57 1
            $this->spacecraftRemover->remove($spacecraft, true);
58
        }
59
    }
60
61 1
    private function deleteConstructionProgress(StationInterface $station): void
62
    {
63 1
        $progress = $station->getConstructionProgress();
64 1
        if ($progress !== null) {
65 1
            $this->constructionProgressRepository->delete($progress);
66 1
            $station->resetConstructionProgress();
67
        }
68
    }
69
70 1
    private function undockAllDockedShips(StationInterface $station): void
71
    {
72 1
        $anyDocked = $this->shipUndocking->undockAllDocked($station);
73
74 1
        if ($anyDocked) {
75 1
            $this->entityManager->flush();
76
        }
77
    }
78
79 1
    private function unsetTractor(SpacecraftInterface $spacecraft): void
80
    {
81 1
        $tractoredShip = $spacecraft->getTractoredShip();
82
83 1
        if ($tractoredShip === null) {
84 1
            return;
85
        }
86
87 1
        $this->spacecraftSystemManager->deactivate(
88 1
            $this->spacecraftWrapperFactory->wrapSpacecraft($spacecraft),
89 1
            SpacecraftSystemTypeEnum::TRACTOR_BEAM,
90 1
            true
91 1
        );
92
    }
93
94 1
    private function deleteMiningQueue(ShipInterface $spacecraft): void
95
    {
96 1
        $miningqueue = $spacecraft->getMiningQueue();
97 1
        if ($miningqueue !== null) {
98
            $this->miningQueueRepository->delete($miningqueue);
99
        }
100
    }
101
}
102