Passed
Push — master ( fcb0ff...ac0f12 )
by Nico
32:37 queued 12:14
created

ResearchRanking::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Database\View\ResearchRanking;
6
7
use Stu\Module\Control\GameControllerInterface;
8
use Stu\Module\Control\ViewControllerInterface;
9
use Stu\Module\Database\Lib\DatabaseTopListWithPoints;
10
use Stu\Module\Database\Lib\DatabaseUiFactoryInterface;
11
use Stu\Orm\Repository\ResearchedRepositoryInterface;
12
13
final class ResearchRanking implements ViewControllerInterface
14
{
15
    public const VIEW_IDENTIFIER = 'SHOW_TOP_RESEARCH';
16
17
    private ResearchedRepositoryInterface $researchedRepository;
18
19
    private DatabaseUiFactoryInterface $databaseUiFactory;
20
21
    public function __construct(
22
        ResearchedRepositoryInterface $researchedRepository,
23
        DatabaseUiFactoryInterface $databaseUiFactory
24
    ) {
25
        $this->researchedRepository = $researchedRepository;
26
        $this->databaseUiFactory = $databaseUiFactory;
27
    }
28
29
    public function handle(GameControllerInterface $game): void
30
    {
31
        $game->appendNavigationPart(
32
            'database.php',
33
            'Datenbank'
34
        );
35
        $game->appendNavigationPart(
36
            sprintf(
37
                'database.php?%s=1',
38
                static::VIEW_IDENTIFIER
39
            ),
40
            'Die 10 besten Forscher'
41
        );
42
        $game->setPageTitle('/ Datenbank / Die 10 besten Forscher');
43
        $game->showMacro('html/database.xhtml/top_research_user');
44
45
        $userPoints = 0;
46
        $list = $this->researchedRepository->getResearchedPoints();
47
        foreach ($list as $data) {
48
            if ($data['user_id'] == $game->getUser()->getId()) {
49
                $userPoints = (int)$data['points'];
50
            }
51
        }
52
        $game->setTemplateVar('USER_POINTS',  $userPoints);
53
54
        $game->setTemplateVar(
55
            'RESEARCH_LIST',
56
            array_map(
57
                fn (array $data): DatabaseTopListWithPoints => $this->databaseUiFactory->createDatabaseTopListWithPoints($data['user_id'], (string) $data['points']),
58
                array_slice($list, 0, 10)
59
            )
60
        );
61
    }
62
}
63