Passed
Push — master ( bf860f...1dd8f7 )
by Nico
57:55 queued 29:36
created

AbstractWeaponPhase::checkForShipDestruction()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 4
dl 0
loc 16
ccs 7
cts 8
cp 0.875
crap 3.0175
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib\Battle\Weapon;
6
7
use Stu\Component\Building\BuildingManagerInterface;
8
use Stu\Component\Ship\ShipModuleTypeEnum;
9
use Stu\Lib\Information\InformationInterface;
10
use Stu\Module\Control\StuRandom;
11
use Stu\Module\History\Lib\EntryCreatorInterface;
12
use Stu\Module\Logging\LoggerUtilFactoryInterface;
13
use Stu\Module\Logging\LoggerUtilInterface;
14
use Stu\Module\Ship\Lib\Damage\ApplyDamageInterface;
15
use Stu\Module\Ship\Lib\Destruction\ShipDestroyerInterface;
16
use Stu\Module\Ship\Lib\Destruction\ShipDestructionCauseEnum;
17
use Stu\Module\Ship\Lib\Destruction\ShipDestructionInterface;
18
use Stu\Module\Ship\Lib\ShipWrapperInterface;
19
use Stu\Orm\Entity\ModuleInterface;
20
use Stu\Orm\Entity\ShipInterface;
21
22
abstract class AbstractWeaponPhase
23
{
24
    protected LoggerUtilInterface $loggerUtil;
25
26 1
    public function __construct(
27
        protected EntryCreatorInterface $entryCreator,
28
        protected ApplyDamageInterface $applyDamage,
29
        protected BuildingManagerInterface $buildingManager,
30
        protected StuRandom $stuRandom,
31
        private ShipDestructionInterface $shipDestruction,
32
        LoggerUtilFactoryInterface $loggerUtilFactory
33
    ) {
34 1
        $this->loggerUtil = $loggerUtilFactory->getLoggerUtil();
35
    }
36
37 1
    protected function checkForShipDestruction(
38
        ShipDestroyerInterface $attacker,
39
        ShipWrapperInterface $targetWrapper,
40
        bool $isAlertRed,
41
        InformationInterface $message
42
    ): void {
43
44 1
        if (!$targetWrapper->get()->isDestroyed()) {
45
            return;
46
        }
47
48 1
        $this->shipDestruction->destroy(
49 1
            $attacker,
50 1
            $targetWrapper,
51 1
            $isAlertRed ? ShipDestructionCauseEnum::ALERT_RED : ShipDestructionCauseEnum::SHIP_FIGHT,
52 1
            $message
53 1
        );
54
    }
55
56 1
    protected function getModule(ShipInterface $ship, ShipModuleTypeEnum $moduleType): ?ModuleInterface
57
    {
58 1
        $buildplan = $ship->getBuildplan();
59 1
        if ($buildplan === null) {
60 1
            return null;
61
        }
62
63
        $buildplanModule = current($buildplan->getModulesByType($moduleType));
64
        if (!$buildplanModule) {
65
            return null;
66
        }
67
68
        return $buildplanModule->getModule();
69
    }
70
}
71