Passed
Push — master ( 13d157...69410e )
by Nico
46:03 queued 25:13
created

TechlistRetriever::getResearchedList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Research;
6
7
use Stu\Component\Research\ResearchEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Research\ResearchEnum 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\Orm\Entity\ResearchedInterface;
9
use Stu\Orm\Entity\UserInterface;
10
use Stu\Orm\Repository\FactionRepositoryInterface;
11
use Stu\Orm\Repository\ResearchDependencyRepositoryInterface;
12
use Stu\Orm\Repository\ResearchedRepositoryInterface;
13
use Stu\Orm\Repository\ResearchRepositoryInterface;
14
15
final class TechlistRetriever implements TechlistRetrieverInterface
16
{
17
    private ResearchRepositoryInterface $researchRepository;
18
19
    private ResearchDependencyRepositoryInterface $researchDependencyRepository;
20
21
    private ResearchedRepositoryInterface $researchedRepository;
22
23
    private FactionRepositoryInterface $factionRepository;
24
25
    public function __construct(
26
        ResearchRepositoryInterface $researchRepository,
27
        ResearchDependencyRepositoryInterface $researchDependencyRepository,
28
        ResearchedRepositoryInterface $researchedRepository,
29
        FactionRepositoryInterface $factionRepository
30
    ) {
31
        $this->researchRepository = $researchRepository;
32
        $this->researchDependencyRepository = $researchDependencyRepository;
33
        $this->researchedRepository = $researchedRepository;
34
        $this->factionRepository = $factionRepository;
35
    }
36
37
    public function getResearchList(UserInterface $user): array
38
    {
39
        $finished_list = array_map(
40
            fn (ResearchedInterface $researched): int => $researched->getResearch()->getId(),
41
            $this->getResearchedList($user)
42
        );
43
44
        $result = $this->researchRepository->getAvailableResearch($user->getId());
45
        $list_result = [];
46
47
        //load dependencies
48
        $dependencies = $this->loadDependencies();
49
50
        //load excludes
51
        $excludes = $this->loadExcludes();
52
53
        // calculate possible research items
54
        foreach ($result as $obj) {
55
            // check for existent user award
56
            if ($obj->getNeededAwardId() !== null && !$user->hasAward($obj->getNeededAwardId())) {
57
                continue;
58
            }
59
60
            $key = $obj->getId();
61
62
            // excludelogic
63
            if (isset($excludes[$key])) {
64
                foreach ($excludes[$key] as $exclude) {
65
                    if (in_array($exclude->getResearchId(), $finished_list)) {
66
                        continue 2;
67
                    }
68
                }
69
            }
70
71
            // dependencie logic
72
            if (isset($dependencies[$key])) {
73
                // check for AND condition
74
                foreach ($dependencies[$key]['AND'] as $and_condition) {
75
                    if (!in_array($and_condition, $finished_list)) {
76
                        continue 2;
77
                    }
78
                }
79
80
                // check for OR condition
81
                if (!empty($dependencies[$key]['OR'])) {
82
                    $or_condition_met = false;
83
                    foreach ($dependencies[$key]['OR'] as $or_condition) {
84
                        if (in_array($or_condition, $finished_list)) {
85
                            $or_condition_met = true;
86
                            break;
87
                        }
88
                    }
89
                    if (!$or_condition_met) {
90
                        continue;
91
                    }
92
                }
93
            }
94
95
            $list_result[$key] = $obj;
96
        }
97
98
99
        foreach ($this->factionRepository->findAll() as $faction) {
100
            $startResearch = $faction->getStartResearch();
101
            if ($startResearch !== null) {
102
                $startResearchId = $startResearch->getId();
103
                if (isset($list_result[$startResearchId])) {
104
                    unset($list_result[$startResearchId]);
105
                }
106
            }
107
        }
108
109
        return $list_result;
110
    }
111
112
    private function loadDependencies(): array
113
    {
114
        $dependencies = [];
115
116
        $dependencies_result = $this->researchDependencyRepository->getByMode(
117
            [ResearchEnum::RESEARCH_MODE_REQUIRE, ResearchEnum::RESEARCH_MODE_REQUIRE_SOME]
118
        );
119
120
        foreach ($dependencies_result as $dependency) {
121
            $research_id = $dependency->getResearchId();
122
            $mode = $dependency->getMode();
123
124
            if (!isset($dependencies[$research_id])) {
125
                $dependencies[$research_id] = [
126
                    'AND' => [],
127
                    'OR' => []
128
                ];
129
            }
130
131
            if ($mode === ResearchEnum::RESEARCH_MODE_REQUIRE) {
132
                $dependencies[$research_id]['AND'][] = $dependency->getDependsOn();
133
            } elseif ($mode === ResearchEnum::RESEARCH_MODE_REQUIRE_SOME) {
134
                $dependencies[$research_id]['OR'][] = $dependency->getDependsOn();
135
            }
136
        }
137
138
        return $dependencies;
139
    }
140
141
    private function loadExcludes(): array
142
    {
143
        $excludes = [];
144
        $exclude_result = $this->researchDependencyRepository->getByMode([ResearchEnum::RESEARCH_MODE_EXCLUDE]);
145
146
        foreach ($exclude_result as $dependency) {
147
            $research_id = $dependency->getDependsOn();
148
            if (array_key_exists($research_id, $excludes) === false) {
149
                $excludes[$research_id] = [];
150
            }
151
            $excludes[$research_id][] = $dependency;
152
        }
153
154
        return $excludes;
155
    }
156
157
    public function getResearchedList(UserInterface $user): array
158
    {
159
        return $this->researchedRepository->getListByUser($user->getId());
160
    }
161
}
162