Skip to content

Commit

Permalink
Internal: Message: Send mail when saving inbox message - refs BT#21613
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelFQC committed May 23, 2024
1 parent f1cf9a6 commit 6b612f2
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
5 changes: 5 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ services:
$persistProcessor: '@api_platform.doctrine.orm.state.persist_processor'
$removeProcessor: '@api_platform.doctrine.orm.state.remove_processor'

Chamilo\CoreBundle\State\MessageProcessor:
bind:
$persistProcessor: '@api_platform.doctrine.orm.state.persist_processor'
$removeProcessor: '@api_platform.doctrine.orm.state.remove_processor'

Chamilo\CoreBundle\EventSubscriber\AnonymousUserSubscriber:
tags:
- name: kernel.event_subscriber
Expand Down
4 changes: 3 additions & 1 deletion src/CoreBundle/Entity/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ApiPlatform\Metadata\Put;
use Chamilo\CoreBundle\Repository\MessageRepository;
use Chamilo\CoreBundle\State\MessageByGroupStateProvider;
use Chamilo\CoreBundle\State\MessageProcessor;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
Expand Down Expand Up @@ -56,7 +57,8 @@
denormalizationContext: [
'groups' => ['message:write'],
],
security: "is_granted('ROLE_USER')"
security: "is_granted('ROLE_USER')",
processor: MessageProcessor::class,
)]
#[ApiFilter(filterClass: OrderFilter::class, properties: ['title', 'sendDate'])]
#[ApiFilter(
Expand Down
66 changes: 66 additions & 0 deletions src/CoreBundle/State/MessageProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/* For licensing terms, see /license.txt */

declare(strict_types=1);

namespace Chamilo\CoreBundle\State;

use ApiPlatform\Metadata\DeleteOperationInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Post;
use ApiPlatform\State\ProcessorInterface;
use Chamilo\CoreBundle\Entity\Message;
use MessageManager;

final class MessageProcessor implements ProcessorInterface
{
public function __construct(
private readonly ProcessorInterface $persistProcessor,
private readonly ProcessorInterface $removeProcessor,
) {}

public function process($data, Operation $operation, array $uriVariables = [], array $context = [])
{
if ($operation instanceof DeleteOperationInterface) {
return $this->removeProcessor->process($data, $operation, $uriVariables, $context);
}

$message = $this->persistProcessor->process($data, $operation, $uriVariables, $context);

assert($message instanceof Message);

if ($operation instanceof Post) {
if (Message::MESSAGE_TYPE_INBOX === $message->getMsgType()) {
$this->sendMailForInboxMessage($message);
}
}

return $message;
}

private function sendMailForInboxMessage(Message $message): void
{
foreach ($message->getReceivers() as $receiver) {
$user = $receiver->getReceiver();

MessageManager::send_message(
$user->getId(),
$message->getTitle(),
$message->getContent(),
[],
[],
0,
0,
0,
0,
0,
false,
0,
true,
false,
Message::MESSAGE_TYPE_INBOX
);
}
}
}

0 comments on commit 6b612f2

Please sign in to comment.