Passed
Pull Request — master (#88)
by
unknown
06:48 queued 02:57
created

ImportWishlistFromCsvHandler::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
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 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\Normalizer\NormalizerInterface;
24
25
final class ImportWishlistFromCsvHandler implements MessageHandlerInterface
26
{
27
    private AddProductVariantToWishlistAction $addProductVariantToWishlistAction;
28
29
    private ProductVariantRepositoryInterface $productVariantRepository;
30
31
    private NormalizerInterface $normalizer;
32
33
    private array $allowedMimeTypes;
34
35
    public function __construct(
36
        AddProductVariantToWishlistAction $addProductVariantToWishlistAction,
37
        ProductVariantRepositoryInterface $productVariantRepository,
38
        NormalizerInterface $normalizer,
39
        array $allowedMimeTypes
40
    ) {
41
        $this->addProductVariantToWishlistAction = $addProductVariantToWishlistAction;
42
        $this->productVariantRepository = $productVariantRepository;
43
        $this->normalizer = $normalizer;
44
        $this->allowedMimeTypes = $allowedMimeTypes;
45
    }
46
47
    public function __invoke(ImportWishlistFromCsv $importWishlistFromCsv): Response
48
    {
49
        $fileInfo = $importWishlistFromCsv->getFileInfo();
50
        $request = $importWishlistFromCsv->getRequest();
51
52
        $this->getDataFromFile($fileInfo, $request);
53
54
        return $this->addProductVariantToWishlistAction->__invoke($request);
55
    }
56
57
    private function getDataFromFile(\SplFileInfo $fileInfo, Request $request): void
58
    {
59
        if (!$this->fileIsValidMimeType($fileInfo)) {
60
            throw new UploadableInvalidMimeTypeException();
61
        }
62
        dump(file_get_contents((string) $fileInfo));
63
        $wishlistProductsArray = $this->normalizer->decode(file_get_contents((string) $fileInfo), 'csv');
0 ignored issues
show
Bug introduced by
The method decode() does not exist on Symfony\Component\Serial...zer\NormalizerInterface. It seems like you code against a sub-type of Symfony\Component\Serial...zer\NormalizerInterface such as Symfony\Component\Serializer\Serializer. ( Ignorable by Annotation )

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

63
        /** @scrutinizer ignore-call */ 
64
        $wishlistProductsArray = $this->normalizer->decode(file_get_contents((string) $fileInfo), 'csv');
Loading history...
64
65
        foreach ($wishlistProductsArray as $wishlistProductArray) {
66
            /** @var CsvWishlistProduct $csvWishlistProduct */
67
            $csvWishlistProduct = $this->normalizer->denormalize($wishlistProductArray, CsvWishlistProduct::class, 'csv');
0 ignored issues
show
Bug introduced by
The method denormalize() does not exist on Symfony\Component\Serial...zer\NormalizerInterface. Did you maybe mean normalize()? ( Ignorable by Annotation )

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

67
            /** @scrutinizer ignore-call */ 
68
            $csvWishlistProduct = $this->normalizer->denormalize($wishlistProductArray, CsvWishlistProduct::class, 'csv');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
69
            if (!$this->csvWishlistProductIsValid($csvWishlistProduct)) {
70
                return;
71
            }
72
            $variantIdRequestAttributes[] = $csvWishlistProduct->getVariantId();
73
        }
74
75
        $request->attributes->set('variantId', $variantIdRequestAttributes);
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()), $this->allowedMimeTypes);
83
    }
84
85
    private function csvWishlistProductIsValid(CsvWishlistProductInterface $csvWishlistProduct): bool
86
        {
87
        $wishlistProduct = $this->productVariantRepository->findOneBy([
88
            'id' => $csvWishlistProduct->getVariantId(),
89
            'product' => $csvWishlistProduct->getProductId(),
90
            'code' => $csvWishlistProduct->getVariantCode(),
91
        ]);
92
93
        dump($csvWishlistProduct);
94
95
        if (null === $wishlistProduct) {
96
            $message = sprintf(
97
                "ProductId: %s, variantId: %s, variantCode: %s",
98
                $csvWishlistProduct->getProductId(),
99
                $csvWishlistProduct->getVariantId(),
100
                $csvWishlistProduct->getVariantCode()
101
            );
102
            throw new NotFoundHttpException($message);
103
        }
104
105
        return true;
106
    }
107
}
108