Passed
Pull Request — master (#1904)
by Nico
39:29 queued 12:01
created

PrivateMessageSender::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 8
dl 0
loc 11
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0

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 JBBCode\Parser;
9
use Noodlehaus\ConfigInterface;
10
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...
11
use RuntimeException;
12
use Stu\Lib\Information\InformationWrapper;
13
use Stu\Lib\Mail\MailFactoryInterface;
14
use Stu\Module\Control\StuTime;
15
use Stu\Module\Logging\LoggerEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\Logging\LoggerEnum 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...
16
use Stu\Module\Logging\LoggerUtilFactoryInterface;
17
use Stu\Module\Logging\LoggerUtilInterface;
18
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...
19
use Stu\Orm\Entity\PrivateMessageInterface;
20
use Stu\Orm\Entity\UserInterface;
21
use Stu\Orm\Repository\PrivateMessageFolderRepositoryInterface;
22
use Stu\Orm\Repository\PrivateMessageRepositoryInterface;
23
use Stu\Orm\Repository\UserRepositoryInterface;
24
25
final class PrivateMessageSender implements PrivateMessageSenderInterface
26
{
27
    private LoggerUtilInterface $loggerUtil;
28
29 8
    public function __construct(
30
        private PrivateMessageFolderRepositoryInterface $privateMessageFolderRepository,
31
        private PrivateMessageRepositoryInterface $privateMessageRepository,
32
        private UserRepositoryInterface $userRepository,
33
        private MailFactoryInterface $mailFactory,
34
        private ConfigInterface $config,
35
        private Parser $bbcodeParser,
36
        private StuTime $stuTime,
37
        LoggerUtilFactoryInterface $loggerUtilFactory
38
    ) {
39 8
        $this->loggerUtil = $loggerUtilFactory->getLoggerUtil();
40
    }
41
42 2
    #[Override]
43
    public function send(
44
        int $senderId,
45
        int $recipientId,
46
        string|InformationWrapper $information,
47
        PrivateMessageFolderTypeEnum $folderType = PrivateMessageFolderTypeEnum::SPECIAL_SYSTEM,
48
        ?string $href = null,
49
        bool $isRead = false
50
    ): void {
51 2
        if ($senderId === $recipientId) {
52
            return;
53
        }
54
55
        if (
56 2
            $information instanceof InformationWrapper
57 2
            && $information->isEmpty()
58
        ) {
59
            return;
60
        }
61
62 2
        $text = $information instanceof InformationWrapper ? $information->getInformationsAsString() : $information;
63
64 2
        $recipient = $this->userRepository->find($recipientId);
65 2
        $sender = $this->userRepository->find($senderId);
66
67 2
        if ($sender === null) {
68
            throw new InvalidArgumentException(sprintf('Sender with id %d does not exist', $senderId));
69
        }
70 2
        if ($recipient === null) {
71
            throw new InvalidArgumentException(sprintf('Recipient with id %d does not exist', $recipientId));
72
        }
73
74 2
        $time = $this->stuTime->time();
75
76 2
        $pm = $this->createPrivateMessage(
77 2
            $sender,
78 2
            $recipient,
79 2
            $time,
80 2
            $folderType,
81 2
            $text,
82 2
            $href,
83 2
            !$isRead
84 2
        );
85
86
        if (
87 2
            $folderType === PrivateMessageFolderTypeEnum::SPECIAL_MAIN
88 2
            && $recipient->isEmailNotification()
89
        ) {
90 1
            $this->sendEmailNotification($sender->getName(), $text, $recipient);
91
        }
92
93 2
        if ($senderId != UserEnum::USER_NOONE) {
94 2
            $this->createPrivateMessage(
95 2
                $recipient,
96 2
                $sender,
97 2
                $time,
98 2
                PrivateMessageFolderTypeEnum::SPECIAL_PMOUT,
99 2
                $text,
100 2
                null,
101 2
                false,
102 2
                $pm
103 2
            );
104
        }
105
    }
106
107 2
    #[Override]
108
    public function sendBroadcast(
109
        UserInterface $sender,
110
        array $recipients,
111
        string $text
112
    ): void {
113 2
        if ($recipients === []) {
114 1
            return;
115
        }
116
117 1
        $time = $this->stuTime->time();
118
119
        //broadcast pm to every recipient
120 1
        foreach ($recipients as $recipient) {
121 1
            $this->createPrivateMessage(
122 1
                $sender,
123 1
                $recipient,
124 1
                $time,
125 1
                PrivateMessageFolderTypeEnum::SPECIAL_MAIN,
126 1
                $text,
127 1
                null,
128 1
                true
129 1
            );
130
        }
131
132
        //single item to outbox
133 1
        $this->createPrivateMessage(
134 1
            $this->userRepository->getFallbackUser(),
135 1
            $sender,
136 1
            $time,
137 1
            PrivateMessageFolderTypeEnum::SPECIAL_PMOUT,
138 1
            $text,
139 1
            null,
140 1
            false
141 1
        );
142
    }
143
144 3
    private function createPrivateMessage(
145
        UserInterface $sender,
146
        UserInterface $recipient,
147
        int $time,
148
        PrivateMessageFolderTypeEnum $folderType,
149
        string $text,
150
        ?string $href,
151
        bool $new,
152
        ?PrivateMessageInterface $inboxPm = null
153
    ): PrivateMessageInterface {
154 3
        $folder = $this->privateMessageFolderRepository->getByUserAndSpecial($recipient->getId(), $folderType);
155
156 3
        if ($folder === null) {
157
            throw new InvalidArgumentException(sprintf('Folder with user_id %d and category %d does not exist', $recipient->getId(), $folderType->value));
158
        }
159
160 3
        $pm = $this->privateMessageRepository->prototype();
161 3
        $pm->setDate($time);
162 3
        $pm->setCategory($folder);
163 3
        $pm->setText($text);
164 3
        $pm->setHref($href);
165 3
        $pm->setRecipient($recipient);
166 3
        $pm->setSender($sender);
167 3
        $pm->setNew($new);
168 3
        if ($inboxPm !== null) {
169 2
            $pm->setInboxPm($inboxPm);
170
        }
171
172 3
        $this->privateMessageRepository->save($pm);
173
174 3
        return $pm;
175
    }
176
177 1
    private function sendEmailNotification(string $senderName, string $message, UserInterface $user): void
178
    {
179 1
        $mail = $this->mailFactory->createStuMail()
180 1
            ->setFrom($this->config->get('game.email_sender_address'))
181 1
            ->addTo($user->getEmail())
182 1
            ->setSubject(sprintf(
183 1
                'Neue Privatnachricht von Spieler %s',
184 1
                $this->bbcodeParser->parse($senderName)->getAsText()
185 1
            ))
186 1
            ->setBody($message);
187
188
        try {
189 1
            $mail->send();
190
        } catch (RuntimeException $e) {
191
            $this->loggerUtil->init("mail", LoggerEnum::LEVEL_ERROR);
192
            $this->loggerUtil->log($e->getMessage());
193
        }
194
    }
195
}
196