|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file was created by developers working at BitBag |
|
5
|
|
|
* Do you need more information about us and what we do? Visit our https://bitbag.io website! |
|
6
|
|
|
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist; |
|
12
|
|
|
|
|
13
|
|
|
use BitBag\SyliusWishlistPlugin\Command\Wishlist\ImportWishlistFromCsv; |
|
14
|
|
|
use BitBag\SyliusWishlistPlugin\Controller\Action\AddProductVariantToWishlistAction; |
|
15
|
|
|
use Gedmo\Exception\UploadableInvalidMimeTypeException; |
|
16
|
|
|
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
19
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
20
|
|
|
use Symfony\Component\Messenger\Handler\MessageHandlerInterface; |
|
21
|
|
|
|
|
22
|
|
|
final class ImportWishlistFromCsvHandler implements MessageHandlerInterface |
|
23
|
|
|
{ |
|
24
|
|
|
private AddProductVariantToWishlistAction $addProductVariantToWishlistAction; |
|
25
|
|
|
|
|
26
|
|
|
private ProductVariantRepositoryInterface $productVariantRepository; |
|
27
|
|
|
|
|
28
|
|
|
private array $allowedMimeTypes; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct( |
|
31
|
|
|
AddProductVariantToWishlistAction $addProductVariantToWishlistAction, |
|
32
|
|
|
ProductVariantRepositoryInterface $productVariantRepository, |
|
33
|
|
|
array $allowedMimeTypes |
|
34
|
|
|
) { |
|
35
|
|
|
$this->addProductVariantToWishlistAction = $addProductVariantToWishlistAction; |
|
36
|
|
|
$this->productVariantRepository = $productVariantRepository; |
|
37
|
|
|
$this->allowedMimeTypes = $allowedMimeTypes; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function __invoke(ImportWishlistFromCsv $importWishlistFromCsv): Response |
|
41
|
|
|
{ |
|
42
|
|
|
$fileInfo = $importWishlistFromCsv->getFileInfo(); |
|
43
|
|
|
$request = $importWishlistFromCsv->getRequest(); |
|
44
|
|
|
|
|
45
|
|
|
$this->getDataFromFile($fileInfo, $request); |
|
46
|
|
|
|
|
47
|
|
|
return $this->addProductVariantToWishlistAction->__invoke($request); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
private function getDataFromFile(\SplFileInfo $fileInfo, Request $request): void |
|
51
|
|
|
{ |
|
52
|
|
|
$requestData = []; |
|
53
|
|
|
|
|
54
|
|
|
if (!$this->fileIsValidMimeType($fileInfo)) { |
|
55
|
|
|
throw new UploadableInvalidMimeTypeException(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$resource = fopen($fileInfo->getRealPath(), 'r'); |
|
59
|
|
|
|
|
60
|
|
|
while ($data = fgetcsv($resource, 1000, ',')) { |
|
61
|
|
|
if ($this->csvContainValidProduct($data)) { |
|
62
|
|
|
$requestData[] = $data[0]; |
|
63
|
|
|
$request->attributes->set('variantId', $requestData); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
fclose($resource); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
private function fileIsValidMimeType(\SplFileInfo $fileInfo): bool |
|
70
|
|
|
{ |
|
71
|
|
|
$finfo = new \finfo(\FILEINFO_MIME_TYPE); |
|
72
|
|
|
|
|
73
|
|
|
return in_array($finfo->file($fileInfo->getRealPath()), $this->allowedMimeTypes); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
private function csvContainValidProduct(array $data): bool |
|
77
|
|
|
{ |
|
78
|
|
|
if (!array_diff(['0', '1', '2'], array_keys($data))) { |
|
79
|
|
|
$variantId = $data[0]; |
|
80
|
|
|
$productId = $data[1]; |
|
81
|
|
|
$variantCode = $data[2]; |
|
82
|
|
|
|
|
83
|
|
|
$variant = $this->productVariantRepository->find($variantId); |
|
84
|
|
|
|
|
85
|
|
|
if (null === $variant) { |
|
86
|
|
|
throw new NotFoundHttpException(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if ((string) $variant->getProduct()->getId() === $productId && $variant->getCode() === $variantCode) { |
|
90
|
|
|
return true; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return false; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|