Passed
Push — master ( 44aa55...07b576 )
by Przemysław eRIZ
04:29
created

ImportWishlistFromCsvAction::__invoke()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 17
rs 9.9666
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\Form\FormInterface;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
14
use Symfony\Component\Messenger\HandleTrait;
15
use Symfony\Component\Messenger\MessageBusInterface;
16
use Twig\Environment;
17
18
final class ImportWishlistFromCsvAction
19
{
20
    use HandleTrait;
21
22
    private FormFactoryInterface $formFactory;
23
24
    private FlashBagInterface $flashBag;
25
26
    private Environment $twigEnvironment;
27
28
    public function __construct(
29
        FormFactoryInterface $formFactory,
30
        FlashBagInterface $flashBag,
31
        MessageBusInterface $messageBus,
32
        Environment $twigEnvironment
33
    ) {
34
        $this->formFactory = $formFactory;
35
        $this->flashBag = $flashBag;
36
        $this->messageBus = $messageBus;
37
        $this->twigEnvironment = $twigEnvironment;
38
    }
39
40
    public function __invoke(Request $request): Response
41
    {
42
        $form = $this->createForm();
43
44
        $form->handleRequest($request);
45
46
        if ($form->isSubmitted() && $form->isValid()) {
47
            return $this->handleCommand($form, $request);
48
        }
49
50
        foreach ($form->getErrors() as $error) {
51
            $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

51
            $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...
52
        }
53
54
        return new Response(
55
            $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/importWishlist.html.twig', [
56
            'form' => $form->createView(),
57
            ])
58
        );
59
    }
60
61
    private function createForm(): FormInterface
62
    {
63
        return $this->formFactory->create(ImportWishlistFromCsvType::class);
64
    }
65
66
    private function handleCommand(FormInterface $form, Request $request): Response
67
    {
68
        $file = $form->get('wishlist_file')->getData();
69
        $command = new ImportWishlistFromCsv($file->getFileInfo(), $request);
70
71
        return $this->handle($command);
72
    }
73
}
74