|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BitBag\SyliusWishlistPlugin\Controller\Action; |
|
6
|
|
|
|
|
7
|
|
|
use BitBag\SyliusWishlistPlugin\Form\Type\ImportWishlistFromCsvType; |
|
8
|
|
|
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface; |
|
9
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; |
|
14
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
15
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
16
|
|
|
use Twig\Environment; |
|
17
|
|
|
|
|
18
|
|
|
final class ImportWishlistFromCsvAction |
|
19
|
|
|
{ |
|
20
|
|
|
private FormFactoryInterface $formFactory; |
|
21
|
|
|
|
|
22
|
|
|
private AddProductVariantToWishlistAction $addProductVariantToWishlistAction; |
|
23
|
|
|
|
|
24
|
|
|
private ProductVariantRepositoryInterface $productVariantRepository; |
|
25
|
|
|
|
|
26
|
|
|
private Environment $twigEnvironment; |
|
27
|
|
|
|
|
28
|
|
|
private FlashBagInterface $flashBag; |
|
29
|
|
|
|
|
30
|
|
|
private TranslatorInterface $translator; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct( |
|
33
|
|
|
FormFactoryInterface $formFactory, |
|
34
|
|
|
Environment $twigEnvironment, |
|
35
|
|
|
AddProductVariantToWishlistAction $addProductVariantToWishlistAction, |
|
36
|
|
|
FlashBagInterface $flashBag, |
|
37
|
|
|
ProductVariantRepositoryInterface $productVariantRepository, |
|
38
|
|
|
TranslatorInterface $translator |
|
39
|
|
|
) { |
|
40
|
|
|
$this->formFactory = $formFactory; |
|
41
|
|
|
$this->twigEnvironment = $twigEnvironment; |
|
42
|
|
|
$this->addProductVariantToWishlistAction = $addProductVariantToWishlistAction; |
|
43
|
|
|
$this->flashBag = $flashBag; |
|
44
|
|
|
$this->productVariantRepository = $productVariantRepository; |
|
45
|
|
|
$this->translator = $translator; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function __invoke(Request $request): Response |
|
49
|
|
|
{ |
|
50
|
|
|
$form = $this->formFactory->create(ImportWishlistFromCsvType::class); |
|
51
|
|
|
|
|
52
|
|
|
$form->handleRequest($request); |
|
53
|
|
|
|
|
54
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
55
|
|
|
$file = $form->get('wishlist_file')->getData(); |
|
56
|
|
|
|
|
57
|
|
|
if ($this->handleUploadedFile($file, $request)) { |
|
58
|
|
|
return $this->addProductVariantToWishlistAction->__invoke($request); |
|
59
|
|
|
} |
|
60
|
|
|
$this->flashBag->add( |
|
61
|
|
|
'error', |
|
62
|
|
|
$this->translator->trans('bitbag_sylius_wishlist_plugin.ui.upload_valid_csv') |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
return new Response( |
|
66
|
|
|
$this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/importWishlist.html.twig', [ |
|
67
|
|
|
'form' => $form->createView(), |
|
68
|
|
|
]) |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
foreach ($form->getErrors() as $error) { |
|
73
|
|
|
$this->flashBag->add('error', $error->getMessage()); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return new Response( |
|
77
|
|
|
$this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/importWishlist.html.twig', [ |
|
78
|
|
|
'form' => $form->createView(), |
|
79
|
|
|
]) |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
private function handleUploadedFile(UploadedFile $file, Request $request): bool |
|
84
|
|
|
{ |
|
85
|
|
|
$requestData = []; |
|
86
|
|
|
if ($this->isValidMimeType($file)) { |
|
87
|
|
|
$resource = fopen($file->getRealPath(), 'r'); |
|
88
|
|
|
|
|
89
|
|
|
while ($data = fgetcsv($resource, 1000, ',')) { |
|
90
|
|
|
if ($this->checkCsvProduct($data)) { |
|
91
|
|
|
$requestData[] = $data[0]; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
$request->attributes->set('variantId', $requestData); |
|
95
|
|
|
fclose($resource); |
|
96
|
|
|
} else { |
|
97
|
|
|
return false; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return true; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
private function isValidMimeType(UploadedFile $file): bool |
|
104
|
|
|
{ |
|
105
|
|
|
return 'text/csv' === $file->getClientMimeType() || 'application/octet-stream' === $file->getClientMimeType(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
private function checkCsvProduct(array $data): bool |
|
109
|
|
|
{ |
|
110
|
|
|
$variant = $this->productVariantRepository->find($data[0]); |
|
111
|
|
|
|
|
112
|
|
|
if (null === $variant) { |
|
113
|
|
|
throw new NotFoundHttpException(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
if ($data[1] == $variant->getProduct()->getId() && $data[2] == $variant->getCode()) { |
|
117
|
|
|
return true; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return false; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
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.