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

AstroEntryLib::finish()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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