Passed
Push — dev ( 5b1d95...923b08 )
by Janko
23:33 queued 07:31
created

FinishedAstroMappingHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Stu\Module\Tick\Spacecraft\Handler;
4
5
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...
6
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...
7
use Stu\Component\Spacecraft\SpacecraftStateEnum;
8
use Stu\Lib\Information\InformationInterface;
9
use Stu\Module\Control\GameControllerInterface;
10
use Stu\Module\Database\Lib\CreateDatabaseEntryInterface;
11
use Stu\Module\Ship\Lib\AstroEntryLibInterface;
12
use Stu\Module\Ship\Lib\ShipWrapperInterface;
13
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
14
use Stu\Orm\Entity\DatabaseEntryInterface;
15
use Stu\Orm\Entity\ShipInterface;
16
use Stu\Orm\Repository\DatabaseUserRepositoryInterface;
17
18
class FinishedAstroMappingHandler implements SpacecraftTickHandlerInterface
19
{
20 1
    public function __construct(
21
        private AstroEntryLibInterface $astroEntryLib,
22
        private DatabaseUserRepositoryInterface $databaseUserRepository,
23
        private CreateDatabaseEntryInterface $createDatabaseEntry,
24
        private GameControllerInterface $game
25 1
    ) {}
26
27
    #[Override]
28
    public function handleSpacecraftTick(
29
        SpacecraftWrapperInterface $wrapper,
30
        InformationInterface $information
31
    ): void {
32
33
        if (!$wrapper instanceof ShipWrapperInterface) {
34
            return;
35
        }
36
37
        $ship = $wrapper->get();
38
39
        /** @var null|DatabaseEntryInterface $databaseEntry */
40
        /** @var string $message */
41
        [$message, $databaseEntry] = $this->getDatabaseEntryForShipLocation($ship);
42
43
        $astroLab = $wrapper->getAstroLaboratorySystemData();
44
45
        if (
46
            $ship->getState() === SpacecraftStateEnum::ASTRO_FINALIZING
47
            && $databaseEntry !== null
48
            && $astroLab !== null
49
            && $this->game->getCurrentRound()->getTurn() >= ($astroLab->getAstroStartTurn() + AstronomicalMappingEnum::TURNS_TO_FINISH)
50
        ) {
51
52
            $this->astroEntryLib->finish($wrapper);
53
54
            $information->addInformationf(
55
                'Die Kartographierung %s wurde vollendet',
56
                $message
57
            );
58
59
            $userId = $ship->getUser()->getId();
60
            $databaseEntryId = $databaseEntry->getId();
61
62
            if (!$this->databaseUserRepository->exists($userId, $databaseEntryId)) {
63
64
                $entry = $this->createDatabaseEntry->createDatabaseEntryForUser($ship->getUser(), $databaseEntryId);
65
66
                if ($entry !== null) {
67
                    $information->addInformationf(
68
                        'Neuer Datenbankeintrag: %s (+%d Punkte)',
69
                        $entry->getDescription(),
70
                        $entry->getCategory()->getPoints()
71
                    );
72
                }
73
            }
74
        }
75
    }
76
77
    /**
78
     * @return array{0: string|null, 1: DatabaseEntryInterface|null}
79
     */
80
    private function getDatabaseEntryForShipLocation(ShipInterface $ship): array
81
    {
82
        $system = $ship->getSystem();
83
        if ($system !== null) {
84
            return [
85
                'des Systems ' . $system->getName(),
86
                $system->getDatabaseEntry()
87
            ];
88
        }
89
90
        $mapRegion = $ship->getMapRegion();
91
        if ($mapRegion !== null) {
92
            return [
93
                'der Region ' . $mapRegion->getDescription(),
94
                $mapRegion->getDatabaseEntry()
95
            ];
96
        }
97
98
        return [null, null];
99
    }
100
}
101