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

Conversation::getLastHeadline()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
ccs 0
cts 8
cp 0
crap 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Game\Lib\View\Provider\Message;
6
7
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...
8
use Stu\Module\Control\StuTime;
9
use Stu\Module\Message\Lib\PrivateMessageListItem;
10
use Stu\Orm\Entity\PrivateMessageInterface;
11
use Stu\Orm\Entity\UserInterface;
12
use Stu\Orm\Repository\ContactRepositoryInterface;
13
use Stu\Orm\Repository\PrivateMessageRepositoryInterface;
14
15
final class Conversation extends PrivateMessageListItem
16
{
17
    public function __construct(
18
        private PrivateMessageInterface $message,
19
        private int $time,
20
        private StuTime $stuTime,
21
        int $currentUserId,
22
        PrivateMessageRepositoryInterface $privateMessageRepository,
23
        ContactRepositoryInterface $contactRepository
24
    ) {
25
        parent::__construct(
26
            $privateMessageRepository,
27
            $contactRepository,
28
            $message,
29
            $currentUserId
30
        );
31
    }
32
33
    public function getLastHeadline(): string
34
    {
35
        return sprintf(
36
            '%s%s',
37
            $this->message->getInboxPm() === null ? sprintf(
38
                '%s: ',
39
                $this->getOtherUser()->getName()
40
            ) : '',
41
            substr($this->message->getText(), 0, 200)
42
        );
43
    }
44
45
    public function getOtherUser(): UserInterface
46
    {
47
        return $this->message->getSender();
48
    }
49
50
    public function getDateString(): string
51
    {
52
        //TODO move code to MessengerStyleProvider and inject as constructor parameter
53
        $messageTimestamp = $this->message->getDate();
54
        $distanceInSeconds = $this->time - $messageTimestamp;
55
56
        if ($distanceInSeconds < TimeConstants::ONE_DAY_IN_SECONDS) {
57
            return date("H:i", $messageTimestamp);
58
        }
59
        if ($distanceInSeconds < TimeConstants::SEVEN_DAYS_IN_SECONDS) {
60
            return match ((int)date("N", $messageTimestamp)) {
61
                1 => 'Montag',
62
                2 => 'Dienstag',
63
                3 => 'Mittwoch',
64
                4 => 'Donnerstag',
65
                5 => 'Freitag',
66
                6 => 'Samstag',
67
                7 => 'Sonntag'
68
            };
69
        }
70
        return $this->stuTime->transformToStuDate($messageTimestamp);
71
    }
72
}
73