Passed
Push — dev ( 242045...34d7fd )
by Janko
18:14 queued 01:44
created

BuildmenuProvider::setTemplateVariables()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4.0039

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 15
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 26
ccs 15
cts 16
cp 0.9375
crap 4.0039
rs 9.7666
1
<?php
2
3
namespace Stu\Module\Colony\Lib\Gui\Component;
4
5
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...
6
use request;
7
use Stu\Component\Building\BuildMenuEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Building\BuildMenuEnum 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\Game\GameEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Game\GameEnum 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\Module\Control\GameControllerInterface;
10
use Stu\Orm\Repository\BuildingRepositoryInterface;
11
use Stu\Orm\Repository\PlanetFieldRepositoryInterface;
12
13
final class BuildmenuProvider implements PlanetFieldHostComponentInterface
14
{
15 1
    public function __construct(private BuildingRepositoryInterface $buildingRepository, private PlanetFieldRepositoryInterface $planetFieldRepository) {}
16
17 1
    #[Override]
18
    public function setTemplateVariables(
19
        $entity,
20
        GameControllerInterface $game
21
    ): void {
22 1
        $fieldType = $this->getFieldType();
23 1
        if ($fieldType !== null) {
24 1
            $game->addExecuteJS(sprintf('fieldType = %d;', $fieldType), GameEnum::JS_EXECUTION_AJAX_UPDATE);
25
        } else {
26
            $game->addExecuteJS('fieldType = null;', GameEnum::JS_EXECUTION_AJAX_UPDATE);
27
        }
28
29 1
        foreach (BuildMenuEnum::BUILDMENU_IDS as $id) {
30
31 1
            $menus[$id]['name'] = BuildMenuEnum::getDescription($id);
32 1
            $menus[$id]['buildings'] = $this->buildingRepository->getBuildmenuBuildings(
33 1
                $entity,
34 1
                $game->getUser()->getId(),
35 1
                $id,
36 1
                0,
37 1
                request::has('cid') ? request::getIntFatal('cid') : null,
38 1
                $fieldType
39 1
            );
40
        }
41
42 1
        $game->setTemplateVar('BUILD_MENUS', $menus);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $menus seems to be defined by a foreach iteration on line 29. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
43
    }
44
45 1
    private function getFieldType(): ?int
46
    {
47 1
        if (!request::has('fid')) {
48
            return null;
49
        }
50
51 1
        $field = $this->planetFieldRepository->find(request::getIntFatal('fid'));
52 1
        if ($field === null) {
53
            return null;
54
        }
55
56 1
        return $field->getFieldType();
57
    }
58
}
59