Passed
Push — master ( aa3b71...5e2219 )
by Nico
55:07 queued 26:20
created

PirateAttack   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 46
ccs 0
cts 22
cp 0
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A attackShip() 0 21 2
A interceptIfNeccessary() 0 8 2
1
<?php
2
3
namespace Stu\Lib\Pirate\Component;
4
5
use Stu\Lib\Information\InformationWrapper;
6
use Stu\Module\Logging\LoggerUtilFactoryInterface;
7
use Stu\Module\Logging\PirateLoggerInterface;
8
use Stu\Module\Ship\Lib\Battle\ShipAttackCoreInterface;
9
use Stu\Module\Ship\Lib\FleetWrapperInterface;
10
use Stu\Module\Ship\Lib\Interaction\InterceptShipCoreInterface;
11
use Stu\Module\Ship\Lib\ShipWrapperFactoryInterface;
12
use Stu\Orm\Entity\ShipInterface;
13
14
class PirateAttack implements PirateAttackInterface
15
{
16
    private PirateLoggerInterface $logger;
17
18
    public function __construct(
19
        private InterceptShipCoreInterface $interceptShipCore,
20
        private ShipAttackCoreInterface $shipAttackCore,
21
        private ShipWrapperFactoryInterface $shipWrapperFactory,
22
        LoggerUtilFactoryInterface $loggerUtilFactory
23
    ) {
24
        $this->logger = $loggerUtilFactory->getPirateLogger();
25
    }
26
27
    public function attackShip(FleetWrapperInterface $fleetWrapper, ShipInterface $target): void
28
    {
29
        $leadWrapper = $fleetWrapper->getLeadWrapper();
30
31
        $this->interceptIfNeccessary($leadWrapper->get(), $target);
32
33
        if ($fleetWrapper->get()->getShips()->isEmpty()) {
34
            $this->logger->log('    cancel attack, no ships left');
35
            return;
36
        }
37
38
        $isFleetFight = false;
39
        $informations = new InformationWrapper();
40
41
        $this->logger->logf('    attacking target with shipId: %d', $target->getId());
42
43
        $this->shipAttackCore->attack(
44
            $leadWrapper,
45
            $this->shipWrapperFactory->wrapShip($target),
46
            $isFleetFight,
47
            $informations
48
        );
49
    }
50
51
52
    private function interceptIfNeccessary(ShipInterface $ship, ShipInterface $target): void
53
    {
54
        if (!$target->getWarpState()) {
55
            return;
56
        }
57
58
        $this->logger->logf('    intercepting target with shipId: %d', $target->getId());
59
        $this->interceptShipCore->intercept($ship, $target, new InformationWrapper());
60
    }
61
}
62