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

SystemActivation::logInfo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
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
23
class SystemActivation
24
{
25 1
    public function __construct(
26
        private readonly SpacecraftSystemManagerInterface $spacecraftSystemManager,
27
        private readonly TemplateHelperInterface $templateHelper
28 1
    ) {}
29
30 13
    public function activateIntern(
31
        SpacecraftWrapperInterface $wrapper,
32
        SpacecraftSystemTypeEnum $type,
33
        ConditionCheckResult|InformationInterface $logger,
34
        bool $isDryRun
35
    ): bool {
36 13
        $systemName = $type->getDescription();
37 13
        $spacecraft = $wrapper->get();
38
39
        try {
40 13
            $this->spacecraftSystemManager->activate($wrapper, $type, false, $isDryRun);
41 8
            $this->logInfo(sprintf(_('%s: System %s aktiviert'), $spacecraft->getName(), $systemName), $logger);
42 8
            return true;
43 8
        } catch (AlreadyActiveException) {
44 3
            $this->logInfo(sprintf(_('%s: System %s ist bereits aktiviert'), $spacecraft->getName(), $systemName), $logger);
45 6
        } catch (SystemNotActivatableException) {
46
            $this->logError($spacecraft, sprintf(_('%s: [b][color=#ff2626]System %s besitzt keinen Aktivierungsmodus[/color][/b]'), $spacecraft->getName(), $systemName), $logger);
47 6
        } catch (InsufficientEnergyException $e) {
48
            $this->logError($spacecraft, sprintf(
49
                _('%s: [b][color=#ff2626]System %s kann aufgrund Energiemangels (%d benötigt) nicht aktiviert werden[/color][/b]'),
50
                $spacecraft->getName(),
51
                $systemName,
52
                $e->getNeededEnergy()
53
            ), $logger);
54 6
        } catch (SystemCooldownException $e) {
55
            $this->logError($spacecraft, sprintf(
56
                _('%s: [b][color=#ff2626]System %s kann nicht aktiviert werden, Cooldown noch %s[/color][/b]'),
57
                $spacecraft->getName(),
58
                $systemName,
59
                $this->templateHelper->formatSeconds((string) $e->getRemainingSeconds())
60
            ), $logger);
61 6
        } catch (SystemDamagedException) {
62
            $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);
63 6
        } catch (ActivationConditionsNotMetException $e) {
64
            $this->logError($spacecraft, sprintf(_('%s: [b][color=#ff2626]System %s konnte nicht aktiviert werden, weil %s[/color][/b]'), $spacecraft->getName(), $systemName, $e->getMessage()), $logger);
65 6
        } catch (SystemNotFoundException) {
66 5
            $this->logError($spacecraft, sprintf(_('%s: [b][color=#ff2626]System %s nicht vorhanden[/color][/b]'), $spacecraft->getName(), $systemName), $logger);
67 1
        } catch (InsufficientCrewException) {
68 1
            $this->logError($spacecraft, sprintf(_('%s: [b][color=#ff2626]System %s konnte wegen Mangel an Crew nicht aktiviert werden[/color][/b]'), $spacecraft->getName(), $systemName), $logger);
69
        } catch (SpacecraftSystemException) {
70
            $this->logError($spacecraft, sprintf(_('%s: [b][color=#ff2626]System %s konnte nicht aktiviert werden[/color][/b]'), $spacecraft->getName(), $systemName), $logger);
71
        }
72
73 8
        return false;
74
    }
75
76 6
    private function logError(Spacecraft $spacecraft, string $message, ConditionCheckResult|InformationInterface $logger): void
77
    {
78 6
        if ($logger instanceof InformationInterface) {
79 6
            $logger->addInformation($message);
80
        } elseif ($spacecraft instanceof Ship) {
81
            $logger->addBlockedShip($spacecraft, $message);
82
        }
83
    }
84
85 10
    private function logInfo(string $message, ConditionCheckResult|InformationInterface $logger): void
86
    {
87 10
        if ($logger instanceof InformationInterface) {
88 6
            $logger->addInformation($message);
89
        }
90
    }
91
}
92