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

PrivateMessageListItem   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 47
c 0
b 0
f 0
dl 0
loc 115
ccs 0
cts 46
cp 0
rs 10
wmc 22

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getDate() 0 4 1
A getSender() 0 7 2
A getId() 0 4 1
A getReplied() 0 4 1
A __construct() 0 6 1
A isMarkableAsReceipt() 0 20 5
A getHref() 0 4 1
A hasTranslation() 0 5 2
A getText() 0 4 1
A senderIsContact() 0 10 2
A getNew() 0 4 1
A displayUserLinks() 0 4 2
A isMarkableAsNew() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Message\Lib;
6
7
use Override;
0 ignored issues
show
Bug introduced by
The type Override 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\PlayerSetting\Lib\UserEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserEnum 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...
9
use Stu\Orm\Entity\ContactInterface;
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
class PrivateMessageListItem implements PrivateMessageListItemInterface
16
{
17
    private ?UserInterface $sender = null;
18
19
    private ?ContactInterface $sendercontact = null;
20
21
    public function __construct(
22
        private PrivateMessageRepositoryInterface $privateMessageRepository,
23
        private ContactRepositoryInterface $contactRepository,
24
        private PrivateMessageInterface $message,
25
        private int $currentUserId
26
    ) {}
27
28
    #[Override]
29
    public function getSender(): UserInterface
30
    {
31
        if ($this->sender === null) {
32
            $this->sender = $this->message->getSender();
33
        }
34
        return $this->sender;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->sender could return the type null which is incompatible with the type-hinted return Stu\Orm\Entity\UserInterface. Consider adding an additional type-check to rule them out.
Loading history...
35
    }
36
37
    #[Override]
38
    public function getDate(): int
39
    {
40
        return $this->message->getDate();
41
    }
42
43
    #[Override]
44
    public function isMarkableAsNew(): bool
45
    {
46
        if ($this->message->getNew() === false) {
47
            return false;
48
        }
49
        $this->message->setNew(false);
50
        $this->privateMessageRepository->save($this->message, true);
51
52
        return true;
53
    }
54
55
    #[Override]
56
    public function isMarkableAsReceipt(): bool
57
    {
58
        $inboxPm = $this->message->getInboxPm();
59
        if ($inboxPm === null) {
60
            return false;
61
        }
62
63
        if (
64
            !$this->message->getSender()->isShowPmReadReceipt()
65
            || !$this->message->getRecipient()->isShowPmReadReceipt()
66
        ) {
67
            return false;
68
        }
69
70
        if ($inboxPm->isDeleted()) {
71
            return true;
72
        }
73
74
        return !$inboxPm->getNew();
75
    }
76
77
    #[Override]
78
    public function getText(): string
79
    {
80
        return $this->message->getText();
81
    }
82
83
    #[Override]
84
    public function getHref(): ?string
85
    {
86
        return $this->message->getHref();
87
    }
88
89
    #[Override]
90
    public function getNew(): bool
91
    {
92
        return $this->message->getNew();
93
    }
94
95
    #[Override]
96
    public function getId(): int
97
    {
98
        return $this->message->getId();
99
    }
100
101
    #[Override]
102
    public function displayUserLinks(): bool
103
    {
104
        return $this->getSender() && $this->getSender()->getId() !== UserEnum::USER_NOONE;
105
    }
106
107
    #[Override]
108
    public function getReplied(): bool
109
    {
110
        return $this->message->getReplied();
111
    }
112
113
    #[Override]
114
    public function senderIsContact(): ?ContactInterface
115
    {
116
        if ($this->sendercontact === null) {
117
            $this->sendercontact = $this->contactRepository->getByUserAndOpponent(
118
                $this->currentUserId,
119
                $this->message->getSenderId()
120
            );
121
        }
122
        return $this->sendercontact;
123
    }
124
125
    #[Override]
126
    public function hasTranslation(): bool
127
    {
128
        $text = $this->getText();
129
        return strpos($text, '[translate]') !== false && strpos($text, '[/translate]') !== false;
130
    }
131
}
132