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

AstroEntryLib::finish()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 1
dl 0
loc 23
ccs 0
cts 14
cp 0
crap 12
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib;
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 RuntimeException;
9
use Stu\Component\Crew\Skill\CrewEnhancemenProxy;
10
use Stu\Component\Crew\Skill\SkillEnhancementEnum;
11
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...
12
use Stu\Component\Spacecraft\SpacecraftStateEnum;
13
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
14
use Stu\Orm\Entity\AstronomicalEntryInterface;
15
use Stu\Orm\Entity\SpacecraftInterface;
16
use Stu\Orm\Entity\UserInterface;
17
use Stu\Orm\Repository\AstroEntryRepositoryInterface;
18
19
final class AstroEntryLib implements AstroEntryLibInterface
20
{
21 2
    public function __construct(private AstroEntryRepositoryInterface $astroEntryRepository) {}
22
23
    #[Override]
24
    public function cancelAstroFinalizing(SpacecraftWrapperInterface $wrapper): void
25
    {
26
        if (!$wrapper instanceof ShipWrapperInterface) {
27
            return;
28
        }
29
30
        $ship = $wrapper->get();
31
32
        $ship->getCondition()->setState(SpacecraftStateEnum::NONE);
33
        $astroLab = $wrapper->getAstroLaboratorySystemData();
34
        if ($astroLab === null) {
35
            throw new RuntimeException('this should not happen');
36
        }
37
        $astroLab->setAstroStartTurn(null)->update();
38
39
        $entry = $this->getAstroEntryByShipLocation($ship, false);
40
        if ($entry === null) {
41
            throw new RuntimeException('this should not happen');
42
        }
43
44
        $entry->setState(AstronomicalMappingEnum::MEASURED);
45
        $entry->setAstroStartTurn(null);
46
        $this->astroEntryRepository->save($entry);
47
    }
48
49
    #[Override]
50
    public function finish(ShipWrapperInterface $wrapper): void
51
    {
52
        $ship = $wrapper->get();
53
54
        $ship->getCondition()->setState(SpacecraftStateEnum::NONE);
55
56
        $astroLab = $wrapper->getAstroLaboratorySystemData();
57
        if ($astroLab === null) {
58
            throw new RuntimeException('this should not happen');
59
        }
60
        $astroLab->setAstroStartTurn(null)->update();
61
62
        $entry = $this->getAstroEntryByShipLocation($ship, false);
63
        if ($entry === null) {
64
            throw new RuntimeException('this should not happen');
65
        }
66
67
        $entry->setState(AstronomicalMappingEnum::DONE);
68
        $entry->setAstroStartTurn(null);
69
        $this->astroEntryRepository->save($entry);
70
71
        CrewEnhancemenProxy::addExpertise($ship, SkillEnhancementEnum::FINISH_ASTRO_MAPPING);
72
    }
73
74 1
    #[Override]
75
    public function getAstroEntryByShipLocation(SpacecraftInterface $spacecraft, bool $showOverSystem = true): ?AstronomicalEntryInterface
76
    {
77 1
        $user = $spacecraft->getUser();
78 1
        $system = $spacecraft->getSystem();
79
80 1
        if ($system !== null) {
81
            return $this->getAstroEntryForUser($system, $user);
82
        }
83
84 1
        $overSystem = $spacecraft->isOverSystem();
85 1
        if ($overSystem !== null && $showOverSystem) {
86 1
            return $this->getAstroEntryForUser($overSystem, $user);
87
        }
88
89 1
        $mapRegion = $spacecraft->getMapRegion();
90 1
        if ($mapRegion !== null) {
91 1
            return $this->getAstroEntryForUser($mapRegion, $user);
92
        }
93
94
        return null;
95
    }
96
97 1
    private function getAstroEntryForUser(EntityWithAstroEntryInterface $entity, UserInterface $user): ?AstronomicalEntryInterface
98
    {
99 1
        return $entity->getAstronomicalEntries()->get($user->getId());
100
    }
101
}
102