|
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 BitBag\SyliusWishlistPlugin\Factory\CsvSerializerFactoryInterface; |
|
16
|
|
|
use BitBag\SyliusWishlistPlugin\Model\DTO\CsvWishlistProduct; |
|
17
|
|
|
use BitBag\SyliusWishlistPlugin\Model\DTO\CsvWishlistProductInterface; |
|
18
|
|
|
use Gedmo\Exception\UploadableInvalidMimeTypeException; |
|
19
|
|
|
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
21
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
22
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
23
|
|
|
use Symfony\Component\Messenger\Handler\MessageHandlerInterface; |
|
24
|
|
|
use Symfony\Component\Serializer\Encoder\CsvEncoder; |
|
25
|
|
|
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer; |
|
26
|
|
|
|
|
27
|
|
|
final class ImportWishlistFromCsvHandler implements MessageHandlerInterface |
|
28
|
|
|
{ |
|
29
|
|
|
private AddProductVariantToWishlistAction $addProductVariantToWishlistAction; |
|
30
|
|
|
|
|
31
|
|
|
private ProductVariantRepositoryInterface $productVariantRepository; |
|
32
|
|
|
|
|
33
|
|
|
private array $allowedMimeTypes; |
|
34
|
|
|
|
|
35
|
|
|
private CsvSerializerFactoryInterface $csvSerializerFactory; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct( |
|
38
|
|
|
AddProductVariantToWishlistAction $addProductVariantToWishlistAction, |
|
39
|
|
|
ProductVariantRepositoryInterface $productVariantRepository, |
|
40
|
|
|
array $allowedMimeTypes, |
|
41
|
|
|
CsvSerializerFactoryInterface $csvSerializerFactory |
|
42
|
|
|
) { |
|
43
|
|
|
$this->addProductVariantToWishlistAction = $addProductVariantToWishlistAction; |
|
44
|
|
|
$this->productVariantRepository = $productVariantRepository; |
|
45
|
|
|
$this->allowedMimeTypes = $allowedMimeTypes; |
|
46
|
|
|
$this->csvSerializerFactory = $csvSerializerFactory; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function __invoke(ImportWishlistFromCsv $importWishlistFromCsv): Response |
|
50
|
|
|
{ |
|
51
|
|
|
$fileInfo = $importWishlistFromCsv->getFileInfo(); |
|
52
|
|
|
$request = $importWishlistFromCsv->getRequest(); |
|
53
|
|
|
|
|
54
|
|
|
$this->getDataFromFile($fileInfo, $request); |
|
55
|
|
|
|
|
56
|
|
|
return $this->addProductVariantToWishlistAction->__invoke($request); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
private function getDataFromFile(\SplFileInfo $fileInfo, Request $request): void |
|
60
|
|
|
{ |
|
61
|
|
|
if (!$this->fileIsValidMimeType($fileInfo)) { |
|
62
|
|
|
throw new UploadableInvalidMimeTypeException(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$csvData = file_get_contents((string) $fileInfo); |
|
66
|
|
|
|
|
67
|
|
|
$csvWishlistProducts = $this->csvSerializerFactory->createNew()->deserialize($csvData, sprintf('%s[]', CsvWishlistProduct::class), 'csv', [ |
|
68
|
|
|
AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true, |
|
69
|
|
|
CsvEncoder::AS_COLLECTION_KEY => true, |
|
70
|
|
|
]); |
|
71
|
|
|
|
|
72
|
|
|
/** @var CsvWishlistProduct $csvWishlistProduct */ |
|
73
|
|
|
foreach ($csvWishlistProducts as $csvWishlistProduct) { |
|
74
|
|
|
if (!$this->csvWishlistProductIsValid($csvWishlistProduct)) { |
|
75
|
|
|
return; |
|
76
|
|
|
} |
|
77
|
|
|
$variantIdRequestAttributes[] = $csvWishlistProduct->getVariantId(); |
|
78
|
|
|
} |
|
79
|
|
|
$request->attributes->set('variantId', $variantIdRequestAttributes); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private function fileIsValidMimeType(\SplFileInfo $fileInfo): bool |
|
83
|
|
|
{ |
|
84
|
|
|
$finfo = new \finfo(\FILEINFO_MIME_TYPE); |
|
85
|
|
|
|
|
86
|
|
|
return in_array($finfo->file($fileInfo->getRealPath()), $this->allowedMimeTypes); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function csvWishlistProductIsValid(CsvWishlistProductInterface $csvWishlistProduct): bool |
|
90
|
|
|
{ |
|
91
|
|
|
$wishlistProduct = $this->productVariantRepository->findOneBy([ |
|
92
|
|
|
'id' => $csvWishlistProduct->getVariantId(), |
|
93
|
|
|
'product' => $csvWishlistProduct->getProductId(), |
|
94
|
|
|
'code' => $csvWishlistProduct->getVariantCode(), |
|
95
|
|
|
]); |
|
96
|
|
|
|
|
97
|
|
|
if (null === $wishlistProduct) { |
|
98
|
|
|
throw new NotFoundHttpException(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return true; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|