Passed
Pull Request — master (#88)
by
unknown
15:35
created

ImportWishlistFromCsvHandler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 77
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A csvWishlistProductIsValid() 0 13 2
A fileIsValidMimeType() 0 5 1
A __invoke() 0 8 1
A getDataFromFile() 0 19 4
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 BitBag\SyliusWishlistPlugin\Model\DTO\CsvWishlistProduct;
16
use BitBag\SyliusWishlistPlugin\Model\DTO\CsvWishlistProductInterface;
17
use Gedmo\Exception\UploadableInvalidMimeTypeException;
18
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
22
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
23
use Symfony\Component\Serializer\Encoder\DecoderInterface;
24
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
25
26
final class ImportWishlistFromCsvHandler implements MessageHandlerInterface
27
{
28
    private AddProductVariantToWishlistAction $addProductVariantToWishlistAction;
29
30
    private ProductVariantRepositoryInterface $productVariantRepository;
31
32
    private DecoderInterface $decoder;
33
34
    private DenormalizerInterface $denormalizer;
35
36
    private array $allowedMimeTypes;
37
38
    public function __construct(
39
        AddProductVariantToWishlistAction $addProductVariantToWishlistAction,
40
        ProductVariantRepositoryInterface $productVariantRepository,
41
        DecoderInterface $serializer,
42
        array $allowedMimeTypes,
43
        DenormalizerInterface $denormalizer
44
    ) {
45
        $this->addProductVariantToWishlistAction = $addProductVariantToWishlistAction;
46
        $this->productVariantRepository = $productVariantRepository;
47
        $this->allowedMimeTypes = $allowedMimeTypes;
48
        $this->decoder = $serializer;
49
        $this->denormalizer = $denormalizer;
50
    }
51
52
    public function __invoke(ImportWishlistFromCsv $importWishlistFromCsv): Response
53
    {
54
        $fileInfo = $importWishlistFromCsv->getFileInfo();
55
        $request = $importWishlistFromCsv->getRequest();
56
57
        $this->getDataFromFile($fileInfo, $request);
58
59
        return $this->addProductVariantToWishlistAction->__invoke($request);
60
    }
61
62
    private function getDataFromFile(\SplFileInfo $fileInfo, Request $request): void
63
    {
64
        if (!$this->fileIsValidMimeType($fileInfo)) {
65
            throw new UploadableInvalidMimeTypeException();
66
        }
67
68
        $wishlistProductsArray = $this->decoder->decode(file_get_contents((string) $fileInfo), 'csv');
69
70
        foreach ($wishlistProductsArray as $wishlistProductArray) {
71
            /** @var CsvWishlistProduct $csvWishlistProduct */
72
            $csvWishlistProduct = $this->denormalizer->denormalize($wishlistProductArray, CsvWishlistProduct::class, 'csv');
73
74
            if (!$this->csvWishlistProductIsValid($csvWishlistProduct)) {
75
                return;
76
            }
77
            $variantIdRequestAttributes[] = $csvWishlistProduct->getVariantId();
78
        }
79
80
        $request->attributes->set('variantId', $variantIdRequestAttributes);
81
    }
82
83
    private function fileIsValidMimeType(\SplFileInfo $fileInfo): bool
84
    {
85
        $finfo = new \finfo(\FILEINFO_MIME_TYPE);
86
87
        return in_array($finfo->file($fileInfo->getRealPath()), $this->allowedMimeTypes);
88
    }
89
90
    private function csvWishlistProductIsValid(CsvWishlistProductInterface $csvWishlistProduct): bool
91
    {
92
        $wishlistProduct = $this->productVariantRepository->findOneBy([
93
            'id' => $csvWishlistProduct->getVariantId(),
94
            'product' => $csvWishlistProduct->getProductId(),
95
            'code' => $csvWishlistProduct->getVariantCode(),
96
        ]);
97
98
        if (null === $wishlistProduct) {
99
            throw new NotFoundHttpException();
100
        }
101
102
        return true;
103
    }
104
}
105