Passed
Push — master ( ad7b81...1c3353 )
by Janko
35:52
created

AstroEntryLib::cancelAstroFinalizing()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
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 20
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\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...
10
use Stu\Component\Ship\ShipStateEnum;
11
use Stu\Orm\Entity\AstronomicalEntryInterface;
12
use Stu\Orm\Entity\ShipInterface;
13
use Stu\Orm\Entity\UserInterface;
14
use Stu\Orm\Repository\AstroEntryRepositoryInterface;
15
16
final class AstroEntryLib implements AstroEntryLibInterface
17
{
18 4
    public function __construct(private AstroEntryRepositoryInterface $astroEntryRepository) {}
19
20
    #[Override]
21
    public function cancelAstroFinalizing(ShipWrapperInterface $wrapper): void
22
    {
23
        $ship = $wrapper->get();
24
25
        $ship->setState(ShipStateEnum::SHIP_STATE_NONE);
26
        $astroLab = $wrapper->getAstroLaboratorySystemData();
27
        if ($astroLab === null) {
28
            throw new RuntimeException('this should not happen');
29
        }
30
        $astroLab->setAstroStartTurn(null)->update();
31
32
        $entry = $this->getAstroEntryByShipLocation($ship, false);
33
        if ($entry === null) {
34
            throw new RuntimeException('this should not happen');
35
        }
36
37
        $entry->setState(AstronomicalMappingEnum::MEASURED);
38
        $entry->setAstroStartTurn(null);
39
        $this->astroEntryRepository->save($entry);
40
    }
41
42
    #[Override]
43
    public function finish(ShipWrapperInterface $wrapper): void
44
    {
45
        $ship = $wrapper->get();
46
47
        $ship->setState(ShipStateEnum::SHIP_STATE_NONE);
48
49
        $astroLab = $wrapper->getAstroLaboratorySystemData();
50
        if ($astroLab === null) {
51
            throw new RuntimeException('this should not happen');
52
        }
53
        $astroLab->setAstroStartTurn(null)->update();
54
55
        $entry = $this->getAstroEntryByShipLocation($ship, false);
56
        if ($entry === null) {
57
            throw new RuntimeException('this should not happen');
58
        }
59
60
        $entry->setState(AstronomicalMappingEnum::DONE);
61
        $entry->setAstroStartTurn(null);
62
        $this->astroEntryRepository->save($entry);
63
    }
64
65
    #[Override]
66
    public function getAstroEntryByShipLocation(ShipInterface $ship, bool $showOverSystem = true): ?AstronomicalEntryInterface
67
    {
68
        $user = $ship->getUser();
69
        $system = $ship->getSystem();
70
71
        if ($system !== null) {
72
            return $this->getAstroEntryForUser($system, $user);
73
        }
74
75
        $overSystem = $ship->isOverSystem();
76
        if ($overSystem !== null && $showOverSystem) {
77
            return $this->getAstroEntryForUser($overSystem, $user);
78
        }
79
80
        $mapRegion = $ship->getMapRegion();
81
        if ($mapRegion !== null) {
82
            return $this->getAstroEntryForUser($mapRegion, $user);
83
        }
84
85
        return null;
86
    }
87
88
    private function getAstroEntryForUser(EntityWithAstroEntryInterface $entity, UserInterface $user): ?AstronomicalEntryInterface
89
    {
90
        return $entity->getAstronomicalEntries()->get($user->getId());
91
    }
92
}
93