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

ImportWishlistFromCsvHandler::handleUploadedFile()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 18
rs 9.9
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\ImportWishlistFromCsv;
14
use BitBag\SyliusWishlistPlugin\Controller\Action\AddProductVariantToWishlistAction;
15
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
16
use Symfony\Component\HttpFoundation\File\UploadedFile;
17
use Symfony\Component\HttpFoundation\RedirectResponse;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
21
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
22
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
23
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
24
use Symfony\Contracts\Translation\TranslatorInterface;
25
26
final class ImportWishlistFromCsvHandler implements MessageHandlerInterface
27
{
28
    private AddProductVariantToWishlistAction $addProductVariantToWishlistAction;
29
30
    private FlashBagInterface $flashBag;
31
32
    private TranslatorInterface $translator;
33
34
    private ProductVariantRepositoryInterface $productVariantRepository;
35
36
    private UrlGeneratorInterface $urlGenerator;
37
38
    public function __construct(
39
        AddProductVariantToWishlistAction $addProductVariantToWishlistAction,
40
        FlashBagInterface $flashBag,
41
        TranslatorInterface $translator,
42
        ProductVariantRepositoryInterface $productVariantRepository,
43
        UrlGeneratorInterface $urlGenerator
44
    ) {
45
        $this->addProductVariantToWishlistAction = $addProductVariantToWishlistAction;
46
        $this->flashBag = $flashBag;
47
        $this->translator = $translator;
48
        $this->productVariantRepository = $productVariantRepository;
49
        $this->urlGenerator = $urlGenerator;
50
    }
51
52
    public function __invoke(ImportWishlistFromCsv $importWishlistFromCsv): Response
53
    {
54
        $file = $importWishlistFromCsv->getFile();
55
        $request = $importWishlistFromCsv->getRequest();
56
57
        if ($this->handleUploadedFile($file, $request)) {
58
            return $this->addProductVariantToWishlistAction->__invoke($request);
59
        }
60
        $this->flashBag->add('error', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.upload_valid_csv'));
61
62
        return new RedirectResponse($this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_import_from_csv'));
63
    }
64
65
    private function handleUploadedFile(UploadedFile $file, Request $request): bool
66
    {
67
        $requestData = [];
68
        if ($this->isValidMimeType($file)) {
69
            $resource = fopen($file->getRealPath(), 'r');
70
71
            while ($data = fgetcsv($resource, 1000, ',')) {
72
                if ($this->checkCsvProduct($data)) {
73
                    $requestData[] = $data[0];
74
                }
75
            }
76
            $request->attributes->set('variantId', $requestData);
77
            fclose($resource);
78
        } else {
79
            return false;
80
        }
81
82
        return true;
83
    }
84
85
    private function isValidMimeType(UploadedFile $file): bool
86
    {
87
        return 'text/csv' === $file->getClientMimeType() || 'application/octet-stream' === $file->getClientMimeType();
88
    }
89
90
    private function checkCsvProduct(array $data): bool
91
    {
92
        $variant = $this->productVariantRepository->find($data[0]);
93
94
        if (null === $variant) {
95
            throw new NotFoundHttpException();
96
        }
97
98
        if ($data[1] == $variant->getProduct()->getId() && $data[2] == $variant->getCode()) {
99
            return true;
100
        }
101
102
        return false;
103
    }
104
}
105