Passed
Push — master ( 288b46...98b0e3 )
by Nico
31:30 queued 08:00
created

PostFlightAstroMappingConsequence::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib\Movement\Component\Consequence\PostFlight;
6
7
use Stu\Component\Ship\AstronomicalMappingEnum;
8
use Stu\Component\Ship\ShipStateEnum;
9
use Stu\Module\Prestige\Lib\CreatePrestigeLogInterface;
10
use Stu\Module\Ship\Lib\Message\Message;
11
use Stu\Module\Ship\Lib\Message\MessageCollectionInterface;
12
use Stu\Module\Ship\Lib\Message\MessageInterface;
13
use Stu\Module\Ship\Lib\Movement\Component\Consequence\AbstractFlightConsequence;
14
use Stu\Module\Ship\Lib\Movement\Route\FlightRouteInterface;
15
use Stu\Module\Ship\Lib\ShipWrapperInterface;
16
use Stu\Orm\Entity\ShipInterface;
17
use Stu\Orm\Repository\AstroEntryRepositoryInterface;
18
19
class PostFlightAstroMappingConsequence extends AbstractFlightConsequence
20
{
21
    private AstroEntryRepositoryInterface $astroEntryRepository;
22
23
    private CreatePrestigeLogInterface $createPrestigeLog;
24
25 8
    public function __construct(
26
        AstroEntryRepositoryInterface $astroEntryRepository,
27
        CreatePrestigeLogInterface $createPrestigeLog
28
    ) {
29 8
        $this->astroEntryRepository = $astroEntryRepository;
30 8
        $this->createPrestigeLog = $createPrestigeLog;
31
    }
32
33 6
    protected function triggerSpecific(
34
        ShipWrapperInterface $wrapper,
35
        FlightRouteInterface $flightRoute,
36
        MessageCollectionInterface $messages
37
    ): void {
38
39 6
        $ship = $wrapper->get();
40
41 6
        if (!$ship->getAstroState()) {
42 1
            return;
43
        }
44
45 5
        if ($ship->getSystem() === null && $ship->getMapRegion() === null) {
46 1
            return;
47
        }
48
49 4
        $astroEntry = $this->astroEntryRepository->getByShipLocation($ship, false);
50 4
        if ($astroEntry === null) {
51 1
            return;
52
        }
53
54 3
        $fieldId = $ship->getCurrentMapField()->getId();
55
56 3
        $message = new Message(null, $ship->getUser()->getId());
57 3
        $messages->add($message);
58
59 3
        if ($astroEntry->getState() === AstronomicalMappingEnum::PLANNED) {
60
            /** @var array<int> */
61 2
            $idsToMap = unserialize($astroEntry->getFieldIds());
62
63 2
            $key = array_search($fieldId, $idsToMap);
64
65 2
            if (is_integer($key)) {
66 2
                unset($idsToMap[$key]);
67
68 2
                if (empty($idsToMap)) {
69 1
                    $astroEntry->setFieldIds('');
70 1
                    $astroEntry->setState(AstronomicalMappingEnum::MEASURED);
71 1
                    $message->add(sprintf(_('Die %s hat alle Kartographierungs-Messpunkte erreicht'), $ship->getName()));
72
                } else {
73 1
                    $astroEntry->setFieldIds(serialize($idsToMap));
74 1
                    $this->addReachedWaypointInfo($message, $ship);
75
                }
76
77 2
                $this->createPrestigeLog($ship);
78
            }
79
        }
80
81
        if (
82 3
            $ship->getState() === ShipStateEnum::SHIP_STATE_ASTRO_FINALIZING
83 3
            && $astroEntry->getState() === AstronomicalMappingEnum::FINISHING
84
        ) {
85 1
            $ship->setState(ShipStateEnum::SHIP_STATE_NONE);
86 1
            $ship->setAstroStartTurn(null);
87 1
            $astroEntry->setState(AstronomicalMappingEnum::MEASURED);
88 1
            $astroEntry->setAstroStartTurn(null);
89 1
            $message->add(sprintf(_('Die %s hat das Finalisieren der Kartographierung abgebrochen'), $ship->getName()));
90
        }
91
92 3
        $this->astroEntryRepository->save($astroEntry);
93
    }
94
95 2
    private function createPrestigeLog(ShipInterface $ship): void
96
    {
97 2
        $this->createPrestigeLog->createLog(
98 2
            1,
99 2
            sprintf('1 Prestige erhalten für Kartographierungs-Messpunkt "%s"', $ship->getSectorString()),
100 2
            $ship->getUser(),
101 2
            time()
102 2
        );
103
    }
104
105 1
    private function addReachedWaypointInfo(MessageInterface $message, ShipInterface $ship): void
106
    {
107 1
        $message->add(sprintf(
108 1
            _('Die %s hat einen Kartographierungs-Messpunkt erreicht: %s'),
109 1
            $ship->getName(),
110 1
            $ship->getSectorString()
111 1
        ));
112
    }
113
}
114