|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusMailchimpPlugin\Controller\Action; |
|
6
|
|
|
|
|
7
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
|
8
|
|
|
use Sylius\Component\Core\OrderPaymentStates; |
|
9
|
|
|
use Sylius\Component\Core\Repository\OrderRepositoryInterface; |
|
10
|
|
|
use Sylius\Component\Core\Storage\CartStorageInterface; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
14
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
15
|
|
|
use Webmozart\Assert\Assert; |
|
16
|
|
|
|
|
17
|
|
|
final class ContinueCartPurchaseAction |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var CartStorageInterface */ |
|
20
|
|
|
private $cartStorage; |
|
21
|
|
|
|
|
22
|
|
|
/** @var OrderRepositoryInterface */ |
|
23
|
|
|
private $orderRepository; |
|
24
|
|
|
|
|
25
|
|
|
/** @var RouterInterface */ |
|
26
|
|
|
private $router; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct( |
|
29
|
|
|
CartStorageInterface $cartStorage, |
|
30
|
|
|
OrderRepositoryInterface $orderRepository, |
|
31
|
|
|
RouterInterface $router |
|
32
|
|
|
) { |
|
33
|
|
|
$this->cartStorage = $cartStorage; |
|
34
|
|
|
$this->orderRepository = $orderRepository; |
|
35
|
|
|
$this->router = $router; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param Request $request |
|
40
|
|
|
* @return Response |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __invoke(Request $request): Response |
|
43
|
|
|
{ |
|
44
|
|
|
$tokenValue = $request->get('tokenValue'); |
|
45
|
|
|
/** @var OrderInterface $order */ |
|
46
|
|
|
$order = $this->orderRepository->findOneBy([ |
|
47
|
|
|
'tokenValue' => $tokenValue, |
|
48
|
|
|
]); |
|
49
|
|
|
|
|
50
|
|
|
Assert::notNull($order); |
|
51
|
|
|
|
|
52
|
|
|
// If the order state is paid |
|
53
|
|
|
if (OrderPaymentStates::STATE_PAID === $order->getPaymentState()) { |
|
54
|
|
|
return new RedirectResponse($this->router->generate('sylius_shop_order_show', [ |
|
55
|
|
|
'tokenValue' => $order->getTokenValue(), |
|
56
|
|
|
])); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// If the order is in state cart with a valid token value |
|
60
|
|
|
if (OrderInterface::STATE_CART !== $order->getState() && $order->getTokenValue()) { |
|
61
|
|
|
return new RedirectResponse($this->router->generate('sylius_shop_order_pay', [ |
|
62
|
|
|
'tokenValue' => $order->getTokenValue(), |
|
63
|
|
|
])); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$this->cartStorage->setForChannel($order->getChannel(), $order); |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
return new RedirectResponse($this->router->generate('sylius_shop_checkout_start')); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|