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

MessageProcessor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
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 MessageManager;
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->sendMailForInboxMessage($message);
36
            }
37
        }
38
39
        return $message;
40
    }
41
42
    private function sendMailForInboxMessage(Message $message): void
43
    {
44
        foreach ($message->getReceivers() as $receiver) {
45
            $user = $receiver->getReceiver();
46
47
            MessageManager::send_message(
48
                $user->getId(),
49
                $message->getTitle(),
50
                $message->getContent(),
51
                [],
52
                [],
53
                0,
54
                0,
55
                0,
56
                0,
57
                0,
58
                false,
59
                0,
60
                true,
61
                false,
62
                Message::MESSAGE_TYPE_INBOX
63
            );
64
        }
65
    }
66
}
67