Passed
Pull Request — dev (#2038)
by Janko
10:55
created

triggerSpecific()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 69
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 12.0028

Importance

Changes 0
Metric Value
cc 12
eloc 39
nc 12
nop 3
dl 0
loc 69
ccs 36
cts 37
cp 0.973
crap 12.0028
rs 6.9666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Spacecraft\Lib\Movement\Component\Consequence\PostFlight;
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\Crew\Skill\CrewEnhancemenProxy;
9
use Stu\Component\Crew\Skill\SkillEnhancementEnum;
10
use Stu\Component\Ship\AstronomicalMappingEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Ship\AstronomicalMappingEnum 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...
11
use Stu\Component\Spacecraft\SpacecraftStateEnum;
12
use Stu\Module\Prestige\Lib\CreatePrestigeLogInterface;
13
use Stu\Module\Ship\Lib\AstroEntryLibInterface;
14
use Stu\Module\Spacecraft\Lib\Message\MessageCollectionInterface;
15
use Stu\Module\Spacecraft\Lib\Message\MessageFactoryInterface;
16
use Stu\Module\Spacecraft\Lib\Message\MessageInterface;
17
use Stu\Module\Spacecraft\Lib\Movement\Component\Consequence\AbstractFlightConsequence;
18
use Stu\Module\Spacecraft\Lib\Movement\Route\FlightRouteInterface;
19
use Stu\Module\Ship\Lib\ShipWrapperInterface;
20
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
21
use Stu\Orm\Entity\ShipInterface;
22
use Stu\Orm\Repository\AstroEntryRepositoryInterface;
23
24
class PostFlightAstroMappingConsequence extends AbstractFlightConsequence implements PostFlightConsequenceInterface
25
{
26 8
    public function __construct(
27
        private AstroEntryRepositoryInterface $astroEntryRepository,
28
        private AstroEntryLibInterface $astroEntryLib,
29
        private CreatePrestigeLogInterface $createPrestigeLog,
30
        private MessageFactoryInterface $messageFactory
31 8
    ) {}
32
33 6
    #[Override]
34
    protected function skipWhenTractored(): bool
35
    {
36 6
        return false;
37
    }
38
39 6
    #[Override]
40
    protected function triggerSpecific(
41
        SpacecraftWrapperInterface $wrapper,
42
        FlightRouteInterface $flightRoute,
43
        MessageCollectionInterface $messages
44
    ): void {
45
46 6
        if (!$wrapper instanceof ShipWrapperInterface) {
47
            return;
48
        }
49
50 6
        $ship = $wrapper->get();
51
52 6
        if (!$ship->getAstroState()) {
53 1
            return;
54
        }
55
56 5
        if ($ship->getSystem() === null && $ship->getMapRegion() === null) {
57 1
            return;
58
        }
59
60 4
        $astroEntry = $this->astroEntryLib->getAstroEntryByShipLocation($ship, false);
61 4
        if ($astroEntry === null) {
62 1
            return;
63
        }
64
65 3
        $fieldId = $ship->getLocation()->getId();
66
67 3
        $message = $this->messageFactory->createMessage(null, $ship->getUser()->getId());
68 3
        $messages->add($message);
69
70 3
        if ($astroEntry->getState() === AstronomicalMappingEnum::PLANNED) {
71
            /** @var array<int> */
72 2
            $idsToMap = unserialize($astroEntry->getFieldIds());
73
74 2
            $key = array_search($fieldId, $idsToMap);
75
76 2
            if (is_int($key)) {
77 2
                unset($idsToMap[$key]);
78
79 2
                if (empty($idsToMap)) {
80 1
                    $astroEntry->setFieldIds('');
81 1
                    $astroEntry->setState(AstronomicalMappingEnum::MEASURED);
82 1
                    $message->add(sprintf(_('Die %s hat alle Kartographierungs-Messpunkte erreicht'), $ship->getName()));
83
                } else {
84 1
                    $astroEntry->setFieldIds(serialize($idsToMap));
85 1
                    $this->addReachedWaypointInfo($message, $ship);
86
                }
87
88 2
                $this->createPrestigeLog($ship);
89 2
                CrewEnhancemenProxy::addExpertise($ship, SkillEnhancementEnum::REACH_ASTRO_WAYPOINT);
90
            }
91
        }
92
93 3
        $astroLab = $wrapper->getAstroLaboratorySystemData();
94
95
        if (
96 3
            $ship->getState() === SpacecraftStateEnum::ASTRO_FINALIZING
97 3
            && $astroEntry->getState() === AstronomicalMappingEnum::FINISHING
98 3
            && $astroLab !== null
99
        ) {
100 1
            $ship->getCondition()->setState(SpacecraftStateEnum::NONE);
101 1
            $astroLab->setAstroStartTurn(null)->update();
102 1
            $astroEntry->setState(AstronomicalMappingEnum::MEASURED);
103 1
            $astroEntry->setAstroStartTurn(null);
104 1
            $message->add(sprintf(_('Die %s hat das Finalisieren der Kartographierung abgebrochen'), $ship->getName()));
105
        }
106
107 3
        $this->astroEntryRepository->save($astroEntry);
108
    }
109
110 2
    private function createPrestigeLog(ShipInterface $ship): void
111
    {
112 2
        $this->createPrestigeLog->createLog(
113 2
            1,
114 2
            sprintf('1 Prestige erhalten für Kartographierungs-Messpunkt "%s"', $ship->getSectorString()),
115 2
            $ship->getUser(),
116 2
            time()
117 2
        );
118
    }
119
120 1
    private function addReachedWaypointInfo(MessageInterface $message, ShipInterface $ship): void
121
    {
122 1
        $message->add(sprintf(
123 1
            _('Die %s hat einen Kartographierungs-Messpunkt erreicht: %s'),
124 1
            $ship->getName(),
125 1
            $ship->getSectorString()
126 1
        ));
127
    }
128
}
129