Passed
Pull Request — master (#6211)
by Angel Fernando Quiroz
09:24
created

MessageProcessor::process()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 47
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 26
nc 16
nop 4
dl 0
loc 47
rs 8.5706
c 0
b 0
f 0
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\Entity\User;
16
use Chamilo\CoreBundle\Repository\ResourceNodeRepository;
17
use Doctrine\ORM\EntityManagerInterface;
18
use LogicException;
19
use Notification;
20
use Symfony\Bundle\SecurityBundle\Security;
21
22
/**
23
 * @implements ProcessorInterface<Message, Message|void>
24
 */
25
final readonly class MessageProcessor implements ProcessorInterface
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 25 at column 6
Loading history...
26
{
27
    public function __construct(
28
        private ProcessorInterface $persistProcessor,
29
        private ProcessorInterface $removeProcessor,
30
        private EntityManagerInterface $entityManager,
31
        private ResourceNodeRepository $resourceNodeRepository,
32
        private Security $security,
33
    ) {}
34
35
    public function process($data, Operation $operation, array $uriVariables = [], array $context = []): ?Message
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
        /** @var User $user */
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