Passed
Pull Request — master (#88)
by
unknown
04:17
created

ExportWishlistToCsvHandler::putDataToCsv()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 2
dl 0
loc 21
rs 9.8666
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
The type BitBag\SyliusWishlistPlu...list\AddWishlistProduct was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use BitBag\SyliusWishlistPlugin\Command\Wishlist\ExportWishlistToCsv;
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 CsvWishlistProductFactoryInterface $factory;
25
26
    private CsvSerializerFactoryInterface $csvSerializerFactory;
27
28
    private int $itemsProcessed = 0;
29
30
    public function __construct(
31
        CsvWishlistProductFactoryInterface $factory,
32
        CsvSerializerFactoryInterface $csvSerializerFactory
33
    ) {
34
        $this->factory = $factory;
35
        $this->csvSerializerFactory = $csvSerializerFactory;
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('bitbag_sylius_wishlist_plugin.ui.select_products');
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->csvSerializerFactory->createNew()->normalize($csvWishlistProduct, 'csv'));
69
            ++$this->itemsProcessed;
70
        }
71
72
        return $file;
73
    }
74
75
    private function createCsvWishlistProduct(AddWishlistProduct $wishlistProduct): CsvWishlistProductInterface
76
    {
77
        return $this->factory->createWithProperties(
78
            $wishlistProduct->getCartItem()->getCartItem()->getVariant()->getId(),
79
            $wishlistProduct->getWishlistProduct()->getProduct()->getId(),
80
            $wishlistProduct->getCartItem()->getCartItem()->getVariant()->getCode(),
81
        );
82
    }
83
}
84