Passed
Push — master ( 288b46...98b0e3 )
by Nico
31:30 queued 08:00
created

DeflectorConsequence::triggerSpecific()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 64
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 37
c 1
b 0
f 0
nc 10
nop 3
dl 0
loc 64
ccs 41
cts 41
cp 1
crap 7
rs 8.3946

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib\Movement\Component\Consequence\PostFlight;
6
7
use Stu\Component\Ship\System\ShipSystemTypeEnum;
8
use Stu\Module\Ship\Lib\Message\MessageCollectionInterface;
9
use Stu\Module\Ship\Lib\Damage\ApplyFieldDamageInterface;
10
use Stu\Module\Ship\Lib\Movement\Component\Consequence\AbstractFlightConsequence;
11
use Stu\Module\Ship\Lib\Movement\Route\FlightRouteInterface;
12
use Stu\Module\Ship\Lib\ShipWrapperInterface;
13
use Stu\Orm\Entity\MapFieldTypeInterface;
14
15
class DeflectorConsequence extends AbstractFlightConsequence
16
{
17
    private ApplyFieldDamageInterface $applyFieldDamage;
18
19 10
    public function __construct(
20
        ApplyFieldDamageInterface $applyFieldDamage
21
    ) {
22 10
        $this->applyFieldDamage = $applyFieldDamage;
23
    }
24
25 8
    protected function triggerSpecific(
26
        ShipWrapperInterface $wrapper,
27
        FlightRouteInterface $flightRoute,
28
        MessageCollectionInterface $messages
29
    ): void {
30
31 8
        if ($wrapper->get()->isTractored()) {
32 1
            return;
33
        }
34
35 7
        $ship = $wrapper->get();
36 7
        $nextFieldType = $flightRoute->getNextWaypoint()->getFieldType();
37
38
        //Einflugschaden Feldschaden
39 7
        if ($nextFieldType->getSpecialDamage() > 0) {
40 2
            $this->applyFieldDamage->damage(
41 2
                $wrapper,
42 2
                $nextFieldType->getSpecialDamage(),
43 2
                true,
44 2
                sprintf(
45 2
                    _('%s in Sektor %d|%d'),
46 2
                    $nextFieldType->getName(),
47 2
                    $ship->getPosX(),
48 2
                    $ship->getPosY()
49 2
                ),
50 2
                $messages
51 2
            );
52
53 2
            if ($ship->isDestroyed()) {
54 1
                return;
55
            }
56
        }
57
58 6
        $energyCost = $nextFieldType->getEnergyCosts();
59 6
        if ($energyCost === 0) {
60 2
            return;
61
        }
62
63
        //check for deflector state
64 4
        $deflectorDestroyed = !$ship->isSystemHealthy(ShipSystemTypeEnum::SYSTEM_DEFLECTOR);
65 4
        if ($deflectorDestroyed) {
66
67 1
            $this->applyFieldDamage->damage(
68 1
                $wrapper,
69 1
                $nextFieldType->getDamage(),
70 1
                false,
71 1
                'Deflektor außer Funktion.',
72 1
                $messages
73 1
            );
74
75 1
            return;
76
        }
77
78 3
        $hasEnoughEnergyForDeflector = $this->hasEnoughEpsForDeflector($wrapper, $nextFieldType);
79
80
        //Einflugschaden Energiemangel oder Deflektor zerstört
81 3
        if (!$hasEnoughEnergyForDeflector) {
82
83 2
            $this->applyFieldDamage->damage(
84 2
                $wrapper,
85 2
                $nextFieldType->getDamage(),
86 2
                false,
87 2
                'Nicht genug Energie für den Deflektor.',
88 2
                $messages
89 2
            );
90
        }
91
    }
92
93
94 3
    private function hasEnoughEpsForDeflector(
95
        ShipWrapperInterface $wrapper,
96
        MapFieldTypeInterface $nextFieldType
97
    ): bool {
98
99 3
        $epsSystem = $wrapper->getEpsSystemData();
100 3
        if ($epsSystem === null) {
101 1
            return false;
102
        }
103
104 2
        $energyCost = $nextFieldType->getEnergyCosts();
105
106 2
        if ($epsSystem->getEps() < $energyCost) {
107 1
            $epsSystem->setEps(0)->update();
108 1
            return false;
109
        }
110
111 1
        $epsSystem->lowerEps($nextFieldType->getEnergyCosts())->update();
112
113 1
        return true;
114
    }
115
}
116