Passed
Pull Request — master (#5462)
by Angel Fernando Quiroz
07:19
created

MessageProcessor::process()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 4
nop 4
dl 0
loc 17
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\State;
8
9
use ApiPlatform\Metadata\DeleteOperationInterface;
10
use ApiPlatform\Metadata\Operation;
11
use ApiPlatform\Metadata\Post;
12
use ApiPlatform\State\ProcessorInterface;
13
use Chamilo\CoreBundle\Entity\Message;
14
use Notification;
15
16
final class MessageProcessor implements ProcessorInterface
17
{
18
    public function __construct(
19
        private readonly ProcessorInterface $persistProcessor,
20
        private readonly ProcessorInterface $removeProcessor,
21
    ) {}
22
23
    public function process($data, Operation $operation, array $uriVariables = [], array $context = [])
24
    {
25
        if ($operation instanceof DeleteOperationInterface) {
26
            return $this->removeProcessor->process($data, $operation, $uriVariables, $context);
27
        }
28
29
        $message = $this->persistProcessor->process($data, $operation, $uriVariables, $context);
30
31
        assert($message instanceof Message);
32
33
        if ($operation instanceof Post) {
34
            if (Message::MESSAGE_TYPE_INBOX === $message->getMsgType()) {
35
                $this->saveNotificationForInboxMessage($message);
36
            }
37
        }
38
39
        return $message;
40
    }
41
42
    private function saveNotificationForInboxMessage(Message $message): void
43
    {
44
        $sender_info = api_get_user_info(
45
            $message->getSender()->getId()
46
        );
47
48
        foreach ($message->getReceivers() as $receiver) {
49
            $user = $receiver->getReceiver();
50
51
            (new Notification())
52
                ->saveNotification(
53
                    $message->getId(),
54
                    Notification::NOTIFICATION_TYPE_MESSAGE,
55
                    [$user->getId()],
56
                    $message->getTitle(),
57
                    $message->getContent(),
58
                    $sender_info,
59
                )
60
            ;
61
        }
62
    }
63
}
64