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

ShipyardProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setTemplateVariables() 0 27 2
A __construct() 0 8 1
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\BuildableRumpListItemInterface;
8
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
9
use Stu\Module\Control\GameControllerInterface;
10
use Stu\Orm\Entity\ShipRumpInterface;
11
use Stu\Orm\Repository\BuildingFunctionRepositoryInterface;
12
use Stu\Orm\Repository\ShipRumpRepositoryInterface;
13
14
final class ShipyardProvider implements GuiComponentProviderInterface
15
{
16
    private BuildingFunctionRepositoryInterface $buildingFunctionRepository;
17
18
    private ShipRumpRepositoryInterface $shipRumpRepository;
19
20
    private ColonyLibFactoryInterface $colonyLibFactory;
21
22
    public function __construct(
23
        BuildingFunctionRepositoryInterface $buildingFunctionRepository,
24
        ShipRumpRepositoryInterface $shipRumpRepository,
25
        ColonyLibFactoryInterface $colonyLibFactory
26
    ) {
27
        $this->buildingFunctionRepository = $buildingFunctionRepository;
28
        $this->shipRumpRepository = $shipRumpRepository;
29
        $this->colonyLibFactory = $colonyLibFactory;
30
    }
31
32
    public function setTemplateVariables(
33
        PlanetFieldHostInterface $host,
34
        GameControllerInterface $game
35
    ): void {
36
37
        $buildingFunction = $this->buildingFunctionRepository->find(
38
            request::getIntFatal('func')
39
        );
40
41
        if ($buildingFunction === null) {
42
            return;
43
        }
44
45
        $user = $game->getUser();
46
        $userId = $user->getId();
47
48
        $game->setTemplateVar(
49
            'BUILDABLE_SHIPS',
50
            array_map(
51
                fn (ShipRumpInterface $shipRump): BuildableRumpListItemInterface => $this->colonyLibFactory->createBuildableRumpItem(
52
                    $shipRump,
53
                    $user
54
                ),
55
                $this->shipRumpRepository->getBuildableByUserAndBuildingFunction($userId, $buildingFunction->getFunction())
56
            )
57
        );
58
        $game->setTemplateVar('BUILDABLE_RUMPS', $this->shipRumpRepository->getBuildableByUser($userId));
59
    }
60
}
61