Passed
Push — master ( 4cbcfa...1ee8de )
by Nico
53:18 queued 29:25
created

ShipCrewCalculator::getBaseCrewCount()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 1
dl 0
loc 10
ccs 0
cts 7
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Ship\Crew;
6
7
use Stu\Component\Ship\ShipRumpEnum;
8
use Stu\Component\Ship\System\Type\TroopQuartersShipSystem;
9
use Stu\Orm\Entity\ModuleInterface;
10
use Stu\Orm\Entity\ShipInterface;
11
use Stu\Orm\Entity\ShipRumpCategoryRoleCrewInterface;
12
use Stu\Orm\Entity\ShipRumpInterface;
13
use Stu\Orm\Entity\UserInterface;
14
use Stu\Orm\Repository\ShipRumpCategoryRoleCrewRepositoryInterface;
15
16
/**
17
 * Provides several calculation methods to retrieve the (max) crew counts of rumps and ships
18
 */
19
final class ShipCrewCalculator implements ShipCrewCalculatorInterface
20
{
21
    private ShipRumpCategoryRoleCrewRepositoryInterface $shipRumpCategoryRoleCrewRepository;
22
23 4
    public function __construct(
24
        ShipRumpCategoryRoleCrewRepositoryInterface $shipRumpCategoryRoleCrewRepository
25
    ) {
26 4
        $this->shipRumpCategoryRoleCrewRepository = $shipRumpCategoryRoleCrewRepository;
27
    }
28
29
    public function getMaxCrewCountByRump(
30
        ShipRumpInterface $shipRump
31
    ): int {
32
        if ($this->getCrewObj($shipRump) === null) {
33
            return $this->getBaseCrewCount($shipRump);
34
        } else {
35
            return $this->getBaseCrewCount($shipRump) + $this->getCrewObj($shipRump)->getJob6Crew();
36
        }
37
    }
38
39
    public function getCrewObj(
40
        ShipRumpInterface $shipRump
41
    ): ?ShipRumpCategoryRoleCrewInterface {
42
        return $this->shipRumpCategoryRoleCrewRepository
43
            ->getByShipRumpCategoryAndRole(
44
                $shipRump->getCategoryId(),
45
                (int) $shipRump->getRoleId()
46
            );
47
    }
48
49
    public function getMaxCrewCountByShip(
50
        ShipInterface $ship
51
    ): int {
52
        $rump = $ship->getRump();
53
54
        $crewCount = $this->getMaxCrewCountByRump($rump);
55
56
        if ($ship->isTroopQuartersHealthy()) {
57
            if ($rump->getRoleId() === ShipRumpEnum::SHIP_ROLE_BASE) {
58
                $crewCount += TroopQuartersShipSystem::QUARTER_COUNT_BASE;
59
            } else {
60
                $crewCount += TroopQuartersShipSystem::QUARTER_COUNT;
61
            }
62
        }
63
        return $crewCount;
64
    }
65
66
    public function getCrewUsage(array $modules, ShipRumpInterface $rump, UserInterface $user): int
67
    {
68
        return array_reduce(
69
            $modules,
70
            fn (int $value, ModuleInterface $module) => $value + $module->getCrewByFactionAndRumpLvl(
71
                $user->getFaction(),
72
                $rump
73
            ),
74
            $rump->getBaseCrew()
75
        );
76
    }
77
78
    private function getBaseCrewCount(ShipRumpInterface $shipRump): int
79
    {
80
        $count = $shipRump->getBaseCrew();
81
        if ($this->getCrewObj($shipRump) !== null) {
82
            foreach ([1, 2, 3, 4, 5, 7] as $slot) {
83
                $crew_func = 'getJob' . $slot . 'Crew';
84
                $count += $this->getCrewObj($shipRump)->$crew_func();
85
            }
86
        }
87
        return $count;
88
    }
89
}
90