Passed
Pull Request — master (#88)
by
unknown
07:34 queued 03:14
created

ExportWishlistToCsvHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

84
            $wishlistProduct->getCartItem()->getCartItem()->/** @scrutinizer ignore-call */ getVariant()->getId(),
Loading history...
85
            $wishlistProduct->getWishlistProduct()->getProduct()->getId(),
86
            $wishlistProduct->getCartItem()->getCartItem()->getVariant()->getCode(),
87
        );
88
    }
89
}
90