Passed
Push — master ( 91454b...2408fc )
by Nico
22:07 queued 10:43
created

PrivateMessageSender::__construct()   A

Complexity

Conditions 1
Paths 1

Size

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