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

ExportWishlistToCsvHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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