SystemActivation   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 4.44%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 43
dl 0
loc 65
ccs 2
cts 45
cp 0.0444
rs 10
c 1
b 0
f 0
wmc 16

3 Methods

Rating   Name   Duplication   Size   Complexity  
A logError() 0 6 3
C activateIntern() 0 49 12
A __construct() 0 5 1
1
<?php
2
3
namespace Stu\Component\Spacecraft\System\Control;
4
5
use Stu\Component\Spacecraft\System\Exception\ActivationConditionsNotMetException;
6
use Stu\Component\Spacecraft\System\Exception\AlreadyActiveException;
7
use Stu\Component\Spacecraft\System\Exception\InsufficientCrewException;
8
use Stu\Component\Spacecraft\System\Exception\InsufficientEnergyException;
9
use Stu\Component\Spacecraft\System\Exception\SpacecraftSystemException;
10
use Stu\Component\Spacecraft\System\Exception\SystemCooldownException;
11
use Stu\Component\Spacecraft\System\Exception\SystemDamagedException;
12
use Stu\Component\Spacecraft\System\Exception\SystemNotActivatableException;
13
use Stu\Component\Spacecraft\System\Exception\SystemNotFoundException;
14
use Stu\Component\Spacecraft\System\SpacecraftSystemManagerInterface;
15
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
16
use Stu\Lib\Information\InformationInterface;
17
use Stu\Module\Spacecraft\Lib\Movement\Component\PreFlight\ConditionCheckResult;
18
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
19
use Stu\Module\Template\TemplateHelperInterface;
20
use Stu\Orm\Entity\Ship;
21
use Stu\Orm\Entity\Spacecraft;
22
use Stu\Orm\Repository\SpacecraftRepositoryInterface;
23
24
class SystemActivation
25
{
26 1
    public function __construct(
27
        private readonly SpacecraftSystemManagerInterface $spacecraftSystemManager,
28
        private readonly SpacecraftRepositoryInterface $spacecraftRepository,
29
        private readonly TemplateHelperInterface $templateHelper
30 1
    ) {}
31
32
    public function activateIntern(
33
        SpacecraftWrapperInterface $wrapper,
34
        SpacecraftSystemTypeEnum $type,
35
        ConditionCheckResult|InformationInterface $logger,
36
        bool $isDryRun
37
    ): bool {
38
        $systemName = $type->getDescription();
39
        $spacecraft = $wrapper->get();
40
41
        try {
42
            $this->spacecraftSystemManager->activate($wrapper, $type, false, $isDryRun);
43
            $this->spacecraftRepository->save($spacecraft);
44
            if ($logger instanceof InformationInterface) {
45
                $logger->addInformationf(_('%s: System %s aktiviert'), $spacecraft->getName(), $systemName);
46
            }
47
            return true;
48
        } catch (AlreadyActiveException) {
49
            if ($logger instanceof InformationInterface) {
50
                $logger->addInformationf(_('%s: System %s ist bereits aktiviert'), $spacecraft->getName(), $systemName);
51
            }
52
        } catch (SystemNotActivatableException) {
53
            $this->logError($spacecraft, sprintf(_('%s: [b][color=#ff2626]System %s besitzt keinen Aktivierungsmodus[/color][/b]'), $spacecraft->getName(), $systemName), $logger);
54
        } catch (InsufficientEnergyException $e) {
55
            $this->logError($spacecraft, sprintf(
56
                _('%s: [b][color=#ff2626]System %s kann aufgrund Energiemangels (%d benötigt) nicht aktiviert werden[/color][/b]'),
57
                $spacecraft->getName(),
58
                $systemName,
59
                $e->getNeededEnergy()
60
            ), $logger);
61
        } catch (SystemCooldownException $e) {
62
            $this->logError($spacecraft, sprintf(
63
                _('%s: [b][color=#ff2626]System %s kann nicht aktiviert werden, Cooldown noch %s[/color][/b]'),
64
                $spacecraft->getName(),
65
                $systemName,
66
                $this->templateHelper->formatSeconds((string) $e->getRemainingSeconds())
67
            ), $logger);
68
        } catch (SystemDamagedException) {
69
            $this->logError($spacecraft, sprintf(_('%s: [b][color=#ff2626]System %s ist beschädigt und kann daher nicht aktiviert werden[/color][/b]'), $spacecraft->getName(), $systemName), $logger);
70
        } catch (ActivationConditionsNotMetException $e) {
71
            $this->logError($spacecraft, sprintf(_('%s: [b][color=#ff2626]System %s konnte nicht aktiviert werden, weil %s[/color][/b]'), $spacecraft->getName(), $systemName, $e->getMessage()), $logger);
72
        } catch (SystemNotFoundException) {
73
            $this->logError($spacecraft, sprintf(_('%s: [b][color=#ff2626]System %s nicht vorhanden[/color][/b]'), $spacecraft->getName(), $systemName), $logger);
74
        } catch (InsufficientCrewException) {
75
            $this->logError($spacecraft, sprintf(_('%s: [b][color=#ff2626]System %s konnte wegen Mangel an Crew nicht aktiviert werden[/color][/b]'), $spacecraft->getName(), $systemName), $logger);
76
        } catch (SpacecraftSystemException) {
77
            $this->logError($spacecraft, sprintf(_('%s: [b][color=#ff2626]System %s konnte nicht aktiviert werden[/color][/b]'), $spacecraft->getName(), $systemName), $logger);
78
        }
79
80
        return false;
81
    }
82
83
    private function logError(Spacecraft $spacecraft, string $message, ConditionCheckResult|InformationInterface $logger): void
84
    {
85
        if ($logger instanceof InformationInterface) {
86
            $logger->addInformation($message);
87
        } elseif ($spacecraft instanceof Ship) {
88
            $logger->addBlockedShip($spacecraft, $message);
89
        }
90
    }
91
}
92