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

PrivateMessageSender::createPrivateMessage()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4.0027

Importance

Changes 0
Metric Value
cc 4
eloc 18
nc 3
nop 8
dl 0
loc 36
rs 9.6666
c 0
b 0
f 0
ccs 17
cts 18
cp 0.9444
crap 4.0027

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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