Passed
Push — master ( 42dd6c...52ae6e )
by Angel Fernando Quiroz
09:11 queued 14s
created

saveNotificationForInboxMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 22
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 35
rs 9.568
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\State\ProcessorInterface;
12
use Chamilo\CoreBundle\Entity\Message;
13
use Chamilo\CoreBundle\Entity\MessageAttachment;
14
use Chamilo\CoreBundle\Entity\MessageRelUser;
15
use Chamilo\CoreBundle\Repository\ResourceNodeRepository;
16
use Doctrine\ORM\EntityManagerInterface;
17
use InvalidArgumentException;
18
use LogicException;
19
use Notification;
20
use Symfony\Bundle\SecurityBundle\Security;
21
use Symfony\Component\HttpFoundation\RequestStack;
22
use Vich\UploaderBundle\Storage\FlysystemStorage;
23
24
final class MessageProcessor implements ProcessorInterface
25
{
26
    public function __construct(
27
        private readonly ProcessorInterface $persistProcessor,
28
        private readonly ProcessorInterface $removeProcessor,
29
        private readonly FlysystemStorage $storage,
30
        private readonly EntityManagerInterface $entityManager,
31
        private readonly ResourceNodeRepository $resourceNodeRepository,
32
        private readonly Security $security,
33
        private readonly RequestStack $requestStack
34
    ) {}
35
36
    public function process($data, Operation $operation, array $uriVariables = [], array $context = [])
37
    {
38
        if ($operation instanceof DeleteOperationInterface) {
39
            return $this->removeProcessor->process($data, $operation, $uriVariables, $context);
40
        }
41
42
        /** @var Message $message */
43
        $message = $this->persistProcessor->process($data, $operation, $uriVariables, $context);
44
45
        foreach ($message->getAttachments() as $attachment) {
46
            $attachment->resourceNode->addResourceFile(
47
                $attachment->getResourceFileToAttach()
48
            );
49
50
            foreach ($message->getReceivers() as $receiver) {
51
                $attachment->addUserLink($receiver->getReceiver());
52
            }
53
        }
54
55
        $user = $this->security->getUser();
56
        if (!$user) {
57
            throw new LogicException('User not found.');
58
        }
59
60
        // Check if the relationship already exists
61
        $messageRelUserRepository = $this->entityManager->getRepository(MessageRelUser::class);
62
        $existingRelation = $messageRelUserRepository->findOneBy([
63
            'message' => $message,
64
            'receiver' => $user,
65
            'receiverType' => MessageRelUser::TYPE_SENDER,
66
        ]);
67
68
        if (!$existingRelation) {
69
            $messageRelUser = new MessageRelUser();
70
            $messageRelUser->setMessage($message);
71
            $messageRelUser->setReceiver($user);
72
            $messageRelUser->setReceiverType(MessageRelUser::TYPE_SENDER);
73
            $this->entityManager->persist($messageRelUser);
74
        }
75
76
        if (Message::MESSAGE_TYPE_INBOX === $message->getMsgType()) {
77
            $this->saveNotificationForInboxMessage($message);
78
        }
79
80
        $this->entityManager->flush();
81
82
        return $message;
83
    }
84
85
    private function saveNotificationForInboxMessage(Message $message): void
86
    {
87
        $sender_info = api_get_user_info(
88
            $message->getSender()->getId()
89
        );
90
91
        $userIdList = $message
92
            ->getReceivers()
93
            ->map(fn (MessageRelUser $messageRelUser): int => $messageRelUser->getReceiver()->getId())
94
            ->getValues()
95
        ;
96
97
        $attachmentList = [];
98
99
        /** @var MessageAttachment $messageAttachment */
100
        foreach ($message->getAttachments() as $messageAttachment) {
101
            $stream = $this->resourceNodeRepository->getResourceNodeFileStream(
102
                $messageAttachment->resourceNode
103
            );
104
105
            $attachmentList[] = [
106
                'stream' => $stream,
107
                'filename' => $messageAttachment->getFilename(),
108
            ];
109
        }
110
111
        (new Notification())
112
            ->saveNotification(
113
                $message->getId(),
114
                Notification::NOTIFICATION_TYPE_MESSAGE,
115
                $userIdList,
116
                $message->getTitle(),
117
                $message->getContent(),
118
                $sender_info,
119
                $attachmentList,
120
            )
121
        ;
122
    }
123
}
124