Passed
Pull Request — master (#2040)
by Nico
20:26 queued 08:51
created

SpacecraftDeletionHandler::deleteMiningQueue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 3
c 1
b 1
f 0
nc 2
nop 1
dl 0
loc 5
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
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
        }
67
    }
68
69 1
    private function undockAllDockedShips(StationInterface $station): void
70
    {
71 1
        $anyDocked = $this->shipUndocking->undockAllDocked($station);
72
73 1
        if ($anyDocked) {
74 1
            $this->entityManager->flush();
75
        }
76
    }
77
78 1
    private function unsetTractor(SpacecraftInterface $spacecraft): void
79
    {
80 1
        $tractoredShip = $spacecraft->getTractoredShip();
81
82 1
        if ($tractoredShip === null) {
83 1
            return;
84
        }
85
86 1
        $this->spacecraftSystemManager->deactivate(
87 1
            $this->spacecraftWrapperFactory->wrapSpacecraft($spacecraft),
88 1
            SpacecraftSystemTypeEnum::TRACTOR_BEAM,
89 1
            true
90 1
        );
91
    }
92
93 1
    private function deleteMiningQueue(ShipInterface $spacecraft): void
94
    {
95 1
        $miningqueue = $spacecraft->getMiningQueue();
96 1
        if ($miningqueue !== null) {
97
            $this->miningQueueRepository->delete($miningqueue);
98
        }
99
    }
100
}