Passed
Pull Request — master (#88)
by
unknown
06:48 queued 02:57
created

ExportWishlistToCsvHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 60
rs 10
c 1
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A createCsvWishlistProduct() 0 6 1
A __invoke() 0 12 2
A putDataToCsv() 0 22 3
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
    public function __construct(
32
        NormalizerInterface $normalizer,
33
        CsvWishlistProductFactoryInterface $factory
34
    ) {
35
        $this->normalizer = $normalizer;
36
        $this->factory = $factory;
37
    }
38
39
    public function __invoke(ExportWishlistToCsv $exportWishlistToCsv): \SplFileObject
40
    {
41
        $wishlistProducts = $exportWishlistToCsv->getWishlistProducts();
42
        $file = $exportWishlistToCsv->getFile();
43
44
        $fileObject = $this->putDataToCsv($wishlistProducts, $file);
45
46
        if (0 === $this->itemsProcessed) {
47
            throw new NoProductSelectedException('bitbag_sylius_wishlist_plugin.ui.select_products');
48
        }
49
50
        return $fileObject;
51
    }
52
53
    private function putDataToCsv(Collection $wishlistProducts, \SplFileObject $file): \SplFileObject
54
    {
55
        $csvHeaders = [
56
          'variantId',
57
          'productId',
58
          'variantCode',
59
        ];
60
61
        $file->fputcsv($csvHeaders);
62
63
        /** @var AddWishlistProduct $wishlistProduct */
64
        foreach ($wishlistProducts as $wishlistProduct) {
65
            if (!$wishlistProduct->isSelected()) {
66
                continue;
67
            }
68
            $csvWishlistProduct = $this->createCsvWishlistProduct($wishlistProduct);
69
            $file->fputcsv($this->normalizer->normalize($csvWishlistProduct, 'csv'));
70
71
            ++$this->itemsProcessed;
72
        }
73
74
        return $file;
75
    }
76
77
    private function createCsvWishlistProduct(AddWishlistProduct $wishlistProduct): CsvWishlistProductInterface
78
    {
79
        return $this->factory->createWithProperties(
80
            $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

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