Passed
Push — master ( 6d8b29...d7e053 )
by Janko
27:40
created

TrapDetection   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getPrestigeOfAlertedSpacecrafts() 0 7 1
A isAlertTrap() 0 18 3
A __construct() 0 5 1
1
<?php
2
3
namespace Stu\Lib\Pirate\Component;
4
5
use Doctrine\Common\Collections\Collection;
6
use Stu\Module\Control\StuRandom;
7
use Stu\Module\Prestige\Lib\PrestigeCalculationInterface;
8
use Stu\Module\Ship\Lib\Battle\AlertDetection\AlertedShipsDetectionInterface;
9
use Stu\Module\Ship\Lib\ShipWrapperInterface;
10
use Stu\Orm\Entity\LocationInterface;
11
use Stu\Orm\Entity\ShipInterface;
12
13
class TrapDetection implements TrapDetectionInterface
14
{
15 4
    public function __construct(
16
        private AlertedShipsDetectionInterface $alertedShipsDetection,
17
        private PrestigeCalculationInterface $prestigeCalculation,
18
        private StuRandom $stuRandom
19 4
    ) {}
20
21 4
    public function isAlertTrap(LocationInterface $location, ShipInterface $leadShip): bool
22
    {
23 4
        $alertedWrappers = $this->alertedShipsDetection->getAlertedShipsOnLocation(
24 4
            $location,
25 4
            $leadShip->getUser()
26 4
        );
27 4
        if ($alertedWrappers->isEmpty()) {
28 1
            return false;
29
        }
30
31 3
        $piratePrestige = $this->prestigeCalculation->getPrestigeOfSpacecraftOrFleet($leadShip);
32 3
        $alertedPrestige = $this->getPrestigeOfAlertedSpacecrafts($alertedWrappers);
33
34 3
        if ($alertedPrestige <= 3 * $piratePrestige) {
35 1
            return false;
36
        }
37
38 2
        return $this->stuRandom->rand(0, $alertedPrestige) > $piratePrestige;
39
    }
40
41
    /** @param Collection<int, ShipWrapperInterface> $alertedWrappers */
42 3
    private function getPrestigeOfAlertedSpacecrafts(Collection $alertedWrappers): int
43
    {
44 3
        return $alertedWrappers
45 3
            ->map(fn(ShipWrapperInterface $wrapper) => $this->prestigeCalculation->getPrestigeOfSpacecraftOrFleet($wrapper))
46 3
            ->reduce(
47 3
                fn(int $sum, int $prestige) => $sum + $prestige,
48 3
                0
49 3
            );
50
    }
51
}
52