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

ExportWishlistToCsvHandler::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 13
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 Doctrine\Common\Collections\Collection;
16
use Symfony\Component\HttpFoundation\RedirectResponse;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
19
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
20
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
21
use Symfony\Contracts\Translation\TranslatorInterface;
22
23
final class ExportWishlistToCsvHandler implements MessageHandlerInterface
24
{
25
    private TranslatorInterface $translator;
26
27
    private FlashBagInterface $flashBag;
28
29
    private UrlGeneratorInterface $urlGenerator;
30
31
    private int $itemsProcessed = 0;
32
33
    public function __construct(
34
        TranslatorInterface $translator,
35
        FlashBagInterface $flashBag,
36
        UrlGeneratorInterface $urlGenerator
37
    ) {
38
        $this->translator = $translator;
39
        $this->flashBag = $flashBag;
40
        $this->urlGenerator = $urlGenerator;
41
    }
42
43
    public function __invoke(ExportWishlistToCsv $exportWishlistToCsv): Response
44
    {
45
        $wishlistProducts = $exportWishlistToCsv->getWishlistProducts();
46
        $file = $exportWishlistToCsv->getFile();
47
48
        $this->putDataToCsv($wishlistProducts, $file);
49
50
        if (0 < $this->itemsProcessed) {
51
            return $this->returnCsvFileAsResponse($file);
52
        }
53
        $this->flashBag->add('error', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.select_products'));
54
55
        return new RedirectResponse($this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_list_products'));
56
    }
57
58
    private function putDataToCsv(Collection $wishlistProducts, \SplFileObject $file): void
59
    {
60
        /** @var AddWishlistProduct $wishlistProduct */
61
        foreach ($wishlistProducts as $wishlistProduct) {
62
            if (!$wishlistProduct->isSelected()) {
63
                continue;
64
            }
65
            $csvWishlistItem = [
66
                    $wishlistProduct->getCartItem()->getCartItem()->getVariant()->getId(),
67
                    $wishlistProduct->getWishlistProduct()->getProduct()->getId(),
68
                    $wishlistProduct->getCartItem()->getCartItem()->getVariant()->getCode(),
69
                ];
70
            $file->fputcsv($csvWishlistItem);
71
            ++$this->itemsProcessed;
72
        }
73
    }
74
75
    private function returnCsvFileAsResponse(\SplFileObject $file): Response
76
    {
77
        $file->rewind();
78
        $response = new Response($file->fread(5000));
79
80
        $response->headers->set('Content-Type', 'text/csv');
81
        $response->headers->set('Content-Disposition', 'attachment; filename=export.csv');
82
83
        return $response;
84
    }
85
}
86