-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathEmailForwardController.php
More file actions
121 lines (112 loc) · 4.49 KB
/
EmailForwardController.php
File metadata and controls
121 lines (112 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
declare(strict_types=1);
namespace PhpList\RestBundle\Messaging\Controller;
use Doctrine\ORM\EntityManagerInterface;
use OpenApi\Attributes as OA;
use PhpList\Core\Domain\Messaging\Model\Dto\MessageForwardDto;
use PhpList\Core\Domain\Messaging\Model\Message;
use PhpList\Core\Domain\Messaging\Service\MessageForwardService;
use PhpList\Core\Security\Authentication;
use PhpList\RestBundle\Common\Controller\BaseController;
use PhpList\RestBundle\Common\Validator\RequestValidator;
use PhpList\RestBundle\Messaging\Request\ForwardMessageRequest;
use PhpList\RestBundle\Messaging\Serializer\ForwardingResultNormalizer;
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
/**
* This controller provides REST API for email forwarding
*
* @author Tatevik Grigoryan <tatevik@phplist.com>
*/
#[Route('/email-forward', name: 'email_forward_')]
class EmailForwardController extends BaseController
{
public function __construct(
Authentication $authentication,
RequestValidator $validator,
private readonly EntityManagerInterface $entityManager,
private readonly MessageForwardService $messageForwardService,
private readonly ForwardingResultNormalizer $forwardingResultNormalizer,
) {
parent::__construct($authentication, $validator);
}
#[Route('/{messageId}', name: 'forward', requirements: ['messageId' => '\\d+'], methods: ['POST'])]
#[OA\Post(
path: '/api/v2/campaigns/{messageId}/forward',
description: '🚧 **Status: Beta** – This method is under development. Avoid using in production. ' .
'Queues forwarding of a campaign/message to provided recipient emails.',
summary: 'Forward a message to recipients.',
requestBody: new OA\RequestBody(
description: 'Forwarding payload',
required: true,
content: new OA\JsonContent(ref: '#/components/schemas/ForwardMessageRequest')
),
tags: ['campaigns'],
parameters: [
new OA\Parameter(
name: 'php-auth-pw',
description: 'Session key obtained from login',
in: 'header',
required: true,
schema: new OA\Schema(type: 'string')
),
new OA\Parameter(
name: 'messageId',
description: 'message ID',
in: 'path',
required: true,
schema: new OA\Schema(type: 'string')
)
],
responses: [
new OA\Response(
response: 202,
description: 'Accepted',
content: new OA\JsonContent(ref: '#/components/schemas/ForwardResult')
),
new OA\Response(
response: 403,
description: 'Failure',
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
),
new OA\Response(
response: 404,
description: 'Failure',
content: new OA\JsonContent(ref: '#/components/schemas/NotFoundErrorResponse')
),
new OA\Response(
response: 422,
description: 'Failure',
content: new OA\JsonContent(ref: '#/components/schemas/ValidationErrorResponse')
)
]
)]
public function forwardMessage(
Request $request,
#[MapEntity(mapping: ['messageId' => 'id'])] ?Message $message = null
): JsonResponse {
if ($message === null) {
throw $this->createNotFoundException('Campaign not found.');
}
/** @var ForwardMessageRequest $forwardRequest */
$forwardRequest = $this->validator->validate($request, ForwardMessageRequest::class);
$result = $this->messageForwardService->forward(
messageForwardDto: new MessageForwardDto(
emails: $forwardRequest->recipients,
uid: $forwardRequest->uid,
fromName: $forwardRequest->fromName,
fromEmail: $forwardRequest->fromEmail,
note: $forwardRequest->note,
),
campaign: $message,
);
$this->entityManager->flush();
return $this->json(
$this->forwardingResultNormalizer->normalize($result),
Response::HTTP_ACCEPTED
);
}
}