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

SpacecraftStartup   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 14
dl 0
loc 26
ccs 17
cts 17
cp 1
rs 10
c 1
b 1
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A startup() 0 18 5
A __construct() 0 5 1
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