Passed
Push — master ( f544cb...b3a3d9 )
by Nico
36:43 queued 09:10
created

ColonyShieldingManager   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 45.16%

Importance

Changes 0
Metric Value
eloc 29
c 0
b 0
f 0
dl 0
loc 68
ccs 14
cts 31
cp 0.4516
rs 10
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A hasShielding() 0 5 1
A isShieldingEnabled() 0 4 3
A __construct() 0 8 1
B updateActualShields() 0 30 8
A getMaxShielding() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Colony\Shields;
6
7
use Stu\Component\Building\BuildingEnum;
8
use Stu\Component\Colony\ColonyFunctionManagerInterface;
9
use Stu\Lib\Colony\PlanetFieldHostInterface;
10
use Stu\Orm\Entity\ColonyInterface;
11
use Stu\Orm\Repository\PlanetFieldRepositoryInterface;
12
13
/**
14
 * Provides shielding related methods
15
 */
16
final class ColonyShieldingManager implements ColonyShieldingManagerInterface
17
{
18
    private PlanetFieldRepositoryInterface $planetFieldRepository;
19
20
    private ColonyFunctionManagerInterface $colonyFunctionManager;
21
22
    private PlanetFieldHostInterface $host;
23
24 5
    public function __construct(
25
        PlanetFieldRepositoryInterface $planetFieldRepository,
26
        ColonyFunctionManagerInterface $colonyFunctionManager,
27
        PlanetFieldHostInterface $host
28
    ) {
29 5
        $this->planetFieldRepository = $planetFieldRepository;
30 5
        $this->colonyFunctionManager = $colonyFunctionManager;
31 5
        $this->host = $host;
32
    }
33
34
    public function updateActualShields(): void
35
    {
36
        if (!$this->host instanceof ColonyInterface) {
37
            return;
38
        }
39
40
        $shieldState = false;
41
        $shields = 0;
42
43
        foreach ($this->host->getPlanetFields() as $field) {
44
            $building = $field->getBuilding();
45
46
            if ($building === null || !$field->isActive()) {
47
                continue;
48
            }
49
50
            $functions = $building->getFunctions();
51
52
            if ($functions->containsKey(BuildingEnum::BUILDING_FUNCTION_SHIELD_GENERATOR)) {
53
                $shields += BuildingEnum::SHIELD_GENERATOR_CAPACITY;
54
                $shieldState = true;
55
            }
56
57
            if ($functions->containsKey(BuildingEnum::BUILDING_FUNCTION_SHIELD_BATTERY)) {
58
                $shields += BuildingEnum::SHIELD_BATTERY_CAPACITY;
59
            }
60
        }
61
62
        if ($shieldState) {
63
            $this->host->setShields(min($this->host->getShields(), $shields));
0 ignored issues
show
Bug introduced by
The method setShields() does not exist on Stu\Lib\Colony\PlanetFieldHostInterface. It seems like you code against a sub-type of Stu\Lib\Colony\PlanetFieldHostInterface such as Stu\Orm\Entity\ColonyInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
            $this->host->/** @scrutinizer ignore-call */ 
64
                         setShields(min($this->host->getShields(), $shields));
Loading history...
Bug introduced by
The method getShields() does not exist on Stu\Lib\Colony\PlanetFieldHostInterface. It seems like you code against a sub-type of Stu\Lib\Colony\PlanetFieldHostInterface such as Stu\Orm\Entity\ColonyInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
            $this->host->setShields(min($this->host->/** @scrutinizer ignore-call */ getShields(), $shields));
Loading history...
64
        }
65
    }
66
67 1
    public function hasShielding(): bool
68
    {
69 1
        return $this->colonyFunctionManager->hasFunction(
70 1
            $this->host,
71 1
            BuildingEnum::BUILDING_FUNCTION_SHIELD_GENERATOR
72 1
        );
73
    }
74
75 1
    public function getMaxShielding(): int
76
    {
77 1
        return $this->planetFieldRepository->getMaxShieldsOfColony($this->host);
78
    }
79
80 3
    public function isShieldingEnabled(): bool
81
    {
82 3
        return $this->colonyFunctionManager->hasActiveFunction($this->host, BuildingEnum::BUILDING_FUNCTION_SHIELD_GENERATOR)
83 3
            && ($this->host instanceof ColonyInterface ? $this->host->getShields() > 0 : true);
84
    }
85
}
86