Test Failed
Pull Request — dev (#1952)
by Janko
02:59
created

MessengerStyleProvider::setTemplateVariables()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3.0593

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 16
nc 3
nop 1
dl 0
loc 24
ccs 13
cts 16
cp 0.8125
crap 3.0593
rs 9.7333
c 1
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
The type Stu\Module\Control\GameControllerInterface 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...
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
            $senderId = $message->getSenderId();
33 1
            if (!array_key_exists($senderId, $conversations)) {
34 1
                $conversations[$senderId] = 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...
35
                    $message,
36 1
                    $this->determineUnreadPmCount($message),
37
                    $this->determineDateString($message, $timestamp),
38
                    $user,
39
                    $this->privateMessageRepository,
40
                    $this->contactRepository,
41
                    $this->userSettingsProvider
42
                );
43 1
            }
44
        }
45 1
46 1
        $game->setTemplateVar('CONVERSATIONS', $conversations);
47 1
    }
48 1
49 1
    private function determineUnreadPmCount(PrivateMessage $message): int
50 1
    {
51 1
        return $this->privateMessageRepository->getNewAmountByFolderAndSender(
52 1
            $message->getCategory(),
53 1
            $message->getSender()
54 1
        );
55
    }
56
57
    private function determineDateString(PrivateMessage $message, int $timestamp): string
58 1
    {
59
        $messageTimestamp = $message->getDate();
60
        $distanceInSeconds = $timestamp - $messageTimestamp;
61 1
62
        if ($distanceInSeconds < TimeConstants::ONE_DAY_IN_SECONDS) {
63 1
            return date("H:i", $messageTimestamp);
64 1
        }
65 1
        if ($distanceInSeconds < TimeConstants::SEVEN_DAYS_IN_SECONDS) {
66 1
            return match ((int)date("N", $messageTimestamp)) {
67
                1 => 'Montag',
68
                2 => 'Dienstag',
69 1
                3 => 'Mittwoch',
70
                4 => 'Donnerstag',
71 1
                5 => 'Freitag',
72 1
                6 => 'Samstag',
73
                7 => 'Sonntag'
74 1
            };
75 1
        }
76
        return $this->stuTime->transformToStuDate($messageTimestamp);
77 1
    }
78
}
79