Passed
Push — master ( 128fe0...fa7a2a )
by Nico
10:40
created

PrivateMessageSender::createPrivateMessage()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 38
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5.0035

Importance

Changes 0
Metric Value
cc 5
eloc 19
c 0
b 0
f 0
nc 5
nop 8
dl 0
loc 38
ccs 18
cts 19
cp 0.9474
crap 5.0035
rs 9.3222

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