Passed
Push — master ( 0a354e...300378 )
by Nico
37:07 queued 24:59
created

BuildmenuProvider::setTemplateVariables()   A

Complexity

Conditions 6
Paths 16

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6.0052

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 16
nop 2
dl 0
loc 29
ccs 18
cts 19
cp 0.9474
crap 6.0052
rs 9.0444
c 0
b 0
f 0
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\Lib\Colony\PlanetFieldHostInterface;
10
use Stu\Module\Control\GameControllerInterface;
11
use Stu\Orm\Entity\ColonyInterface;
12
use Stu\Orm\Repository\BuildingRepositoryInterface;
13
use Stu\Orm\Repository\PlanetFieldRepositoryInterface;
14
15
final class BuildmenuProvider implements PlanetFieldHostComponentInterface
16
{
17 1
    public function __construct(private BuildingRepositoryInterface $buildingRepository, private PlanetFieldRepositoryInterface $planetFieldRepository) {}
18
19 1
    #[Override]
20
    public function setTemplateVariables(
21
        $entity,
22
        GameControllerInterface $game
23
    ): void {
24 1
        $colonyClass = $entity instanceof ColonyInterface ? $entity->getColonyClass() : null;
25 1
        $colonyClassId = $colonyClass !== null ? $colonyClass->getId() : null;
26 1
        $fieldType = $this->getFieldType();
27 1
        if ($fieldType !== null) {
28 1
            $game->addExecuteJS(sprintf('fieldType = %d;', $fieldType), GameEnum::JS_EXECUTION_AJAX_UPDATE);
29
        } else {
30
            $game->addExecuteJS('fieldType = null;', GameEnum::JS_EXECUTION_AJAX_UPDATE);
31
        }
32
33 1
        foreach (BuildMenuEnum::BUILDMENU_IDS as $id) {
34
35 1
            $menus[$id]['name'] = BuildMenuEnum::getDescription($id);
36 1
            $menus[$id]['buildings'] = $this->buildingRepository->getBuildmenuBuildings(
37 1
                $entity,
38 1
                $game->getUser()->getId(),
39 1
                $id,
40 1
                0,
41 1
                request::has('cid') ? request::getIntFatal('cid') : null,
42 1
                $fieldType,
43 1
                $colonyClassId
44 1
            );
45
        }
46
47 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 33. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
48
    }
49
50 1
    private function getFieldType(): ?int
51
    {
52 1
        if (!request::has('fid')) {
53
            return null;
54
        }
55
56 1
        $field = $this->planetFieldRepository->find(request::getIntFatal('fid'));
57 1
        if ($field === null) {
58
            return null;
59
        }
60
61 1
        return $field->getFieldType();
62
    }
63
}