Passed
Pull Request — master (#1806)
by Nico
53:13 queued 28:49
created

PirateWrathRanking::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 28
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 42
ccs 0
cts 37
cp 0
crap 2
rs 9.472
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Database\View\PirateWrathRanking;
6
7
use Stu\Module\Control\GameControllerInterface;
8
use Stu\Module\Control\ViewControllerInterface;
9
use Stu\Module\Database\Lib\DatabaseUiFactoryInterface;
10
use Stu\Lib\ModuleScreen\GradientColorInterface;
11
use Stu\Orm\Repository\PirateWrathRepositoryInterface;
12
use Stu\Orm\Entity\PirateWrathInterface;
13
use Stu\Orm\Repository\UserRepositoryInterface;
14
15
final class PirateWrathRanking implements ViewControllerInterface
16
{
17
    public const VIEW_IDENTIFIER = 'SHOW_TOP_PIRATE_WRATH';
18
19
    private DatabaseUiFactoryInterface $databaseUiFactory;
20
21
    private UserRepositoryInterface $userRepository;
22
23
    private PirateWrathRepositoryInterface $pirateWrathRepository;
24
25
    private GradientColorInterface $gradientColor;
26
27
    public function __construct(
28
        DatabaseUiFactoryInterface $databaseUiFactory,
29
        PirateWrathRepositoryInterface $pirateWrathRepository,
30
        GradientColorInterface $gradientColor,
31
        UserRepositoryInterface $userRepository
32
    ) {
33
        $this->databaseUiFactory = $databaseUiFactory;
34
        $this->pirateWrathRepository = $pirateWrathRepository;
35
        $this->gradientColor = $gradientColor;
36
        $this->userRepository = $userRepository;
37
    }
38
39
    public function handle(GameControllerInterface $game): void
40
    {
41
        $game->appendNavigationPart(
42
            'database.php',
43
            'Datenbank'
44
        );
45
        $game->appendNavigationPart(
46
            sprintf(
47
                'database.php?%s=1',
48
                static::VIEW_IDENTIFIER
49
            ),
50
            'Die 10 größten Feinde der Kazon'
51
        );
52
        $game->setPageTitle('/ Datenbank / Die 10 größten Feinde der Kazon');
53
        $game->showMacro('html/database.xhtml/top_pirate_wrath_user');
54
        $wrathList = $this->pirateWrathRepository->getPirateWrathTop10();
55
        $wrathData = array_map(
56
            function ($wrathEntity) {
57
                $userId = $wrathEntity->getUser()->getId();
58
                $wrath = $wrathEntity->getWrath();
59
60
                return [
61
                    'user' => $this->userRepository->find($userId),
62
                    'entry' => $this->databaseUiFactory->createDatabaseTopListWithColorGradient(
63
                        $userId,
64
                        $this->gradientColor->calculateGradientColor(
65
                            $wrath,
66
                            500, // Untergrenze
67
                            2000 // Obergrenze
68
                        )
69
                    ),
70
                ];
71
            },
72
            $wrathList
73
        );
74
75
76
        $game->setTemplateVar('PIRATE_WRATH_LIST', $wrathData);
77
78
        $game->setTemplateVar(
79
            'USER_WRATH',
80
            ($game->getUser()->getPirateWrath()?->getWrath() ?? PirateWrathInterface::DEFAULT_WRATH) / 10
81
        );
82
    }
83
}
84