Passed
Push — master ( e41cdd...5a4400 )
by Nico
20:21 queued 14:48
created

CancelRetrofit   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 11.76%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 48
c 1
b 0
f 0
dl 0
loc 90
ccs 6
cts 51
cp 0.1176
rs 10
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setStateNoneAndSave() 0 4 1
B cancelRetrofit() 0 74 11
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Ship\Retrofit;
6
7
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...
8
use Stu\Component\Spacecraft\SpacecraftStateEnum;
9
use Stu\Component\Spacecraft\SpacecraftModuleTypeEnum;
10
use Stu\Lib\Transfer\Storage\StorageManagerInterface;
11
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum;
12
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
13
use Stu\Module\PlayerSetting\Lib\UserConstants;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserConstants 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...
14
use Stu\Orm\Entity\Module;
15
use Stu\Orm\Entity\Ship;
16
use Stu\Orm\Repository\ColonyShipQueueRepositoryInterface;
17
use Stu\Orm\Repository\ShipRepositoryInterface;
18
19
20
21
22
final class CancelRetrofit implements CancelRetrofitInterface
23
{
24 2
    public function __construct(
25
        private ShipRepositoryInterface $shipRepository,
26
        private ColonyShipQueueRepositoryInterface $colonyShipQueueRepository,
27
        private readonly StorageManagerInterface $storageManager,
28
        private readonly PrivateMessageSenderInterface $privateMessageSender
29 2
    ) {}
30
31
32 8
    #[Override]
33
    public function cancelRetrofit(Ship $ship): bool
34
    {
35 8
        $state = $ship->getState();
36 8
        if ($state === SpacecraftStateEnum::RETROFIT) {
37
            $queueEntry = $this->colonyShipQueueRepository->getByShip($ship->getId());
38
            if ($queueEntry === null) {
39
                $this->setStateNoneAndSave($ship);
40
                return true;
41
            }
42
43
            $colony = $queueEntry->getColony();
44
            $newBuildplan = $queueEntry->getSpacecraftBuildplan();
45
            $oldBuildplan = $ship->getBuildplan();
46
            $returnedmodules = [];
47
48
            $buildTime = $queueEntry->getBuildtime();
49
            $finishDate = $queueEntry->getFinishDate();
50
            $startDate = $finishDate - $buildTime;
51
            $currentTime = time();
52
            $firstQuarter = $startDate + ($buildTime * 0.25);
53
54
            $withinFirstQuarter = $currentTime <= $firstQuarter;
55
56
57
            if ($oldBuildplan != null && $newBuildplan != null) {
58
                foreach (SpacecraftModuleTypeEnum::getModuleSelectorOrder() as $moduleType) {
59
                    $oldModules = $oldBuildplan->getModulesByType($moduleType)->toArray();
60
                    $newModules = $newBuildplan->getModulesByType($moduleType)->toArray();
61
62
                    /** @var array<Module> */
63
                    $addingModules = array_udiff($newModules, $oldModules, function (Module $a, Module $b): int {
64
                        return $a->getId() - $b->getId();
65
                    });
66
67
                    if ($withinFirstQuarter) {
68
                        foreach ($addingModules as $module) {
69
                            if ($module->getType() != SpacecraftModuleTypeEnum::HULL) {
70
                                $returnedmodules[] = $module;
71
                            }
72
                        }
73
                    }
74
                }
75
            }
76
77
            $txt = _("Auf der Kolonie " . $colony->getName() . " wurde die Umrüstung der " . $ship->getName() . " abgebrochen");
78
79
            if ($returnedmodules !== []) {
80
                $msg = "\n\nDie folgenden Module wurden beim Abbruch der Umrüstung zurückgewonnen: \n";
81
82
                foreach ($returnedmodules as $module) {
83
                    $this->storageManager->upperStorage($colony, $module->getCommodity(), 1);
84
                    $msg .= "- " . $module->getName() . "\n";
85
                }
86
87
                $txt .= $msg;
88
            } else {
89
                $txt .= "\n\nEs konnten keine Module zurückgewonnen werden, da der Umrüstungsprozess bereits zu weit fortgeschritten war";
90
            }
91
92
            $this->privateMessageSender->send(
93
                UserConstants::USER_NOONE,
94
                $colony->getUserId(),
95
                $txt,
96
                PrivateMessageFolderTypeEnum::SPECIAL_COLONY
97
            );
98
99
            $this->setStateNoneAndSave($ship);
100
            $this->colonyShipQueueRepository->truncateByShip($ship->getId());
101
102
            return true;
103
        }
104
105 8
        return false;
106
    }
107
108
    private function setStateNoneAndSave(Ship $ship): void
109
    {
110
        $ship->getCondition()->setState(SpacecraftStateEnum::NONE);
111
        $this->shipRepository->save($ship);
112
    }
113
}