Passed
Pull Request — master (#2331)
by Janko
11:37 queued 06:03
created

SystemDeactivation::deactivateIntern()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
nc 9
nop 3
dl 0
loc 23
ccs 9
cts 15
cp 0.6
crap 6.6
rs 9.4555
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
14
class SystemDeactivation
15
{
16 1
    public function __construct(
17
        private readonly SpacecraftSystemManagerInterface $spacecraftSystemManager
18 1
    ) {}
19
20 5
    public function deactivateIntern(
21
        SpacecraftWrapperInterface $wrapper,
22
        spacecraftSystemTypeEnum $type,
23
        InformationInterface $informations
24
    ): bool {
25 5
        $systemName = $type->getDescription();
26 5
        $ship = $wrapper->get();
27
28
        try {
29 5
            $this->spacecraftSystemManager->deactivate($wrapper, $type);
30 3
            $informations->addInformationf(_('%s: System %s deaktiviert'), $ship->getName(), $systemName);
31 3
            return true;
32 3
        } catch (AlreadyOffException) {
33 3
            $informations->addInformationf(_('%s: System %s ist bereits deaktiviert'), $ship->getName(), $systemName);
34
        } catch (SystemNotDeactivatableException) {
35
            $informations->addInformationf(_('%s: [b][color=#ff2626]System %s besitzt keinen Deaktivierungsmodus[/color][/b]'), $ship->getName(), $systemName);
36
        } catch (DeactivationConditionsNotMetException $e) {
37
            $informations->addInformationf(_('%s: [b][color=#ff2626]System %s konnte nicht deaktiviert werden, weil %s[/color][/b]'), $ship->getName(), $systemName, $e->getMessage());
38
        } catch (SystemNotFoundException) {
39
            $informations->addInformationf(_('%s: System %s nicht vorhanden'), $ship->getName(), $systemName);
40
        }
41
42 3
        return false;
43
    }
44
}
45