Passed
Push — dev ( c2ee06...dfea32 )
by Nico
05:29
created

MessengerStyleProvider::setTemplateVariables()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 6.0622

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 6
eloc 24
c 1
b 1
f 0
nc 6
nop 1
dl 0
loc 36
ccs 22
cts 25
cp 0.88
crap 6.0622
rs 8.9137
1
<?php
2
3
namespace Stu\Module\Game\Lib\View\Provider\Message;
4
5
use Stu\Component\Game\TimeConstants;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Game\TimeConstants was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Stu\Component\Player\Settings\UserSettingsProviderInterface;
7
use Stu\Module\Control\GameControllerInterface;
8
use Stu\Module\Control\StuTime;
9
use Stu\Module\Game\Lib\View\Provider\ViewComponentProviderInterface;
10
use Stu\Orm\Entity\PrivateMessage;
11
use Stu\Orm\Repository\ContactRepositoryInterface;
12
use Stu\Orm\Repository\PrivateMessageRepositoryInterface;
13
14
class MessengerStyleProvider implements ViewComponentProviderInterface
15
{
16 1
    public function __construct(
17
        private readonly PrivateMessageRepositoryInterface $privateMessageRepository,
18
        private readonly ContactRepositoryInterface $contactRepository,
19
        private readonly UserSettingsProviderInterface $userSettingsProvider,
20
        private readonly StuTime $stuTime
21 1
    ) {}
22
23 1
    public function setTemplateVariables(GameControllerInterface $game): void
24
    {
25 1
        $user = $game->getUser();
26 1
        $messages = $this->privateMessageRepository->getConversations($user);
27 1
        $timestamp = $this->stuTime->time();
28
29 1
        $conversations = [];
30
31 1
        foreach ($messages as $message) {
32 1
            $sender = $message->getSender();
33 1
            $senderId = $sender->getId();
34 1
            $formerSenderId = $message->getFormerSendUser();
35
36 1
            if ($senderId === 1) {
37
                if ($formerSenderId === null || $formerSenderId === 1) {
38
                    continue;
39
                }
40
41
                $groupId = $formerSenderId;
42
            } else {
43 1
                $groupId = $senderId;
44
            }
45 1
            if (!array_key_exists($groupId, $conversations)) {
46 1
                $conversations[$groupId] = new Conversation(
0 ignored issues
show
Bug introduced by
The type Stu\Module\Game\Lib\View...er\Message\Conversation was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
47 1
                    $message,
48 1
                    $this->determineUnreadPmCount($message),
49 1
                    $this->determineDateString($message, $timestamp),
50 1
                    $user,
51 1
                    $this->privateMessageRepository,
52 1
                    $this->contactRepository,
53 1
                    $this->userSettingsProvider
54 1
                );
55
            }
56
        }
57
58 1
        $game->setTemplateVar('CONVERSATIONS', $conversations);
59
    }
60
61 1
    private function determineUnreadPmCount(PrivateMessage $message): int
62
    {
63 1
        return $this->privateMessageRepository->getNewAmountByFolderAndSender(
64 1
            $message->getCategory(),
65 1
            $message->getSender()
66 1
        );
67
    }
68
69 1
    private function determineDateString(PrivateMessage $message, int $timestamp): string
70
    {
71 1
        $messageTimestamp = $message->getDate();
72 1
        $distanceInSeconds = $timestamp - $messageTimestamp;
73
74 1
        if ($distanceInSeconds < TimeConstants::ONE_DAY_IN_SECONDS) {
75 1
            return date("H:i", $messageTimestamp);
76
        }
77 1
        if ($distanceInSeconds < TimeConstants::SEVEN_DAYS_IN_SECONDS) {
78 1
            return match ((int)date("N", $messageTimestamp)) {
79
                1 => 'Montag',
80
                2 => 'Dienstag',
81
                3 => 'Mittwoch',
82
                4 => 'Donnerstag',
83 1
                5 => 'Freitag',
84
                6 => 'Samstag',
85 1
                7 => 'Sonntag'
86 1
            };
87
        }
88 1
        return $this->stuTime->transformToStuDate($messageTimestamp);
89
    }
90
}