Passed
Pull Request — master (#1696)
by Nico
49:43 queued 22:34
created

ShipBuildplansProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 41
ccs 0
cts 20
cp 0
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A setTemplateVariables() 0 23 2
1
<?php
2
3
namespace Stu\Module\Colony\Lib\Gui\Component;
4
5
use request;
6
use Stu\Lib\Colony\PlanetFieldHostInterface;
7
use Stu\Module\Colony\Lib\BuildPlanDeleterInterface;
8
use Stu\Module\Control\GameControllerInterface;
9
use Stu\Orm\Entity\ShipBuildplanInterface;
10
use Stu\Orm\Repository\BuildingFunctionRepositoryInterface;
11
use Stu\Orm\Repository\ShipBuildplanRepositoryInterface;
12
13
final class ShipBuildplansProvider implements GuiComponentProviderInterface
14
{
15
    private BuildingFunctionRepositoryInterface $buildingFunctionRepository;
16
17
    private ShipBuildplanRepositoryInterface $shipBuildplanRepository;
18
19
    private BuildPlanDeleterInterface $buildPlanDeleter;
20
21
    public function __construct(
22
        BuildPlanDeleterInterface $buildPlanDeleter,
23
        BuildingFunctionRepositoryInterface $buildingFunctionRepository,
24
        ShipBuildplanRepositoryInterface $shipBuildplanRepository
25
    ) {
26
        $this->buildingFunctionRepository = $buildingFunctionRepository;
27
        $this->shipBuildplanRepository = $shipBuildplanRepository;
28
        $this->buildPlanDeleter = $buildPlanDeleter;
29
    }
30
31
    public function setTemplateVariables(
32
        PlanetFieldHostInterface $host,
33
        GameControllerInterface $game
34
    ): void {
35
36
        $buildingFunction = $this->buildingFunctionRepository->find(
37
            request::getIntFatal('func')
38
        );
39
40
        if ($buildingFunction === null) {
41
            return;
42
        }
43
44
        $game->setTemplateVar(
45
            'AVAILABLE_BUILDPLANS',
46
            array_map(
47
                fn (ShipBuildplanInterface $plan): array => [
48
                    'plan' => $plan,
49
                    'deletable' => $this->buildPlanDeleter->isDeletable($plan)
50
                ],
51
                $this->shipBuildplanRepository->getByUserAndBuildingFunction(
52
                    $game->getUser()->getId(),
53
                    $buildingFunction->getFunction()
54
                )
55
            )
56
        );
57
    }
58
}
59