Passed
Push — dev ( 91186a...20d5e2 )
by Janko
05:17
created

getDatabaseEntryForShipLocation()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 2
dl 0
loc 17
ccs 9
cts 10
cp 0.9
crap 4.016
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\View\ShowShip;
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\Ship\AstronomicalMappingStateEnum;
9
use Stu\Module\Control\GameControllerInterface;
10
use Stu\Component\Game\ModuleEnum;
11
use Stu\Component\Spacecraft\System\Type\AstroLaboratoryShipSystem;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Spacecraft...troLaboratoryShipSystem 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...
12
use Stu\Module\Control\ViewContext;
13
use Stu\Module\Ship\Lib\AstroEntryLibInterface;
14
use Stu\Module\Ship\Lib\ShipLoaderInterface;
15
use Stu\Module\Spacecraft\View\ShowSpacecraft\ShowSpacecraft;
0 ignored issues
show
Bug introduced by
The type Stu\Module\Spacecraft\Vi...acecraft\ShowSpacecraft 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...
16
use Stu\Module\Spacecraft\View\ShowSpacecraft\SpacecraftTypeShowStragegyInterface;
17
use Stu\Orm\Entity\DatabaseEntry;
18
use Stu\Orm\Entity\Ship;
19
use Stu\Orm\Repository\DatabaseUserRepositoryInterface;
20
21
class ShipShowStrategy implements SpacecraftTypeShowStragegyInterface
22
{
23 1
    public function __construct(
24
        private ShipLoaderInterface $shipLoader,
25
        private DatabaseUserRepositoryInterface $databaseUserRepository,
26
        private AstroEntryLibInterface $astroEntryLib,
27 1
    ) {}
28
29 2
    #[Override]
30
    public function appendNavigationPart(GameControllerInterface $game): SpacecraftTypeShowStragegyInterface
31
    {
32 2
        $game->appendNavigationPart('ship.php',  _('Schiffe'));
33
34 2
        return $this;
35
    }
36
37 2
    #[Override]
38
    public function setTemplateVariables(int $spacecraftId, GameControllerInterface $game): SpacecraftTypeShowStragegyInterface
39
    {
40 2
        $ship = $this->shipLoader->getByIdAndUser($spacecraftId, $game->getUser()->getId(), true, false);
41 2
        $game->setTemplateVar('ASTRO_STATE_SYSTEM', $this->getAstroState($ship, $game, true));
42 2
        $game->setTemplateVar('ASTRO_STATE_REGION', $this->getAstroState($ship, $game, false));
43
44 2
        return $this;
45
    }
46
47 2
    private function getAstroState(Ship $ship, GameControllerInterface $game, bool $isSystem): AstroStateWrapper
48
    {
49
        //$this->loggerUtil->init('SS', LogLevelEnum::ERROR);
50
51 2
        $databaseEntry = $this->getDatabaseEntryForShipLocation($ship, $isSystem);
52
53 2
        $astroEntry = null;
54
55 2
        if ($databaseEntry === null) {
56 1
            $state = AstronomicalMappingStateEnum::NONE;
57 2
        } elseif ($this->databaseUserRepository->exists($game->getUser()->getId(), $databaseEntry->getId())) {
58
            $state = AstronomicalMappingStateEnum::DONE;
59
        } else {
60 2
            $astroEntry = $this->astroEntryLib->getAstroEntryByShipLocation($ship, $isSystem);
61
62 2
            $state = $astroEntry === null ? AstronomicalMappingStateEnum::PLANNABLE : $astroEntry->getState();
63
        }
64 2
        $turnsLeft = null;
65 2
        if ($state === AstronomicalMappingStateEnum::FINISHING && $astroEntry !== null) {
66
            $turnsLeft = AstroLaboratoryShipSystem::TURNS_TO_FINISH - ($game->getCurrentRound()->getTurn() - $astroEntry->getAstroStartTurn());
67
        }
68 2
        $measurementpointsleft = null;
69 2
        if ($state === AstronomicalMappingStateEnum::PLANNED && $astroEntry !== null) {
70
            $fieldIds = unserialize($astroEntry->getFieldIds());
71
            $measurementpointsleft = is_array($fieldIds) ? count($fieldIds) : 0;
72
        }
73
74 2
        return new AstroStateWrapper($state, $turnsLeft, $isSystem, $measurementpointsleft);
75
    }
76
77 2
    private function getDatabaseEntryForShipLocation(Ship $ship, bool $isSystem): ?DatabaseEntry
78
    {
79 2
        if ($isSystem) {
80 2
            $system = $ship->getSystem() ?? $ship->isOverSystem();
81 2
            if ($system !== null) {
82 2
                return $system->getDatabaseEntry();
83
            }
84
85
            return null;
86
        }
87
88 2
        $mapRegion = $ship->getMapRegion();
89 2
        if ($mapRegion !== null) {
90 1
            return $mapRegion->getDatabaseEntry();
91
        }
92
93 1
        return null;
94
    }
95
96 2
    #[Override]
97
    public function getViewContext(): ViewContext
98
    {
99 2
        return new ViewContext(ModuleEnum::SHIP, ShowSpacecraft::VIEW_IDENTIFIER);
100
    }
101
}
102