Passed
Push — dev ( 0f4afd...29c427 )
by Janko
25:18 queued 14:48
created

MessengerStyleProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
dl 0
loc 61
ccs 35
cts 40
cp 0.875
rs 10
c 1
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A determineUnreadPmCount() 0 5 1
A setTemplateVariables() 0 23 3
A determineDateString() 0 20 3
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\Module\Control\GameControllerInterface;
7
use Stu\Module\Control\StuTime;
8
use Stu\Module\Game\Lib\View\Provider\ViewComponentProviderInterface;
9
use Stu\Orm\Entity\PrivateMessageInterface;
10
use Stu\Orm\Repository\ContactRepositoryInterface;
11
use Stu\Orm\Repository\PrivateMessageRepositoryInterface;
12
13
class MessengerStyleProvider implements ViewComponentProviderInterface
14
{
15 1
    public function __construct(
16
        private PrivateMessageRepositoryInterface $privateMessageRepository,
17
        private ContactRepositoryInterface $contactRepository,
18
        private StuTime $stuTime
19 1
    ) {}
20
21 1
    public function setTemplateVariables(GameControllerInterface $game): void
22
    {
23 1
        $user = $game->getUser();
24 1
        $messages = $this->privateMessageRepository->getConversations($user);
25 1
        $timestamp = $this->stuTime->time();
26
27 1
        $conversations = [];
28
29 1
        foreach ($messages as $message) {
30 1
            $senderId = $message->getSenderId();
31 1
            if (!array_key_exists($senderId, $conversations)) {
32 1
                $conversations[$senderId] = new Conversation(
33 1
                    $message,
34 1
                    $this->determineUnreadPmCount($message),
35 1
                    $this->determineDateString($message, $timestamp),
36 1
                    $user,
37 1
                    $this->privateMessageRepository,
38 1
                    $this->contactRepository
39 1
                );
40
            }
41
        }
42
43 1
        $game->setTemplateVar('CONVERSATIONS', $conversations);
44
    }
45
46 1
    private function determineUnreadPmCount(PrivateMessageInterface $message): int
47
    {
48 1
        return $this->privateMessageRepository->getNewAmountByFolderAndSender(
49 1
            $message->getCategory(),
50 1
            $message->getSender()
51 1
        );
52
    }
53
54 1
    private function determineDateString(PrivateMessageInterface $message, int $timestamp): string
55
    {
56 1
        $messageTimestamp = $message->getDate();
57 1
        $distanceInSeconds = $timestamp - $messageTimestamp;
58
59 1
        if ($distanceInSeconds < TimeConstants::ONE_DAY_IN_SECONDS) {
60 1
            return date("H:i", $messageTimestamp);
61
        }
62 1
        if ($distanceInSeconds < TimeConstants::SEVEN_DAYS_IN_SECONDS) {
63 1
            return match ((int)date("N", $messageTimestamp)) {
64
                1 => 'Montag',
65
                2 => 'Dienstag',
66
                3 => 'Mittwoch',
67
                4 => 'Donnerstag',
68 1
                5 => 'Freitag',
69
                6 => 'Samstag',
70 1
                7 => 'Sonntag'
71 1
            };
72
        }
73 1
        return $this->stuTime->transformToStuDate($messageTimestamp);
74
    }
75
}
76