Passed
Push — dev ( 996fbb...25004f )
by Janko
16:40
created

SystemDeactivation::deactivateIntern()   A

Complexity

Conditions 5
Paths 13

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 16
nc 13
nop 3
dl 0
loc 24
ccs 0
cts 16
cp 0
crap 30
rs 9.4222
c 1
b 0
f 0
1
<?php
2
3
namespace Stu\Component\Spacecraft\System\Control;
4
5
use Stu\Component\Spacecraft\System\Exception\AlreadyOffException;
6
use Stu\Component\Spacecraft\System\Exception\DeactivationConditionsNotMetException;
7
use Stu\Component\Spacecraft\System\Exception\SystemNotDeactivatableException;
8
use Stu\Component\Spacecraft\System\Exception\SystemNotFoundException;
9
use Stu\Component\Spacecraft\System\SpacecraftSystemManagerInterface;
10
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
11
use Stu\Lib\Information\InformationInterface;
12
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
13
use Stu\Orm\Repository\SpacecraftRepositoryInterface;
14
15
class SystemDeactivation
16
{
17 1
    public function __construct(
18
        private readonly SpacecraftSystemManagerInterface $spacecraftSystemManager,
19
        private readonly SpacecraftRepositoryInterface $spacecraftRepository
20 1
    ) {}
21
22
    public function deactivateIntern(
23
        SpacecraftWrapperInterface $wrapper,
24
        spacecraftSystemTypeEnum $type,
25
        InformationInterface $informations
26
    ): bool {
27
        $systemName = $type->getDescription();
28
        $ship = $wrapper->get();
29
30
        try {
31
            $this->spacecraftSystemManager->deactivate($wrapper, $type);
32
            $this->spacecraftRepository->save($ship);
33
            $informations->addInformationf(_('%s: System %s deaktiviert'), $ship->getName(), $systemName);
34
            return true;
35
        } catch (AlreadyOffException) {
36
            $informations->addInformationf(_('%s: System %s ist bereits deaktiviert'), $ship->getName(), $systemName);
37
        } catch (SystemNotDeactivatableException) {
38
            $informations->addInformationf(_('%s: [b][color=#ff2626]System %s besitzt keinen Deaktivierungsmodus[/color][/b]'), $ship->getName(), $systemName);
39
        } catch (DeactivationConditionsNotMetException $e) {
40
            $informations->addInformationf(_('%s: [b][color=#ff2626]System %s konnte nicht deaktiviert werden, weil %s[/color][/b]'), $ship->getName(), $systemName, $e->getMessage());
41
        } catch (SystemNotFoundException) {
42
            $informations->addInformationf(_('%s: System %s nicht vorhanden'), $ship->getName(), $systemName);
43
        }
44
45
        return false;
46
    }
47
}
48