Passed
Pull Request — master (#88)
by
unknown
03:40
created

createCsvWishlistProduct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
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\AddWishlistProduct;
14
use BitBag\SyliusWishlistPlugin\Command\Wishlist\ExportWishlistToCsv;
15
use BitBag\SyliusWishlistPlugin\Exception\NoProductSelectedException;
16
use BitBag\SyliusWishlistPlugin\Factory\CsvWishlistProductFactoryInterface;
17
use BitBag\SyliusWishlistPlugin\Model\DTO\CsvWishlistProduct;
18
use BitBag\SyliusWishlistPlugin\Model\DTO\CsvWishlistProductInterface;
19
use Doctrine\Common\Collections\Collection;
20
use Sylius\Component\Resource\Factory\FactoryInterface;
21
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
22
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
23
24
final class ExportWishlistToCsvHandler implements MessageHandlerInterface
25
{
26
    private NormalizerInterface $normalizer;
27
28
    private CsvWishlistProductFactoryInterface $factory;
29
30
    private int $itemsProcessed = 0;
31
32
    public function __construct(NormalizerInterface $normalizer, CsvWishlistProductFactoryInterface $factory)
33
    {
34
        $this->normalizer = $normalizer;
35
        $this->factory = $factory;
36
    }
37
38
    public function __invoke(ExportWishlistToCsv $exportWishlistToCsv): \SplFileObject
39
    {
40
        $wishlistProducts = $exportWishlistToCsv->getWishlistProducts();
41
        $file = $exportWishlistToCsv->getFile();
42
43
        $fileObject = $this->putDataToCsv($wishlistProducts, $file);
44
45
        if (0 === $this->itemsProcessed) {
46
            throw new NoProductSelectedException();
47
        }
48
49
        return $fileObject;
50
    }
51
52
    private function putDataToCsv(Collection $wishlistProducts, \SplFileObject $file): \SplFileObject
53
    {
54
        $csvHeaders = [
55
          'variantId',
56
          'productId',
57
          'variantCode',
58
        ];
59
60
        $file->fputcsv($csvHeaders);
61
62
        /** @var AddWishlistProduct $wishlistProduct */
63
        foreach ($wishlistProducts as $wishlistProduct) {
64
            if (!$wishlistProduct->isSelected()) {
65
                continue;
66
            }
67
            $csvWishlistProduct = $this->createCsvWishlistProduct($wishlistProduct);
68
            $file->fputcsv($this->normalizer->normalize($csvWishlistProduct, 'csv'));
69
70
            ++$this->itemsProcessed;
71
        }
72
73
        return $file;
74
    }
75
76
    private function createCsvWishlistProduct(AddWishlistProduct $wishlistProduct): CsvWishlistProductInterface
77
    {
78
        return $this->factory->createWithProperties(
79
            $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

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