1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitBag\SyliusWishlistPlugin\Controller\Action; |
6
|
|
|
|
7
|
|
|
use BitBag\SyliusWishlistPlugin\Command\Wishlist\ExportWishlistToCsv; |
8
|
|
|
use BitBag\SyliusWishlistPlugin\Context\WishlistContextInterface; |
9
|
|
|
use BitBag\SyliusWishlistPlugin\Exception\NoProductSelectedException; |
10
|
|
|
use BitBag\SyliusWishlistPlugin\Form\Type\WishlistCollectionType; |
11
|
|
|
use BitBag\SyliusWishlistPlugin\Processor\WishlistCommandProcessorInterface; |
12
|
|
|
use Sylius\Component\Order\Context\CartContextInterface; |
13
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
14
|
|
|
use Symfony\Component\Form\FormInterface; |
15
|
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse; |
16
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
19
|
|
|
use Symfony\Component\HttpFoundation\ResponseHeaderBag; |
20
|
|
|
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; |
21
|
|
|
use Symfony\Component\Messenger\HandleTrait; |
22
|
|
|
use Symfony\Component\Messenger\MessageBusInterface; |
23
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
24
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
25
|
|
|
|
26
|
|
|
final class ExportSelectedProductsToCsvAction |
27
|
|
|
{ |
28
|
|
|
use HandleTrait; |
29
|
|
|
|
30
|
|
|
private WishlistContextInterface $wishlistContext; |
31
|
|
|
|
32
|
|
|
private CartContextInterface $cartContext; |
33
|
|
|
|
34
|
|
|
private FormFactoryInterface $formFactory; |
35
|
|
|
|
36
|
|
|
private FlashBagInterface $flashBag; |
37
|
|
|
|
38
|
|
|
private WishlistCommandProcessorInterface $wishlistCommandProcessor; |
39
|
|
|
|
40
|
|
|
private UrlGeneratorInterface $urlGenerator; |
41
|
|
|
|
42
|
|
|
private TranslatorInterface $translator; |
43
|
|
|
|
44
|
|
|
public function __construct( |
45
|
|
|
WishlistContextInterface $wishlistContext, |
46
|
|
|
CartContextInterface $cartContext, |
47
|
|
|
FormFactoryInterface $formFactory, |
48
|
|
|
FlashBagInterface $flashBag, |
49
|
|
|
MessageBusInterface $messageBus, |
50
|
|
|
WishlistCommandProcessorInterface $wishlistCommandProcessor, |
51
|
|
|
UrlGeneratorInterface $urlGenerator, |
52
|
|
|
TranslatorInterface $translator |
53
|
|
|
) { |
54
|
|
|
$this->wishlistContext = $wishlistContext; |
55
|
|
|
$this->cartContext = $cartContext; |
56
|
|
|
$this->formFactory = $formFactory; |
57
|
|
|
$this->flashBag = $flashBag; |
58
|
|
|
$this->messageBus = $messageBus; |
59
|
|
|
$this->wishlistCommandProcessor = $wishlistCommandProcessor; |
60
|
|
|
$this->urlGenerator = $urlGenerator; |
61
|
|
|
$this->translator = $translator; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function __invoke(Request $request): Response |
65
|
|
|
{ |
66
|
|
|
$form = $this->createForm($request); |
67
|
|
|
|
68
|
|
|
$form->handleRequest($request); |
69
|
|
|
|
70
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
71
|
|
|
return $this->exportSelectedWishlistProductsToCsv($form); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
foreach ($form->getErrors() as $error) { |
75
|
|
|
$this->flashBag->add('error', $error->getMessage()); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return new RedirectResponse($this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_list_products')); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function createForm(Request $request): FormInterface |
82
|
|
|
{ |
83
|
|
|
$wishlist = $this->wishlistContext->getWishlist($request); |
84
|
|
|
$cart = $this->cartContext->getCart(); |
85
|
|
|
|
86
|
|
|
$commandsArray = $this->wishlistCommandProcessor->createAddCommandCollectionFromWishlistProducts($wishlist->getWishlistProducts()); |
87
|
|
|
|
88
|
|
|
return $this->formFactory->create(WishlistCollectionType::class, ['items' => $commandsArray], [ |
89
|
|
|
'cart' => $cart, |
90
|
|
|
]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function exportSelectedWishlistProductsToCsv(FormInterface $form): Response |
94
|
|
|
{ |
95
|
|
|
try { |
96
|
|
|
/** @var \SplFileObject $file */ |
97
|
|
|
$file = $this->getCsvFileFromWishlistProducts($form); |
98
|
|
|
} catch (NoProductSelectedException $e) { |
99
|
|
|
$this->flashBag->add('error', $this->translator->trans($e->getMessage())); |
100
|
|
|
|
101
|
|
|
return new RedirectResponse($this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_list_products')); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->returnCsvFile($file); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function getCsvFileFromWishlistProducts(FormInterface $form): \SplFileObject |
108
|
|
|
{ |
109
|
|
|
$file = new \SplFileObject('export.csv', 'w+'); |
110
|
|
|
$command = new ExportWishlistToCsv($form->get('items')->getData(), $file); |
111
|
|
|
|
112
|
|
|
return $this->handle($command); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function returnCsvFile(\SplFileObject $file): Response |
116
|
|
|
{ |
117
|
|
|
$file->rewind(); |
118
|
|
|
|
119
|
|
|
$response = new BinaryFileResponse($file); |
120
|
|
|
|
121
|
|
|
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $file->getFilename()); |
122
|
|
|
$response->headers->set('Content-Type', 'text/csv'); |
123
|
|
|
$response->deleteFileAfterSend(true); |
124
|
|
|
|
125
|
|
|
return $response; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.