Passed
Pull Request — master (#88)
by
unknown
05:28
created

ExportWishlistToCsvHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A exportToCSV() 0 20 4
A __invoke() 0 19 2
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 Symfony\Component\HttpFoundation\RedirectResponse;
16
use Symfony\Component\HttpFoundation\Response;
17
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
18
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
19
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
20
use Symfony\Contracts\Translation\TranslatorInterface;
21
22
final class ExportWishlistToCsvHandler implements MessageHandlerInterface
23
{
24
    private TranslatorInterface $translator;
25
26
    private FlashBagInterface $flashBag;
27
28
    private UrlGeneratorInterface $urlGenerator;
29
30
    public function __construct(
31
        TranslatorInterface $translator,
32
        FlashBagInterface $flashBag,
33
        UrlGeneratorInterface $urlGenerator
34
    ) {
35
        $this->translator = $translator;
36
        $this->flashBag = $flashBag;
37
        $this->urlGenerator = $urlGenerator;
38
    }
39
40
    public function __invoke(ExportWishlistToCsv $exportWishlistToCsv): Response
41
    {
42
        $wishlistProducts = $exportWishlistToCsv->getWishlistProducts();
43
        $file = $exportWishlistToCsv->getFile();
44
45
        if ($this->exportToCSV($wishlistProducts, $file)) {
46
            $this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.selected_wishlist_items_successfully_exported'));
47
            rewind($file);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type boolean; however, parameter $stream of rewind() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
            rewind(/** @scrutinizer ignore-type */ $file);
Loading history...
48
            $response = new Response(stream_get_contents($file));
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type boolean; however, parameter $stream of stream_get_contents() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
            $response = new Response(stream_get_contents(/** @scrutinizer ignore-type */ $file));
Loading history...
49
            fclose($file);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type boolean; however, parameter $stream of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
            fclose(/** @scrutinizer ignore-type */ $file);
Loading history...
50
51
            $response->headers->set('Content-Type', 'text/csv');
52
            $response->headers->set('Content-Disposition', 'attachment; filename=export.csv');
53
54
            return $response;
55
        }
56
        $this->flashBag->add('error', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.select_products'));
57
58
        return new RedirectResponse($this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_list_products'));
59
    }
60
61
    private function exportToCSV(array $wishlistProductsCommands, $file): bool
62
    {
63
        $result = false;
64
65
        foreach ($wishlistProductsCommands as $wishlistProducts) {
66
            /** @var AddWishlistProduct $wishlistProduct */
67
            foreach ($wishlistProducts as $wishlistProduct) {
68
                if ($wishlistProduct->isSelected()) {
69
                    $result = true;
70
                    $csvWishlistItem = [
71
                        $wishlistProduct->getCartItem()->getCartItem()->getVariant()->getId(),
72
                        $wishlistProduct->getWishlistProduct()->getProduct()->getId(),
73
                        $wishlistProduct->getCartItem()->getCartItem()->getVariant()->getCode(),
74
                    ];
75
                    fputcsv($file, $csvWishlistItem);
76
                }
77
            }
78
        }
79
80
        return $result;
81
    }
82
}
83