Passed
Push — dev ( 3fd410...aa33df )
by Janko
13:32
created

SpacecraftCreator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 4.65%

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 92
ccs 2
cts 43
cp 0.0465
rs 10
c 0
b 0
f 0
wmc 13

3 Methods

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