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

MessageProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 5
dl 0
loc 12
ccs 0
cts 6
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 RuntimeException;
9
use Stu\Component\Game\ModuleViewEnum;
10
use Stu\Module\Control\GameControllerInterface;
11
use Stu\Module\Message\Lib\PrivateMessageFolderItem;
12
use Stu\Module\Message\Lib\PrivateMessageFolderSpecialEnum;
13
use Stu\Module\Message\Lib\PrivateMessageListItem;
14
use Stu\Module\Message\Lib\PrivateMessageUiFactoryInterface;
15
use Stu\Orm\Entity\PrivateMessageFolderInterface;
16
use Stu\Orm\Entity\PrivateMessageInterface;
17
use Stu\Orm\Repository\ContactRepositoryInterface;
18
use Stu\Orm\Repository\IgnoreListRepositoryInterface;
19
use Stu\Orm\Repository\PrivateMessageFolderRepositoryInterface;
20
use Stu\Orm\Repository\PrivateMessageRepositoryInterface;
21
22
final class MessageProvider implements ViewComponentProviderInterface
23
{
24
    private const PMLIMITER = 6;
25
26
    private PrivateMessageFolderRepositoryInterface $privateMessageFolderRepository;
27
28
    private PrivateMessageRepositoryInterface $privateMessageRepository;
29
30
    private IgnoreListRepositoryInterface $ignoreListRepository;
31
32
    private ContactRepositoryInterface $contactRepository;
33
34
    private PrivateMessageUiFactoryInterface $privateMessageUiFactory;
35
36
    public function __construct(
37
        PrivateMessageFolderRepositoryInterface $privateMessageFolderRepository,
38
        PrivateMessageRepositoryInterface $privateMessageRepository,
39
        IgnoreListRepositoryInterface $ignoreListRepository,
40
        PrivateMessageUiFactoryInterface $privateMessageUiFactory,
41
        ContactRepositoryInterface $contactRepository
42
    ) {
43
        $this->privateMessageFolderRepository = $privateMessageFolderRepository;
44
        $this->privateMessageRepository = $privateMessageRepository;
45
        $this->ignoreListRepository = $ignoreListRepository;
46
        $this->contactRepository = $contactRepository;
47
        $this->privateMessageUiFactory = $privateMessageUiFactory;
48
    }
49
50
    public function setTemplateVariables(GameControllerInterface $game): void
51
    {
52
        $userId = $game->getUser()->getId();
53
        $categoryId = request::indInt('pmcat');
54
55
        $mark = request::indInt('mark');
56
57
        if ($categoryId === 0) {
58
            $category = $this->privateMessageFolderRepository->getByUserAndSpecial(
59
                $userId,
60
                PrivateMessageFolderSpecialEnum::PM_SPECIAL_MAIN
61
            );
62
        } else {
63
            $category = $this->privateMessageFolderRepository->find($categoryId);
64
            if ($category === null || $category->getUserId() !== $userId || $category->isDeleted()) {
65
                $category = $this->privateMessageFolderRepository->getByUserAndSpecial(
66
                    $userId,
67
                    PrivateMessageFolderSpecialEnum::PM_SPECIAL_MAIN
68
                );
69
            }
70
        }
71
72
        if ($category === null) {
73
            throw new RuntimeException('this should not happen');
74
        }
75
76
        if ($mark % static::PMLIMITER != 0 || $mark < 0) {
77
            $mark = 0;
78
        }
79
        $maxcount = $this->privateMessageRepository->getAmountByFolder($category);
80
        $maxpage = ceil($maxcount / static::PMLIMITER);
81
        $curpage = floor($mark / static::PMLIMITER);
82
        $pmNavigation = [];
83
        if ($curpage != 0) {
84
            $pmNavigation[] = ["page" => "<<", "mark" => 0, "cssclass" => "pages"];
85
            $pmNavigation[] = ["page" => "<", "mark" => ($mark - static::PMLIMITER), "cssclass" => "pages"];
86
        }
87
        for ($i = $curpage - 1; $i <= $curpage + 3; $i++) {
88
            if ($i > $maxpage || $i < 1) {
89
                continue;
90
            }
91
            $pmNavigation[] = [
92
                "page" => $i,
93
                "mark" => ($i * static::PMLIMITER - static::PMLIMITER),
94
                "cssclass" => ($curpage + 1 === $i ? "pages selected" : "pages")
95
            ];
96
        }
97
        if ($curpage + 1 !== $maxpage) {
98
            $pmNavigation[] = ["page" => ">", "mark" => ($mark + static::PMLIMITER), "cssclass" => "pages"];
99
            $pmNavigation[] = ["page" => ">>", "mark" => $maxpage * static::PMLIMITER - static::PMLIMITER, "cssclass" => "pages"];
100
        }
101
102
        $game->appendNavigationPart(
103
            sprintf('%s?pmcat=%d', ModuleViewEnum::PM->getPhpPage(), $category->getId()),
104
            sprintf(_('Ordner: %s'), $category->getDescription())
105
        );
106
107
        $game->setTemplateVar('CATEGORY', $category);
108
        $game->setTemplateVar(
109
            'PM_LIST',
110
            array_map(
111
                fn (PrivateMessageInterface $message): PrivateMessageListItem => new PrivateMessageListItem(
112
                    $this->privateMessageRepository,
113
                    $this->contactRepository,
114
                    $this->ignoreListRepository,
115
                    $message,
116
                    $userId
117
                ),
118
                $this->privateMessageRepository->getByUserAndFolder(
119
                    $userId,
120
                    $category->getId(),
121
                    $mark,
122
                    static::PMLIMITER
123
                )
124
            )
125
        );
126
        $game->setTemplateVar('PM_NAVIGATION', $pmNavigation);
127
        $game->setTemplateVar(
128
            'PM_CATEGORIES',
129
            array_map(
130
                fn (PrivateMessageFolderInterface $folder): PrivateMessageFolderItem =>
131
                $this->privateMessageUiFactory->createPrivateMessageFolderItem($folder),
132
                $this->privateMessageFolderRepository->getOrderedByUser($userId)
133
            )
134
        );
135
    }
136
}
137