Passed
Push — dev ( 1dcdd9...74922b )
by Janko
06:48
created

SpacecraftStartup::startup()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 13
nc 2
nop 2
dl 0
loc 18
ccs 15
cts 15
cp 1
crap 5
rs 9.5222
c 1
b 1
f 0
1
<?php
2
3
namespace Stu\Module\Spacecraft\Lib\Auxiliary;
4
5
use Stu\Component\Spacecraft\System\Control\ActivatorDeactivatorHelperInterface;
6
use Stu\Component\Spacecraft\System\SpacecraftSystemManagerInterface;
7
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeInterface;
8
use Stu\Lib\Information\InformationWrapper;
9
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
10
use Stu\Orm\Entity\SpacecraftSystem;
11
use Stu\Orm\Repository\SpacecraftRepositoryInterface;
12
13
class SpacecraftStartup implements SpacecraftStartupInterface
14
{
15 3
    public function __construct(
16
        private readonly SpacecraftRepositoryInterface $spacecraftRepository,
17
        private readonly SpacecraftSystemManagerInterface $spacecraftSystemManager,
18
        private readonly ActivatorDeactivatorHelperInterface $helper
19 3
    ) {}
20
21 2
    public function startup(SpacecraftWrapperInterface $wrapper, array $additionalSystemTypes = []): void
22
    {
23 2
        $isSaveNeeded = false;
24
25 2
        $wrapper
26 2
            ->get()
27 2
            ->getSystems()
28 2
            ->filter(fn(SpacecraftSystem $system): bool => !$system->getMode()->isActivated() && $system->isHealthy())
29 2
            ->map(fn(SpacecraftSystem $system): SpacecraftSystemTypeInterface =>  $this->spacecraftSystemManager->lookupSystem($system->getSystemType()))
30 2
            ->filter(fn(SpacecraftSystemTypeInterface $systemType): bool => $systemType->getDefaultMode()->isActivated()
31 2
                || in_array($systemType->getSystemType(), $additionalSystemTypes))
32 2
            ->forAll(function (int $key, SpacecraftSystemTypeInterface $systemType) use ($wrapper, &$isSaveNeeded): bool {
33 2
                $isSaveNeeded = $this->helper->activate($wrapper, $systemType->getSystemType(), new InformationWrapper()) || $isSaveNeeded;
34 2
                return true;
35 2
            });
36
37 2
        if ($isSaveNeeded) {
38 1
            $this->spacecraftRepository->save($wrapper->get());
39
        }
40
    }
41
}
42