/
ResendOrderConfirmationEmailAction.php
75 lines (64 loc) · 2.71 KB
/
ResendOrderConfirmationEmailAction.php
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
<?php
/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Bundle\AdminBundle\Action;
use Sylius\Bundle\AdminBundle\EmailManager\OrderEmailManagerInterface;
use Sylius\Bundle\CoreBundle\Provider\FlashBagProvider;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Csrf\CsrfToken;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
final class ResendOrderConfirmationEmailAction
{
public function __construct(
private OrderRepositoryInterface $orderRepository,
private OrderEmailManagerInterface $orderEmailManager,
private CsrfTokenManagerInterface $csrfTokenManager,
private SessionInterface|RequestStack $requestStackOrSession,
) {
if ($this->requestStackOrSession instanceof SessionInterface) {
trigger_deprecation(
'sylius/admin-bundle',
'1.12',
'Passing an instance of %s as constructor argument for %s is deprecated and will be removed in Sylius 2.0. Pass an instance of %s instead.',
SessionInterface::class,
self::class,
RequestStack::class,
);
}
}
public function __invoke(Request $request): Response
{
$orderId = $request->attributes->get('id', '');
if (!$this->csrfTokenManager->isTokenValid(
new CsrfToken($orderId, (string) $request->query->get('_csrf_token', '')),
)) {
throw new HttpException(Response::HTTP_FORBIDDEN, 'Invalid csrf token.');
}
/** @var OrderInterface|null $order */
$order = $this->orderRepository->findOrderById($orderId);
if ($order === null) {
throw new NotFoundHttpException(sprintf('The order with id %s has not been found', $orderId));
}
$this->orderEmailManager->sendConfirmationEmail($order);
FlashBagProvider
::getFlashBag($this->requestStackOrSession)
->add('success', 'sylius.email.order_confirmation_resent')
;
return new RedirectResponse($request->headers->get('referer'));
}
}