Passed
Push — master ( 0a21cd...0ad97c )
by Janko
05:01
created

PrivateMessageSender::send()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 55
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 8.0142

Importance

Changes 0
Metric Value
cc 8
eloc 33
nc 9
nop 6
dl 0
loc 55
ccs 31
cts 33
cp 0.9394
crap 8.0142
rs 8.1475
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Message\Lib;
6
7
use InvalidArgumentException;
8
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...
9
use Stu\Component\Player\Settings\UserSettingsProviderInterface;
10
use Stu\Lib\General\EntityWithHrefInterface;
11
use Stu\Lib\Information\InformationWrapper;
12
use Stu\Module\Control\StuTime;
13
use Stu\Orm\Entity\PrivateMessage;
14
use Stu\Orm\Entity\User;
15
use Stu\Orm\Repository\PrivateMessageFolderRepositoryInterface;
16
use Stu\Orm\Repository\PrivateMessageRepositoryInterface;
17
use Stu\Orm\Repository\UserRepositoryInterface;
18
19
final class PrivateMessageSender implements PrivateMessageSenderInterface
20
{
21
    /** @var array<int> */
22
    public static array $blockedUserIds = [];
23
24 8
    public function __construct(
25
        private readonly PrivateMessageFolderRepositoryInterface $privateMessageFolderRepository,
26
        private readonly PrivateMessageRepositoryInterface $privateMessageRepository,
27
        private readonly UserRepositoryInterface $userRepository,
28
        private readonly EmailNotificationSenderInterface $emailNotificationSender,
29
        private readonly UserSettingsProviderInterface $userSettingsProvider,
30
        private readonly StuTime $stuTime
31 8
    ) {}
32
33 17
    #[Override]
34
    public function send(
35
        int $senderId,
36
        int $recipientId,
37
        string|InformationWrapper $information,
38
        PrivateMessageFolderTypeEnum $folderType = PrivateMessageFolderTypeEnum::SPECIAL_SYSTEM,
39
        null|string|EntityWithHrefInterface $href = null,
40
        bool $isRead = false
41
    ): void {
42 17
        if ($senderId === $recipientId) {
43 10
            return;
44
        }
45
46
        if (
47 7
            $information instanceof InformationWrapper
48 7
            && $information->isEmpty()
49
        ) {
50
            return;
51
        }
52
53 7
        if (in_array($recipientId, self::$blockedUserIds)) {
54 1
            return;
55
        }
56
57 6
        $text = $information instanceof InformationWrapper ? $information->getInformationsAsString() : $information;
58
59 6
        $sender = $this->getSender($senderId);
60
61 6
        $recipient = $this->userRepository->find($recipientId);
62 6
        if ($recipient === null) {
63
            throw new InvalidArgumentException(sprintf('Recipient with id %d does not exist', $recipientId));
64
        }
65
66 6
        $time = $this->stuTime->time();
67
68 6
        $pm = $this->createPrivateMessage(
69 6
            $sender,
70 6
            $recipient,
71 6
            $time,
72 6
            $folderType,
73 6
            $text,
74 6
            $this->getHref($href),
75 6
            !$isRead
76 6
        );
77
78 6
        if ($sender->isContactable()) {
79 3
            $this->createPrivateMessage(
80 3
                $recipient,
81 3
                $sender,
82 3
                $time,
83 3
                PrivateMessageFolderTypeEnum::SPECIAL_PMOUT,
84 3
                $text,
85 3
                null,
86 3
                false,
87 3
                $pm
88 3
            );
89
        }
90
    }
91
92 6
    private function getSender(int $senderId): User
93
    {
94 6
        if (in_array($senderId, self::$blockedUserIds)) {
95 1
            return $this->userRepository->getFallbackUser();
96
        }
97
98 5
        $sender = $this->userRepository->find($senderId);
99 5
        if ($sender === null) {
100
            throw new InvalidArgumentException(sprintf('Sender with id %d does not exist', $senderId));
101
        }
102
103 5
        return $sender;
104
    }
105
106 6
    private function getHref(null|string|EntityWithHrefInterface $href): ?string
107
    {
108 6
        return $href instanceof EntityWithHrefInterface
109 1
            ? $href->getHref()
110 6
            : $href;
111
    }
112
113 2
    #[Override]
114
    public function sendBroadcast(
115
        User $sender,
116
        array $recipients,
117
        string $text
118
    ): void {
119 2
        if ($recipients === []) {
120 1
            return;
121
        }
122
123 1
        $time = $this->stuTime->time();
124
125
        //broadcast pm to every recipient
126 1
        foreach ($recipients as $recipient) {
127 1
            $this->createPrivateMessage(
128 1
                $sender,
129 1
                $recipient,
130 1
                $time,
131 1
                PrivateMessageFolderTypeEnum::SPECIAL_MAIN,
132 1
                $text,
133 1
                null,
134 1
                true
135 1
            );
136
        }
137
138
        //single item to outbox
139 1
        $this->createPrivateMessage(
140 1
            $this->userRepository->getFallbackUser(),
141 1
            $sender,
142 1
            $time,
143 1
            PrivateMessageFolderTypeEnum::SPECIAL_PMOUT,
144 1
            $text,
145 1
            null,
146 1
            false
147 1
        );
148
    }
149
150 7
    private function createPrivateMessage(
151
        User $sender,
152
        User $recipient,
153
        int $time,
154
        PrivateMessageFolderTypeEnum $folderType,
155
        string $text,
156
        ?string $href,
157
        bool $new,
158
        ?PrivateMessage $inboxPm = null
159
    ): PrivateMessage {
160 7
        $folder = $this->privateMessageFolderRepository->getByUserAndSpecial($recipient->getId(), $folderType);
161
162 7
        if ($folder === null) {
163
            throw new InvalidArgumentException(sprintf('Folder with user_id %d and category %d does not exist', $recipient->getId(), $folderType->value));
164
        }
165
166
        if (
167 7
            $folderType === PrivateMessageFolderTypeEnum::SPECIAL_MAIN
168 7
            && $this->userSettingsProvider->isEmailNotification($recipient)
169
        ) {
170 2
            $this->emailNotificationSender->sendNotification($sender->getName(), $text, $recipient);
171
        }
172
173 7
        $pm = $this->privateMessageRepository->prototype()
174 7
            ->setDate($time)
175 7
            ->setCategory($folder)
176 7
            ->setText($text)
177 7
            ->setHref($href)
178 7
            ->setRecipient($recipient)
179 7
            ->setSender($sender)
180 7
            ->setNew($new)
181 7
            ->setInboxPm($inboxPm);
182
183 7
        $this->privateMessageRepository->save($pm);
184
185 7
        return $pm;
186
    }
187
}
188