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

ApplyFieldDamage::damage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 14
c 1
b 0
f 0
nc 2
nop 5
dl 0
loc 26
ccs 16
cts 16
cp 1
crap 2
rs 9.7998
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib\Damage;
6
7
use Stu\Lib\DamageWrapper;
8
use Stu\Module\History\Lib\EntryCreatorInterface;
9
use Stu\Module\Ship\Lib\Battle\ApplyDamageInterface;
10
use Stu\Module\Ship\Lib\Message\Message;
11
use Stu\Module\Ship\Lib\Message\MessageCollectionInterface;
12
use Stu\Module\Ship\Lib\ShipRemoverInterface;
13
use Stu\Module\Ship\Lib\ShipWrapperInterface;
14
15
final class ApplyFieldDamage implements ApplyFieldDamageInterface
16
{
17
    private ApplyDamageInterface $applyDamage;
18
19
    private EntryCreatorInterface $entryCreator;
20
21
    private ShipRemoverInterface $shipRemover;
22
23 3
    public function __construct(
24
        ApplyDamageInterface $applyDamage,
25
        EntryCreatorInterface $entryCreator,
26
        ShipRemoverInterface $shipRemover
27
    ) {
28 3
        $this->applyDamage = $applyDamage;
29 3
        $this->entryCreator = $entryCreator;
30 3
        $this->shipRemover = $shipRemover;
31
    }
32
33 2
    public function damage(
34
        ShipWrapperInterface $wrapper,
35
        int $damage,
36
        bool $isAbsolutDmg,
37
        string $cause,
38
        MessageCollectionInterface $messages
39
    ): void {
40
41
        //ship itself
42 2
        $this->damageShip(
43 2
            $wrapper,
44 2
            $damage,
45 2
            $isAbsolutDmg,
46 2
            $cause,
47 2
            $messages
48 2
        );
49
50
        //tractored ship
51 2
        $tractoredShipWrapper = $wrapper->getTractoredShipWrapper();
52 2
        if ($tractoredShipWrapper !== null) {
53 1
            $this->damageShip(
54 1
                $tractoredShipWrapper,
55 1
                $damage,
56 1
                $isAbsolutDmg,
57 1
                $cause,
58 1
                $messages
59 1
            );
60
        }
61
    }
62
63 2
    private function damageShip(
64
        ShipWrapperInterface $wrapper,
65
        int $damage,
66
        bool $isAbsolutDmg,
67
        string $cause,
68
        MessageCollectionInterface $messages
69
    ): void {
70 2
        $ship = $wrapper->get();
71
72 2
        $message = new Message(null, $ship->getUser()->getId());
73 2
        $messages->add($message);
74
75 2
        $shipName = $ship->getName();
76 2
        $rumpName = $ship->getRump()->getName();
77
78 2
        $dmg = $isAbsolutDmg ? $damage : $ship->getMaxHull() * $damage / 100;
79
80 2
        $message->add(sprintf(
81 2
            _('%s: Die %s wurde in Sektor %d|%d beschädigt'),
82 2
            $cause,
83 2
            $shipName,
84 2
            $ship->getPosX(),
85 2
            $ship->getPosY()
86 2
        ));
87 2
        $message->addMessageMerge($this->applyDamage->damage(
88 2
            new DamageWrapper((int) ceil($dmg)),
89 2
            $wrapper
90 2
        )->getInformations());
91
92 2
        if ($ship->isDestroyed()) {
93 1
            $this->entryCreator->addShipEntry(sprintf(
94 1
                _('Die %s (%s) wurde beim Einflug in Sektor %s zerstört'),
95 1
                $shipName,
96 1
                $rumpName,
97 1
                $ship->getSectorString()
98 1
            ));
99
100 1
            $this->shipRemover->destroy($wrapper);
101
        }
102
    }
103
}
104