Passed
Push — dev ( 25004f...be11ce )
by Janko
16:12
created

ApplyBuildingDamage::damageBuilding()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 42
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 26
nc 10
nop 3
dl 0
loc 42
ccs 0
cts 25
cp 0
crap 56
rs 8.5706
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Colony\Lib\Damage;
6
7
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use RuntimeException;
9
use Stu\Lib\Damage\DamageModeEnum;
10
use Stu\Lib\Damage\DamageWrapper;
11
use Stu\Lib\Information\InformationWrapper;
12
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
13
use Stu\Orm\Entity\ColonyInterface;
14
use Stu\Orm\Entity\PlanetFieldInterface;
15
16
final class ApplyBuildingDamage implements ApplyBuildingDamageInterface
17
{
18 1
    public function __construct(
19
        private ColonyLibFactoryInterface $colonyLibFactory
20 1
    ) {}
21
22
    #[Override]
23
    public function damageBuilding(
24
        DamageWrapper $damageWrapper,
25
        PlanetFieldInterface $target,
26
        bool $isOrbitField
27
    ): InformationWrapper {
28
        $informations = new InformationWrapper();
29
30
        $colony = $target->getHost();
31
        if (!$colony instanceof ColonyInterface) {
32
            throw new RuntimeException('this should not happen');
33
        }
34
35
        $changeable = $colony->getChangeable();
36
37
        if (!$isOrbitField && $this->colonyLibFactory->createColonyShieldingManager($colony)->isShieldingEnabled()) {
38
            $damage = (int) $damageWrapper->getDamageRelative($colony, DamageModeEnum::SHIELDS);
39
            if ($damage > $changeable->getShields()) {
40
                $informations->addInformation("- Schildschaden: " . $changeable->getShields());
41
                $informations->addInformation("-- Schilde brechen zusammen!");
42
43
                $changeable->setShields(0);
44
            } else {
45
                $changeable->setShields($changeable->getShields() - $damage);
46
                $informations->addInformation("- Schildschaden: " . $damage . " - Status: " . $changeable->getShields());
47
            }
48
        }
49
        if ($damageWrapper->getNetDamage() <= 0) {
50
            return $informations;
51
        }
52
        $damage = (int) $damageWrapper->getDamageRelative($colony, DamageModeEnum::HULL);
53
        if ($target->getIntegrity() > $damage) {
54
            $target->setIntegrity($target->getIntegrity() - $damage);
55
            $informations->addInformation("- Gebäudeschaden: " . $damage . " - Status: " . $target->getIntegrity());
56
57
            return $informations;
58
        }
59
        $informations->addInformation("- Gebäudeschaden: " . $damage);
60
        $informations->addInformation("-- Das Gebäude wurde zerstört!");
61
        $target->setIntegrity(0);
62
63
        return $informations;
64
    }
65
}
66