Passed
Push — master ( 4cbcfa...1ee8de )
by Nico
53:18 queued 29:25
created

PrivateMessageSender::sendBroadcast()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 20
nc 3
nop 3
dl 0
loc 33
ccs 22
cts 22
cp 1
crap 3
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Message\Lib;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use InvalidArgumentException;
9
use JBBCode\Parser;
10
use Laminas\Mail\Exception\RuntimeException;
11
use Noodlehaus\ConfigInterface;
12
use Stu\Lib\Information\InformationWrapper;
13
use Stu\Lib\Mail\MailFactoryInterface;
14
use Stu\Module\Control\StuTime;
15
use Stu\Module\Logging\LoggerEnum;
16
use Stu\Module\Logging\LoggerUtilFactoryInterface;
17
use Stu\Module\Logging\LoggerUtilInterface;
18
use Stu\Module\PlayerSetting\Lib\UserEnum;
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 PrivateMessageFolderRepositoryInterface $privateMessageFolderRepository;
28
29
    private PrivateMessageRepositoryInterface $privateMessageRepository;
30
31
    private MailFactoryInterface $mailFactory;
32
33
    private UserRepositoryInterface $userRepository;
34
35
    private ConfigInterface $config;
36
37
    private LoggerUtilInterface $loggerUtil;
38
39
    private Parser $bbcodeParser;
40
41
    private StuTime $stuTime;
42
43
    private EntityManagerInterface $entityManager;
44
45 8
    public function __construct(
46
        PrivateMessageFolderRepositoryInterface $privateMessageFolderRepository,
47
        PrivateMessageRepositoryInterface $privateMessageRepository,
48
        UserRepositoryInterface $userRepository,
49
        MailFactoryInterface $mailFactory,
50
        ConfigInterface $config,
51
        Parser $bbcodeParser,
52
        StuTime $stuTime,
53
        EntityManagerInterface $entityManager,
54
        LoggerUtilFactoryInterface $loggerUtilFactory
55
    ) {
56 8
        $this->privateMessageFolderRepository = $privateMessageFolderRepository;
57 8
        $this->privateMessageRepository = $privateMessageRepository;
58 8
        $this->userRepository = $userRepository;
59 8
        $this->mailFactory = $mailFactory;
60 8
        $this->config = $config;
61 8
        $this->bbcodeParser = $bbcodeParser;
62 8
        $this->stuTime = $stuTime;
63 8
        $this->entityManager = $entityManager;
64 8
        $this->loggerUtil = $loggerUtilFactory->getLoggerUtil();
65
    }
66
67 2
    public function send(
68
        int $senderId,
69
        int $recipientId,
70
        string|InformationWrapper $information,
71
        int $category = PrivateMessageFolderSpecialEnum::PM_SPECIAL_SYSTEM,
72
        string $href = null,
73
        bool $isRead = false
74
    ): void {
75 2
        if ($senderId === $recipientId) {
76
            return;
77
        }
78
79
        if (
80 2
            $information instanceof InformationWrapper
81 2
            && $information->isEmpty()
82
        ) {
83
            return;
84
        }
85
86 2
        $text = $information instanceof InformationWrapper ? $information->getInformationsAsString() : $information;
87
88 2
        $recipient = $this->userRepository->find($recipientId);
89 2
        $sender = $this->userRepository->find($senderId);
90
91 2
        if ($sender === null) {
92
            throw new InvalidArgumentException(sprintf('Sender with id %d does not exist', $senderId));
93
        }
94 2
        if ($recipient === null) {
95
            throw new InvalidArgumentException(sprintf('Recipient with id %d does not exist', $recipientId));
96
        }
97
98 2
        $time = $this->stuTime->time();
99
100 2
        $pm = $this->createPrivateMessage(
101 2
            $sender,
102 2
            $recipient,
103 2
            $time,
104 2
            $category,
105 2
            $text,
106 2
            $href,
107 2
            !$isRead
108 2
        );
109
110
        if (
111 2
            $category === PrivateMessageFolderSpecialEnum::PM_SPECIAL_MAIN
112 2
            && $recipient->isEmailNotification()
113
        ) {
114 1
            $this->sendEmailNotification($sender->getName(), $text, $recipient);
115
        }
116
117 2
        if ($senderId != UserEnum::USER_NOONE) {
118 2
            $this->entityManager->flush();
119
120 2
            $this->createPrivateMessage(
121 2
                $recipient,
122 2
                $sender,
123 2
                $time,
124 2
                PrivateMessageFolderSpecialEnum::PM_SPECIAL_PMOUT,
125 2
                $text,
126 2
                null,
127 2
                false,
128 2
                $pm->getId()
129 2
            );
130
        }
131
    }
132
133 2
    public function sendBroadcast(
134
        UserInterface $sender,
135
        array $recipients,
136
        string $text
137
    ): void {
138 2
        if ($recipients === []) {
139 1
            return;
140
        }
141
142 1
        $time = $this->stuTime->time();
143
144
        //broadcast pm to every recipient
145 1
        foreach ($recipients as $recipient) {
146 1
            $this->createPrivateMessage(
147 1
                $sender,
148 1
                $recipient,
149 1
                $time,
150 1
                PrivateMessageFolderSpecialEnum::PM_SPECIAL_MAIN,
151 1
                $text,
152 1
                null,
153 1
                true
154 1
            );
155
        }
156
157
        //single item to outbox
158 1
        $this->createPrivateMessage(
159 1
            $this->userRepository->getFallbackUser(),
160 1
            $sender,
161 1
            $time,
162 1
            PrivateMessageFolderSpecialEnum::PM_SPECIAL_PMOUT,
163 1
            $text,
164 1
            null,
165 1
            false
166 1
        );
167
    }
168
169 3
    private function createPrivateMessage(
170
        UserInterface $sender,
171
        UserInterface $recipient,
172
        int $time,
173
        int $category,
174
        string $text,
175
        ?string $href,
176
        bool $new,
177
        int $inboxPmId = null
178
    ): PrivateMessageInterface {
179 3
        $folder = $this->privateMessageFolderRepository->getByUserAndSpecial($recipient->getId(), $category);
180
181 3
        if ($folder === null) {
182
            throw new InvalidArgumentException(sprintf('Folder with user_id %d and category %d does not exist', $recipient->getId(), $category));
183
        }
184
185 3
        $pm = $this->privateMessageRepository->prototype();
186 3
        $pm->setDate($time);
187 3
        $pm->setCategory($folder);
188 3
        $pm->setText($text);
189 3
        $pm->setHref($href);
190 3
        $pm->setRecipient($recipient);
191 3
        $pm->setSender($sender);
192 3
        $pm->setNew($new);
193 3
        $pm->setInboxPmId($inboxPmId);
194
195 3
        $this->privateMessageRepository->save($pm);
196
197 3
        return $pm;
198
    }
199
200 1
    private function sendEmailNotification(string $senderName, string $message, UserInterface $user): void
201
    {
202 1
        $mail = $this->mailFactory->createMessage();
203
204 1
        $mail->addTo($user->getEmail());
205 1
        $senderNameAsText = $this->bbcodeParser->parse($senderName)->getAsText();
206 1
        $mail->setSubject(sprintf(_('Neue Privatnachricht von Spieler %s'), $senderNameAsText));
207 1
        $mail->setFrom($this->config->get('game.email_sender_address'));
208 1
        $mail->setBody($message);
209
210
        try {
211 1
            $transport = $this->mailFactory->createSendmail();
212 1
            $transport->send($mail);
213
        } catch (RuntimeException $e) {
214
            $this->loggerUtil->init("mail", LoggerEnum::LEVEL_ERROR);
215
            $this->loggerUtil->log($e->getMessage());
216
        }
217
    }
218
}
219