Passed
Push — dev ( 4c76dc...4abd48 )
by Janko
09:59
created

MessengerStyleProvider::setTemplateVariables()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 15
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 23
ccs 0
cts 17
cp 0
crap 12
rs 9.7666
1
<?php
2
3
namespace Stu\Module\Game\Lib\View\Provider\Message;
4
5
use Stu\Module\Control\GameControllerInterface;
6
use Stu\Module\Control\StuTime;
7
use Stu\Module\Game\Lib\View\Provider\ViewComponentProviderInterface;
8
use Stu\Orm\Repository\ContactRepositoryInterface;
9
use Stu\Orm\Repository\PrivateMessageRepositoryInterface;
10
11
class MessengerStyleProvider implements ViewComponentProviderInterface
12
{
13 1
    public function __construct(
14
        private PrivateMessageRepositoryInterface $privateMessageRepository,
15
        private ContactRepositoryInterface $contactRepository,
16
        private StuTime $stuTime
17 1
    ) {}
18
19
    public function setTemplateVariables(GameControllerInterface $game): void
20
    {
21
        $user = $game->getUser();
22
        $messages = $this->privateMessageRepository->getConversations($user);
23
        $timestamp = $this->stuTime->time();
24
25
        $conversations = [];
26
27
        foreach ($messages as $message) {
28
            $senderId = $message->getSenderId();
29
            if (!array_key_exists($senderId, $conversations)) {
30
                $conversations[$senderId] = new Conversation(
31
                    $message,
32
                    $timestamp,
33
                    $this->stuTime,
34
                    $user->getId(),
35
                    $this->privateMessageRepository,
36
                    $this->contactRepository
37
                );
38
            }
39
        }
40
41
        $game->setTemplateVar('CONVERSATIONS', $conversations);
42
    }
43
}
44