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

ImportWishlistFromCsvHandler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

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

5 Methods

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