Passed
Push — dependabot/npm_and_yarn/microm... ( e84ba6...f2f212 )
by
unknown
10:03
created

MessageProcessor::process()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 47
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

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