Passed
Push — master ( 66afc8...ff3c83 )
by Janko
09:01
created

SpacecraftCrewCalculator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 65.85%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 86
ccs 27
cts 41
cp 0.6585
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getCrewUsage() 0 10 1
A __construct() 0 3 1
A getMaxCrewCountByShip() 0 16 3
A getMaxCrewCountByRump() 0 8 2
A getCrewObj() 0 20 3
A getBaseCrewCount() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Spacecraft\Crew;
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 Stu\Component\Crew\CrewTypeEnum;
9
use Stu\Component\Spacecraft\SpacecraftRumpRoleEnum;
10
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
11
use Stu\Component\Spacecraft\System\Type\TroopQuartersShipSystem;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Spacecraft...TroopQuartersShipSystem 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...
12
use Stu\Orm\Entity\Module;
13
use Stu\Orm\Entity\ShipRumpCategoryRoleCrew;
14
use Stu\Orm\Entity\SpacecraftRump;
15
use Stu\Orm\Entity\Spacecraft;
16
use Stu\Orm\Entity\User;
17
use Stu\Orm\Repository\ShipRumpCategoryRoleCrewRepositoryInterface;
18
19
/**
20
 * Provides several calculation methods to retrieve the (max) crew counts of rumps and ships
21
 */
22
final class SpacecraftCrewCalculator implements SpacecraftCrewCalculatorInterface
23
{
24
    /** @var array<int, ?ShipRumpCategoryRoleCrew>  */
25
    private array $shipRumpCategoryRoleCrewCache = [];
26
27
    /** @var array<int, int>  */
28
    private array $baseCrewCountCache = [];
29
30 2
    public function __construct(
31
        private readonly ShipRumpCategoryRoleCrewRepositoryInterface $shipRumpCategoryRoleCrewRepository
32 2
    ) {}
33
34 11
    #[Override]
35
    public function getMaxCrewCountByRump(
36
        SpacecraftRump $shipRump
37
    ): int {
38 11
        if ($this->getCrewObj($shipRump) === null) {
39
            return $this->getBaseCrewCount($shipRump);
40
        } else {
41 11
            return $this->getBaseCrewCount($shipRump) + $this->getCrewObj($shipRump)->getCrewForPosition(CrewTypeEnum::CREWMAN);
42
        }
43
    }
44
45 11
    #[Override]
46
    public function getCrewObj(
47
        SpacecraftRump $shipRump
48
    ): ?ShipRumpCategoryRoleCrew {
49
50 11
        $rumpRole = $shipRump->getRoleId();
51 11
        if ($rumpRole === null) {
52
            return null;
53
        }
54
55 11
        $id = $rumpRole->value;
56 11
        if (!array_key_exists($id, $this->shipRumpCategoryRoleCrewCache)) {
57 2
            $this->shipRumpCategoryRoleCrewCache[$id] = $this->shipRumpCategoryRoleCrewRepository
58 2
                ->getByShipRumpCategoryAndRole(
59 2
                    $shipRump->getCategoryId(),
60 2
                    $rumpRole
61 2
                );
62
        }
63
64 11
        return $this->shipRumpCategoryRoleCrewCache[$id];
65
    }
66
67 7
    #[Override]
68
    public function getMaxCrewCountByShip(
69
        Spacecraft $spacecraft
70
    ): int {
71 7
        $rump = $spacecraft->getRump();
72
73 7
        $crewCount = $this->getMaxCrewCountByRump($rump);
74
75 7
        if ($spacecraft->isSystemHealthy(SpacecraftSystemTypeEnum::TROOP_QUARTERS)) {
76
            if ($rump->getRoleId() === SpacecraftRumpRoleEnum::BASE) {
77
                $crewCount += TroopQuartersShipSystem::QUARTER_COUNT_BASE;
78
            } else {
79
                $crewCount += TroopQuartersShipSystem::QUARTER_COUNT;
80
            }
81
        }
82 7
        return $crewCount;
83
    }
84
85
    #[Override]
86
    public function getCrewUsage(array $modules, SpacecraftRump $rump, User $user): int
87
    {
88
        return array_reduce(
89
            $modules,
90
            fn(int $value, Module $module): int => $value + $module->getCrewByFactionAndRumpLvl(
91
                $user->getFaction(),
92
                $rump
93
            ),
94
            $rump->getBaseValues()->getBaseCrew()
95
        );
96
    }
97
98 11
    private function getBaseCrewCount(SpacecraftRump $shipRump): int
99
    {
100 11
        $key = $shipRump->getId();
101 11
        if (!array_key_exists($key, $this->baseCrewCountCache)) {
102
103 2
            $this->baseCrewCountCache[$key] = $shipRump->getBaseValues()->getBaseCrew()
104 2
                + ($this->getCrewObj($shipRump)?->getCrewSumForPositionsExceptCrewman() ?? 0);
105
        }
106
107 11
        return $this->baseCrewCountCache[$key];
108
    }
109
}
110