Passed
Push — master ( 44aa55...07b576 )
by Przemysław eRIZ
04:29
created

ExportWishlistToCsvHandler::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 12
rs 10
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(),
0 ignored issues
show
Bug introduced by
The method getVariant() does not exist on Sylius\Component\Order\Model\OrderItemInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Sylius\Component\Order\Model\OrderItem. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
            $wishlistProduct->getCartItem()->getCartItem()->/** @scrutinizer ignore-call */ getVariant()->getId(),
Loading history...
79
            $wishlistProduct->getWishlistProduct()->getProduct()->getId(),
80
            $wishlistProduct->getCartItem()->getCartItem()->getVariant()->getCode(),
81
        );
82
    }
83
}
84