Passed
Push — master ( 5a11dc...578008 )
by Janko
08:39
created

BuildmenuProvider::setTemplateVariables()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4.0032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 16
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 27
ccs 16
cts 17
cp 0.9412
crap 4.0032
rs 9.7333
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;
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;
0 ignored issues
show
Bug introduced by
The type Stu\Module\Control\GameControllerInterface 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...
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::cases() as $menu) {
30
31 1
            $id = $menu->value;
32 1
            $menus[$id]['name'] = $menu->getDescription();
33 1
            $menus[$id]['buildings'] = $this->buildingRepository->getBuildmenuBuildings(
34 1
                $entity,
35 1
                $game->getUser()->getId(),
36 1
                $menu,
37 1
                0,
38 1
                request::has('cid') ? request::getIntFatal('cid') : null,
39 1
                $fieldType
40 1
            );
41
        }
42
43 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...
44
    }
45
46 1
    private function getFieldType(): ?int
47
    {
48 1
        if (!request::has('fid')) {
49
            return null;
50
        }
51
52 1
        $field = $this->planetFieldRepository->find(request::getIntFatal('fid'));
53 1
        if ($field === null) {
54
            return null;
55
        }
56
57 1
        return $field->getFieldType();
58
    }
59
}
60