Passed
Push — master ( f50aa1...d726cb )
by Nico
57:47 queued 28:23
created

SelectedTech::getDistinctPositiveDependencyNames()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 2
nop 0
dl 0
loc 24
ccs 0
cts 16
cp 0
crap 20
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Research;
6
7
use Noodlehaus\ConfigInterface;
8
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...
9
use RuntimeException;
10
use Stu\Module\Template\StatusBarColorEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\Template\StatusBarColorEnum 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...
11
use Stu\Module\Template\StatusBar;
12
use Stu\Orm\Entity\ResearchDependencyInterface;
13
use Stu\Orm\Entity\ResearchedInterface;
14
use Stu\Orm\Entity\ResearchInterface;
15
use Stu\Orm\Entity\UserInterface;
16
use Stu\Orm\Repository\BuildingRepositoryInterface;
17
use Stu\Orm\Repository\ResearchDependencyRepositoryInterface;
18
use Stu\Orm\Repository\ResearchedRepositoryInterface;
19
use Stu\Orm\Repository\ResearchRepositoryInterface;
20
21
final class SelectedTech implements SelectedTechInterface
22
{
23
    private ?ResearchedInterface $state = null;
24
25
    /** @var null|array<string, TechDependency> */
26
    private ?array $excludes = null;
27
28
    /** @var null|array<string, TechDependency> */
29
    private ?array $dependencies = null;
30
31
    public function __construct(private ResearchRepositoryInterface $researchRepository, private ResearchedRepositoryInterface $researchedRepository, private ResearchDependencyRepositoryInterface $researchDependencyRepository, private BuildingRepositoryInterface $buildingRepository, private ResearchInterface $research, private UserInterface $currentUser, private ConfigInterface $config)
32
    {
33
    }
34
35
    #[Override]
36
    public function getResearch(): ResearchInterface
37
    {
38
        return $this->research;
39
    }
40
41
    #[Override]
42
    public function getResearchState(): ?ResearchedInterface
43
    {
44
        if ($this->state === null) {
45
            $this->state = $this->researchedRepository->getFor(
46
                $this->research->getId(),
47
                $this->currentUser->getId()
48
            );
49
        }
50
        return $this->state;
51
    }
52
53
    #[Override]
54
    public function getDistinctExcludeNames(): array
55
    {
56
        if ($this->excludes === null) {
57
            $result = [];
58
59
            $techList = $this->researchDependencyRepository->getExcludesByResearch($this->research->getId());
60
61
            array_walk(
62
                $techList,
63
                function (ResearchDependencyInterface $dependecy) use (&$result): void {
64
                    $name = $dependecy->getResearchDependOn()->getName();
65
66
                    if (!array_key_exists($name, $result) && $name !== $this->research->getName()) {
67
                        $result[$name] = new TechDependency($name, $dependecy->getResearchDependOn()->getCommodity());
68
                    }
69
                }
70
            );
71
72
            $this->excludes = $result;
73
        }
74
        return $this->excludes;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->excludes could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
75
    }
76
77
    #[Override]
78
    public function hasExcludes(): bool
79
    {
80
        return $this->getDistinctExcludeNames() !== [];
81
    }
82
83
    #[Override]
84
    public function getDistinctPositiveDependencyNames(): array
85
    {
86
        if ($this->dependencies === null) {
87
            $result = [];
88
89
            $techList = $this->researchRepository->getPossibleResearchByParent(
90
                $this->research->getId()
91
            );
92
93
            array_walk(
94
                $techList,
95
                function (ResearchInterface $research) use (&$result): void {
96
                    $name = $research->getName();
97
98
                    if (!array_key_exists($name, $result) && $name !== $this->research->getName()) {
99
                        $result[$name] = new TechDependency($name, $research->getCommodity());
100
                    }
101
                }
102
            );
103
104
            $this->dependencies = $result;
105
        }
106
        return $this->dependencies;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->dependencies could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
107
    }
108
109
    #[Override]
110
    public function hasPositiveDependencies(): bool
111
    {
112
        return $this->getDistinctPositiveDependencyNames() !== [];
113
    }
114
115
    #[Override]
116
    public function getDonePoints(): int
117
    {
118
        $researchState = $this->getResearchState();
119
120
        return $researchState === null
121
            ? $this->getPoints()
122
            : $this->getPoints() - $researchState->getActive();
123
    }
124
125
    private function getPoints(): int
126
    {
127
        return $this->research->getPoints();
128
    }
129
130
    #[Override]
131
    public function isResearchFinished(): bool
132
    {
133
        $researchState = $this->getResearchState();
134
135
        return $researchState === null
136
            ? false
137
            : $researchState->getFinished() > 0;
138
    }
139
140
    #[Override]
141
    public function getBuildings(): array
142
    {
143
        return $this->buildingRepository->getByResearch($this->research);
144
    }
145
146
    #[Override]
147
    public function getStatusBar(): string
148
    {
149
        $researchState = $this->getResearchState();
150
        if ($researchState === null) {
151
            throw new RuntimeException('can not call when no researchState present');
152
        }
153
154
        return (new StatusBar())
155
            ->setColor(StatusBarColorEnum::STATUSBAR_BLUE)
156
            ->setLabel(_('Forschung'))
157
            ->setMaxValue($this->research->getPoints())
158
            ->setValue($this->research->getPoints() - $researchState->getActive())
159
            ->setSizeModifier(2)
160
            ->render();
161
    }
162
163
    #[Override]
164
    public function getWikiLink(): string
165
    {
166
        return sprintf(
167
            '%s/index.php?title=Forschung:%s',
168
            $this->config->get('wiki.base_url'),
169
            str_replace(' ', '_', $this->research->getName())
170
        );
171
    }
172
}
173