Passed
Pull Request — master (#1699)
by Nico
43:19 queued 18:45
created

ShipListProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 10
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Game\Lib\View\Provider;
6
7
use Stu\Component\Game\GameEnum;
8
use Stu\Component\Ship\SpacecraftTypeEnum;
9
use Stu\Lib\SessionInterface;
10
use Stu\Module\Control\GameControllerInterface;
11
use Stu\Module\Game\Lib\View\Provider\ViewComponentProviderInterface;
12
use Stu\Module\Ship\Lib\ShipWrapperFactoryInterface;
13
use Stu\Orm\Repository\FleetRepositoryInterface;
14
use Stu\Orm\Repository\ShipRepositoryInterface;
15
16
final class ShipListProvider implements ViewComponentProviderInterface
17
{
18
    private FleetRepositoryInterface $fleetRepository;
19
20
    private ShipRepositoryInterface $shipRepository;
21
22
    private ShipWrapperFactoryInterface $shipWrapperFactory;
23
24
    private SessionInterface $session;
25
26
    public function __construct(
27
        FleetRepositoryInterface $fleetRepository,
28
        ShipRepositoryInterface $shipRepository,
29
        ShipWrapperFactoryInterface $shipWrapperFactory,
30
        SessionInterface $session
31
    ) {
32
        $this->fleetRepository = $fleetRepository;
33
        $this->shipRepository = $shipRepository;
34
        $this->shipWrapperFactory = $shipWrapperFactory;
35
        $this->session = $session;
36
    }
37
38
    public function setTemplateVariables(GameControllerInterface $game): void
39
    {
40
        $userId = $game->getUser()->getId();
41
42
        $fleets = $this->fleetRepository->getByUser($userId);
43
        $ships = $this->shipRepository->getByUserAndFleetAndType($userId, null, SpacecraftTypeEnum::SPACECRAFT_TYPE_SHIP);
44
45
        foreach ($fleets as $fleet) {
46
            $fleet->setHiddenStyle($this->session->hasSessionValue('hiddenshiplistfleets', $fleet->getId()) ? 'display: none' : '');
47
        }
48
49
        $game->setTemplateVar('MAX_CREW_PER_FLEET', GameEnum::CREW_PER_FLEET);
50
        $game->setTemplateVar('SHIPS_AVAILABLE', $fleets !== [] || $ships !== []);
51
        $game->setTemplateVar('FLEETWRAPPERS', $this->shipWrapperFactory->wrapFleets($fleets));
52
        $game->setTemplateVar('SINGLESHIPWRAPPERS', $this->shipWrapperFactory->wrapShips($ships));
53
    }
54
}
55