Passed
Push — master ( 5a11dc...578008 )
by Janko
08:39
created

ColonyShieldingManager   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 40%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 63
ccs 12
cts 30
cp 0.4
rs 10
c 0
b 0
f 0
wmc 14
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Colony\Shields;
6
7
use Override;
8
use Stu\Component\Building\BuildingFunctionEnum;
9
use Stu\Component\Colony\ColonyFunctionManagerInterface;
10
use Stu\Lib\Colony\PlanetFieldHostInterface;
11
use Stu\Orm\Entity\Colony;
12
use Stu\Orm\Repository\PlanetFieldRepositoryInterface;
13
14
/**
15
 * Provides shielding related methods
16
 */
17
final class ColonyShieldingManager implements ColonyShieldingManagerInterface
18
{
19
    public const int SHIELD_GENERATOR_CAPACITY = 4000;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 19 at column 21
Loading history...
20
    public const int SHIELD_BATTERY_CAPACITY = 10000;
21
22 12
    public function __construct(
23
        private PlanetFieldRepositoryInterface $planetFieldRepository,
24
        private ColonyFunctionManagerInterface $colonyFunctionManager,
25
        private PlanetFieldHostInterface $host
26 12
    ) {}
27
28
    #[Override]
29
    public function updateActualShields(): void
30
    {
31
        if (!$this->host instanceof Colony) {
32
            return;
33
        }
34
35
        $shieldState = false;
36
        $shields = 0;
37
38
        foreach ($this->host->getPlanetFields() as $field) {
39
            $building = $field->getBuilding();
40
41
            if ($building === null || !$field->isActive()) {
42
                continue;
43
            }
44
45
            $functions = $building->getFunctions();
46
47
            if ($functions->containsKey(BuildingFunctionEnum::SHIELD_GENERATOR->value)) {
48
                $shields += self::SHIELD_GENERATOR_CAPACITY;
49
                $shieldState = true;
50
            }
51
52
            if ($functions->containsKey(BuildingFunctionEnum::SHIELD_BATTERY->value)) {
53
                $shields += self::SHIELD_BATTERY_CAPACITY;
54
            }
55
        }
56
57
        if ($shieldState) {
58
            $changeable = $this->host->getChangeable();
59
            $changeable->setShields(min($changeable->getShields(), $shields));
60
        }
61
    }
62
63 6
    #[Override]
64
    public function hasShielding(): bool
65
    {
66 6
        return $this->colonyFunctionManager->hasFunction(
67 6
            $this->host,
68 6
            BuildingFunctionEnum::SHIELD_GENERATOR
69 6
        );
70
    }
71
72 1
    #[Override]
73
    public function getMaxShielding(): int
74
    {
75 1
        return $this->planetFieldRepository->getMaxShieldsOfHost($this->host);
76
    }
77
78 5
    #[Override]
79
    public function isShieldingEnabled(): bool
80
    {
81 5
        return $this->colonyFunctionManager->hasActiveFunction($this->host, BuildingFunctionEnum::SHIELD_GENERATOR)
82 5
            && ($this->host instanceof Colony ? $this->host->getChangeable()->getShields() > 0 : true);
83
    }
84
}
85