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

SystemDeactivation   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 11.11%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 31
ccs 2
cts 18
cp 0.1111
rs 10
c 1
b 0
f 0
wmc 6

2 Methods

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