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

ImportWishlistFromCsvAction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 45
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A __invoke() 0 23 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitBag\SyliusWishlistPlugin\Controller\Action;
6
7
use BitBag\SyliusWishlistPlugin\Command\Wishlist\ImportWishlistFromCsv;
8
use BitBag\SyliusWishlistPlugin\Form\Type\ImportWishlistFromCsvType;
9
use Symfony\Component\Form\FormFactoryInterface;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
13
use Symfony\Component\Messenger\MessageBusInterface;
14
use Symfony\Component\Messenger\Stamp\HandledStamp;
15
use Twig\Environment;
16
17
final class ImportWishlistFromCsvAction
18
{
19
    private FormFactoryInterface $formFactory;
20
21
    private Environment $twigEnvironment;
22
23
    private FlashBagInterface $flashBag;
24
25
    private MessageBusInterface $commandBus;
26
27
    public function __construct(
28
        FormFactoryInterface $formFactory,
29
        Environment $twigEnvironment,
30
        FlashBagInterface $flashBag,
31
        MessageBusInterface $commandBus
32
    ) {
33
        $this->formFactory = $formFactory;
34
        $this->twigEnvironment = $twigEnvironment;
35
        $this->flashBag = $flashBag;
36
        $this->commandBus = $commandBus;
37
    }
38
39
    public function __invoke(Request $request): Response
40
    {
41
        $form = $this->formFactory->create(ImportWishlistFromCsvType::class);
42
43
        $form->handleRequest($request);
44
45
        if ($form->isSubmitted() && $form->isValid()) {
46
            $file = $form->get('wishlist_file')->getData();
47
            $command = new ImportWishlistFromCsv($file, $request);
48
49
            $envelope = $this->commandBus->dispatch($command);
50
            $responseStamp = $envelope->last(HandledStamp::class);
51
52
            return $responseStamp->getResult();
0 ignored issues
show
Bug introduced by
The method getResult() does not exist on Symfony\Component\Messenger\Stamp\StampInterface. It seems like you code against a sub-type of Symfony\Component\Messenger\Stamp\StampInterface such as Symfony\Component\Messenger\Stamp\HandledStamp. ( Ignorable by Annotation )

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

52
            return $responseStamp->/** @scrutinizer ignore-call */ getResult();
Loading history...
53
        }
54
55
        foreach ($form->getErrors() as $error) {
56
            $this->flashBag->add('error', $error->getMessage());
0 ignored issues
show
Bug introduced by
The method getMessage() does not exist on Symfony\Component\Form\FormErrorIterator. ( Ignorable by Annotation )

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

56
            $this->flashBag->add('error', $error->/** @scrutinizer ignore-call */ getMessage());

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...
57
        }
58
59
        return new Response(
60
            $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/importWishlist.html.twig', [
61
                        'form' => $form->createView(),
62
                ])
63
        );
64
    }
65
}
66