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

HistoryProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 2
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 request;
8
use Stu\Component\History\HistoryTypeEnum;
9
use Stu\Module\Control\GameControllerInterface;
10
use Stu\Orm\Repository\HistoryRepositoryInterface;
11
12
final class HistoryProvider implements ViewComponentProviderInterface
13
{
14
    private const MAX_LIMIT = 10000;
15
16
    private const LIMIT = 50;
17
18
    private HistoryRepositoryInterface $historyRepository;
19
20
    public function __construct(
21
        HistoryRepositoryInterface $historyRepository
22
    ) {
23
        $this->historyRepository = $historyRepository;
24
    }
25
26
    public function setTemplateVariables(GameControllerInterface $game): void
27
    {
28
        $type = HistoryTypeEnum::tryFrom(request::indInt('htype')) ?? HistoryTypeEnum::SHIP;
29
        $count = request::indInt('hcount');
30
        if (!$count) {
31
            $count = self::LIMIT;
32
        }
33
        $search = request::indString('hsearch');
34
35
        if ($count < 1 || $count > self::MAX_LIMIT) {
36
            $count = self::MAX_LIMIT;
37
        }
38
39
        $history_types = [];
40
        foreach (HistoryTypeEnum::cases() as $enum) {
41
            $key = $enum->value;
42
            $history_types[$key]['type'] = $enum;
43
            $history_types[$key]['class'] = $enum == $type ? 'selected' : '';
44
            $history_types[$key]['count'] = $this->historyRepository->getAmountByType($key);
45
        }
46
47
        $game->setTemplateVar(
48
            'HISTORY_TYPE',
49
            $type
50
        );
51
        $game->setTemplateVar(
52
            'HISTORY_TYPE_COUNT',
53
            count($history_types)
54
        );
55
        $game->setTemplateVar(
56
            'HISTORY_TYPES',
57
            $history_types
58
        );
59
        $game->setTemplateVar(
60
            'HISTORY_COUNT',
61
            $count
62
        );
63
        $game->setTemplateVar(
64
            'HISTORY_SEARCH',
65
            $search ?: ''
0 ignored issues
show
introduced by
The condition $search is always false.
Loading history...
66
        );
67
        $game->setTemplateVar(
68
            'HISTORY',
69
            $this->historyRepository->getByTypeAndSearch($type, $count, $search)
70
        );
71
    }
72
}
73