Passed
Push — dev ( 80c162...225d4f )
by Janko
09:12
created

SpacecraftCreator::getSpacecraft()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Spacecraft\Lib\Creation;
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\Spacecraft\SpacecraftModuleTypeEnum;
10
use Stu\Component\Spacecraft\SpacecraftStateEnum;
11
use Stu\Module\Logging\LoggerUtilFactoryInterface;
12
use Stu\Module\Logging\LoggerUtilInterface;
13
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperFactoryInterface;
14
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
15
use Stu\Orm\Entity\SpacecraftInterface;
16
use Stu\Orm\Entity\SpacecraftRumpInterface;
17
use Stu\Orm\Repository\SpacecraftBuildplanRepositoryInterface;
18
use Stu\Orm\Repository\SpacecraftRumpRepositoryInterface;
19
use Stu\Orm\Repository\SpacecraftRepositoryInterface;
20
use Stu\Orm\Repository\UserRepositoryInterface;
21
22
/**
23
 * @template T of SpacecraftWrapperInterface
24
 * 
25
 * @implements SpacecraftCreatorInterface<T>
26
 */
27
final class SpacecraftCreator implements SpacecraftCreatorInterface
28
{
29
    private LoggerUtilInterface $loggerUtil;
30
31 1
    public function __construct(
32
        private SpacecraftRepositoryInterface $spacecraftRepository,
33
        private UserRepositoryInterface $userRepository,
34
        private SpacecraftRumpRepositoryInterface $spacecraftRumpRepository,
35
        private SpacecraftBuildplanRepositoryInterface $spacecraftBuildplanRepository,
36
        private SpacecraftSystemCreationInterface $spacecraftSystemCreation,
37
        private SpacecraftFactoryInterface $spacecraftFactory,
38
        private SpacecraftWrapperFactoryInterface $spacecraftWrapperFactory,
39
        private SpacecraftConfiguratorFactoryInterface $spacecraftConfiguratorFactory,
40
        LoggerUtilFactoryInterface $loggerUtilFactory
41
    ) {
42 1
        $this->loggerUtil = $loggerUtilFactory->getLoggerUtil();
43
    }
44
45
    #[Override]
46
    public function createBy(
47
        int $userId,
48
        int $rumpId,
49
        int $buildplanId,
50
        ?SpacecraftCreationConfigInterface $spacecraftCreationConfig
51
    ): SpacecraftConfiguratorInterface {
52
53
        $user = $this->userRepository->find($userId);
54
        if ($user === null) {
55
            throw new RuntimeException('user not existent');
56
        }
57
58
        $rump = $this->spacecraftRumpRepository->find($rumpId);
59
        if ($rump === null) {
60
            throw new RuntimeException('rump not existent');
61
        }
62
63
        $buildplan = $this->spacecraftBuildplanRepository->find($buildplanId);
64
        if ($buildplan === null) {
65
            throw new RuntimeException('buildplan not existent');
66
        }
67
68
        $spacecraft =  $this->getSpacecraft($rump, $spacecraftCreationConfig);
69
        $spacecraft->setUser($user);
70
        $spacecraft->setBuildplan($buildplan);
71
        $spacecraft->setRump($rump);
72
        $spacecraft->setState(SpacecraftStateEnum::SHIP_STATE_NONE);
73
74
        //create ship systems
75
        $this->spacecraftSystemCreation->createShipSystemsByModuleList(
76
            $spacecraft,
77
            $buildplan->getModules(),
78
            $spacecraftCreationConfig
79
        );
80
81
        $wrapper = $this->spacecraftWrapperFactory->wrapSpacecraft($spacecraft);
82
83
        foreach (SpacecraftModuleTypeEnum::cases() as $moduleType) {
84
85
            $moduleTypeId = $moduleType->value;
86
87
            if ($this->loggerUtil->doLog()) {
88
                $this->loggerUtil->log(sprintf("moduleTypeId: %d", $moduleTypeId));
89
            }
90
            $buildplanModules = $buildplan->getModulesByType($moduleType);
91
            if (!$buildplanModules->isEmpty()) {
92
                if ($this->loggerUtil->doLog()) {
93
                    $this->loggerUtil->log("wrapperCallable!");
94
                }
95
                $moduleRumpWrapper = $moduleType->getModuleRumpWrapperCallable()($rump, $buildplan);
96
                $moduleRumpWrapper->apply($wrapper);
97
            }
98
        }
99
100
        if ($spacecraft->getName() === '' || $spacecraft->getName() === sprintf('%s in Bau', $spacecraft->getRump()->getName())) {
101
            $spacecraft->setName($spacecraft->getRump()->getName());
102
        }
103
104
        $spacecraft->setAlertStateGreen();
105
106
        $this->spacecraftRepository->save($spacecraft);
107
108
        return $this->spacecraftConfiguratorFactory->createSpacecraftConfigurator($wrapper);
109
    }
110
111
    private function getSpacecraft(SpacecraftRumpInterface $rump, ?SpacecraftCreationConfigInterface $spacecraftCreationConfig): SpacecraftInterface
112
    {
113
        if ($spacecraftCreationConfig === null) {
114
            return $this->spacecraftFactory->create($rump);
115
        }
116
117
        return $spacecraftCreationConfig->getSpacecraft() ?? $this->spacecraftFactory->create($rump);
118
    }
119
}
120