|
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\ExportWishlistToCsv; |
|
14
|
|
|
use BitBag\SyliusWishlistPlugin\Command\Wishlist\WishlistItemInterface; |
|
15
|
|
|
use BitBag\SyliusWishlistPlugin\Exception\NoProductSelectedException; |
|
16
|
|
|
use BitBag\SyliusWishlistPlugin\Factory\CsvSerializerFactoryInterface; |
|
17
|
|
|
use BitBag\SyliusWishlistPlugin\Factory\CsvWishlistProductFactoryInterface; |
|
18
|
|
|
use BitBag\SyliusWishlistPlugin\Model\DTO\CsvWishlistProductInterface; |
|
19
|
|
|
use Doctrine\Common\Collections\Collection; |
|
20
|
|
|
use Symfony\Component\Messenger\Handler\MessageHandlerInterface; |
|
21
|
|
|
|
|
22
|
|
|
final class ExportWishlistToCsvHandler implements MessageHandlerInterface |
|
23
|
|
|
{ |
|
24
|
|
|
private const CSV_HEADERS = [ |
|
25
|
|
|
'variantId', |
|
26
|
|
|
'productId', |
|
27
|
|
|
'variantCode', |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
private CsvWishlistProductFactoryInterface $factory; |
|
31
|
|
|
|
|
32
|
|
|
private CsvSerializerFactoryInterface $csvSerializerFactory; |
|
33
|
|
|
|
|
34
|
|
|
private int $itemsProcessed = 0; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct( |
|
37
|
|
|
CsvWishlistProductFactoryInterface $factory, |
|
38
|
|
|
CsvSerializerFactoryInterface $csvSerializerFactory |
|
39
|
|
|
) { |
|
40
|
|
|
$this->factory = $factory; |
|
41
|
|
|
$this->csvSerializerFactory = $csvSerializerFactory; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function __invoke(ExportWishlistToCsv $exportWishlistToCsv): \SplFileObject |
|
45
|
|
|
{ |
|
46
|
|
|
$wishlistProducts = $exportWishlistToCsv->getWishlistProducts(); |
|
47
|
|
|
$file = $exportWishlistToCsv->getFile(); |
|
48
|
|
|
|
|
49
|
|
|
$fileObject = $this->putDataToCsv($wishlistProducts, $file); |
|
50
|
|
|
|
|
51
|
|
|
if (0 === $this->itemsProcessed) { |
|
52
|
|
|
throw new NoProductSelectedException('bitbag_sylius_wishlist_plugin.ui.select_products'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $fileObject; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private function putDataToCsv(Collection $wishlistProducts, \SplFileObject $file): \SplFileObject |
|
59
|
|
|
{ |
|
60
|
|
|
$file->fputcsv(self::CSV_HEADERS); |
|
61
|
|
|
|
|
62
|
|
|
/** @var WishlistItemInterface $wishlistProduct */ |
|
63
|
|
|
foreach ($wishlistProducts as $wishlistProduct) { |
|
64
|
|
|
if (!$wishlistProduct->isSelected()) { |
|
65
|
|
|
continue; |
|
66
|
|
|
} |
|
67
|
|
|
$csvWishlistProduct = $this->createCsvWishlistProduct($wishlistProduct); |
|
68
|
|
|
$file->fputcsv($this->csvSerializerFactory->createNew()->normalize($csvWishlistProduct, 'csv')); |
|
69
|
|
|
++$this->itemsProcessed; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $file; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private function createCsvWishlistProduct(WishlistItemInterface $wishlistProduct): CsvWishlistProductInterface |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->factory->createWithProperties( |
|
78
|
|
|
$wishlistProduct->getCartItem()->getCartItem()->getVariant()->getId(), |
|
|
|
|
|
|
79
|
|
|
$wishlistProduct->getWishlistProduct()->getProduct()->getId(), |
|
80
|
|
|
$wishlistProduct->getCartItem()->getCartItem()->getVariant()->getCode(), |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|