Passed
Push — master ( eefae6...712ebc )
by Nico
27:55 queued 18:33
created

PrivateMessageListItem::hasTranslation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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