Passed
Push — dev ( 25004f...be11ce )
by Janko
16:12
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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A hasShielding() 0 6 1
A isShieldingEnabled() 0 5 3
B updateActualShields() 0 32 8
A getMaxShielding() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Colony\Shields;
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\Building\BuildingEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Building\BuildingEnum 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...
9
use Stu\Component\Building\BuildingFunctionEnum;
10
use Stu\Component\Colony\ColonyFunctionManagerInterface;
11
use Stu\Lib\Colony\PlanetFieldHostInterface;
12
use Stu\Orm\Entity\ColonyInterface;
13
use Stu\Orm\Repository\PlanetFieldRepositoryInterface;
14
15
/**
16
 * Provides shielding related methods
17
 */
18
final class ColonyShieldingManager implements ColonyShieldingManagerInterface
19
{
20 12
    public function __construct(
21
        private PlanetFieldRepositoryInterface $planetFieldRepository,
22
        private ColonyFunctionManagerInterface $colonyFunctionManager,
23
        private PlanetFieldHostInterface $host
24 12
    ) {}
25
26
    #[Override]
27
    public function updateActualShields(): void
28
    {
29
        if (!$this->host instanceof ColonyInterface) {
30
            return;
31
        }
32
33
        $shieldState = false;
34
        $shields = 0;
35
36
        foreach ($this->host->getPlanetFields() as $field) {
37
            $building = $field->getBuilding();
38
39
            if ($building === null || !$field->isActive()) {
40
                continue;
41
            }
42
43
            $functions = $building->getFunctions();
44
45
            if ($functions->containsKey(BuildingFunctionEnum::SHIELD_GENERATOR->value)) {
46
                $shields += BuildingEnum::SHIELD_GENERATOR_CAPACITY;
47
                $shieldState = true;
48
            }
49
50
            if ($functions->containsKey(BuildingFunctionEnum::SHIELD_BATTERY->value)) {
51
                $shields += BuildingEnum::SHIELD_BATTERY_CAPACITY;
52
            }
53
        }
54
55
        if ($shieldState) {
56
            $changeable = $this->host->getChangeable();
0 ignored issues
show
Bug introduced by
The method getChangeable() 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

56
            /** @scrutinizer ignore-call */ 
57
            $changeable = $this->host->getChangeable();
Loading history...
57
            $changeable->setShields(min($changeable->getShields(), $shields));
58
        }
59
    }
60
61 6
    #[Override]
62
    public function hasShielding(): bool
63
    {
64 6
        return $this->colonyFunctionManager->hasFunction(
65 6
            $this->host,
66 6
            BuildingFunctionEnum::SHIELD_GENERATOR
67 6
        );
68
    }
69
70 1
    #[Override]
71
    public function getMaxShielding(): int
72
    {
73 1
        return $this->planetFieldRepository->getMaxShieldsOfHost($this->host);
74
    }
75
76 5
    #[Override]
77
    public function isShieldingEnabled(): bool
78
    {
79 5
        return $this->colonyFunctionManager->hasActiveFunction($this->host, BuildingFunctionEnum::SHIELD_GENERATOR)
80 5
            && ($this->host instanceof ColonyInterface ? $this->host->getChangeable()->getShields() > 0 : true);
81
    }
82
}
83