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

PrivateMessageListItem::getReplied()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
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 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 1
    public function __construct(
22
        private PrivateMessageRepositoryInterface $privateMessageRepository,
23
        private ContactRepositoryInterface $contactRepository,
24
        private PrivateMessageInterface $message,
25
        private UserInterface $currentUser
26 1
    ) {}
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 1
    #[Override]
56
    public function isMarkableAsReceipt(): bool
57
    {
58 1
        $inboxPm = $this->message->getInboxPm();
59 1
        if ($inboxPm === null) {
60 1
            return false;
61
        }
62
63
        if (
64 1
            !$this->message->getSender()->isShowPmReadReceipt()
65 1
            || !$this->message->getRecipient()->isShowPmReadReceipt()
66
        ) {
67 1
            return false;
68
        }
69
70 1
        if ($inboxPm->isDeleted()) {
71
            return true;
72
        }
73
74 1
        return !$inboxPm->getNew();
75
    }
76
77
    #[Override]
78
    public function getText(): string
79
    {
80
        return $this->message->getText();
81
    }
82
83 1
    #[Override]
84
    public function getHref(): ?string
85
    {
86 1
        return $this->message->getHref();
87
    }
88
89
    #[Override]
90
    public function getNew(): bool
91
    {
92
        return $this->message->getNew();
93
    }
94
95 1
    #[Override]
96
    public function getId(): int
97
    {
98 1
        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 1
    #[Override]
108
    public function senderIsContact(): ?ContactInterface
109
    {
110 1
        if ($this->sendercontact === null) {
111 1
            $this->sendercontact = $this->contactRepository->getByUserAndOpponent(
112 1
                $this->currentUser->getId(),
113 1
                $this->message->getSenderId()
114 1
            );
115
        }
116 1
        return $this->sendercontact;
117
    }
118
119
    #[Override]
120
    public function hasTranslation(): bool
121
    {
122
        $text = $this->getText();
123
        return strpos($text, '[translate]') !== false && strpos($text, '[/translate]') !== false;
124
    }
125
}
126