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