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

ImportWishlistFromCsvHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
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\ImportWishlistFromCsv;
14
use BitBag\SyliusWishlistPlugin\Controller\Action\AddProductVariantToWishlistAction;
15
use Gedmo\Exception\UploadableInvalidMimeTypeException;
16
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
20
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
21
22
final class ImportWishlistFromCsvHandler implements MessageHandlerInterface
23
{
24
    private const ALLOWED_MIME_TYPES = [
25
        'text/csv',
26
        'text/plain',
27
        'application/csv',
28
        'text/comma-separated-values',
29
        'application/excel',
30
        'application/vnd.ms-excel',
31
        'application/vnd.msexcel',
32
        'text/anytext',
33
        'application/octet-stream',
34
        'application/txt',
35
    ];
36
37
    private AddProductVariantToWishlistAction $addProductVariantToWishlistAction;
38
39
    private ProductVariantRepositoryInterface $productVariantRepository;
40
41
    public function __construct(
42
        AddProductVariantToWishlistAction $addProductVariantToWishlistAction,
43
        ProductVariantRepositoryInterface $productVariantRepository
44
    ) {
45
        $this->addProductVariantToWishlistAction = $addProductVariantToWishlistAction;
46
        $this->productVariantRepository = $productVariantRepository;
47
    }
48
49
    public function __invoke(ImportWishlistFromCsv $importWishlistFromCsv): Response
50
    {
51
        $fileInfo = $importWishlistFromCsv->getFileInfo();
52
        $request = $importWishlistFromCsv->getRequest();
53
54
        $this->getDataFromFile($fileInfo, $request);
55
56
        return $this->addProductVariantToWishlistAction->__invoke($request);
57
    }
58
59
    private function getDataFromFile(\SplFileInfo $fileInfo, Request $request): void
60
    {
61
        $requestData = [];
62
63
        if (!$this->fileIsValidMimeType($fileInfo)) {
64
            throw new UploadableInvalidMimeTypeException();
65
        }
66
67
        $resource = fopen($fileInfo->getRealPath(), 'r');
68
69
        while ($data = fgetcsv($resource, 1000, ',')) {
70
            if ($this->csvContainValidProduct($data)) {
71
                $requestData[] = $data[0];
72
                $request->attributes->set('variantId', $requestData);
73
            }
74
        }
75
        fclose($resource);
76
    }
77
78
    private function fileIsValidMimeType(\SplFileInfo $fileInfo): bool
79
    {
80
        $finfo = new \finfo(\FILEINFO_MIME_TYPE);
81
82
        return in_array($finfo->file($fileInfo->getRealPath()), self::ALLOWED_MIME_TYPES);
83
    }
84
85
    private function csvContainValidProduct(array $data): bool
86
    {
87
        $variantId = $data[0];
88
        $productId = $data[1];
89
        $variantCode = $data[2];
90
91
        $variant = $this->productVariantRepository->find($variantId);
92
93
        if (null === $variant) {
94
            throw new NotFoundHttpException();
95
        }
96
97
        if ((string) $variant->getProduct()->getId() === $productId && $variant->getCode() === $variantCode) {
98
            return true;
99
        }
100
101
        return false;
102
    }
103
}
104