Issues (36)

src/Controller/Action/Shop/ListProductsAction.php (1 issue)

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\SyliusElasticsearchPlugin\Controller\Action\Shop;
12
13
use BitBag\SyliusElasticsearchPlugin\Controller\RequestDataHandler\DataHandlerInterface;
14
use BitBag\SyliusElasticsearchPlugin\Controller\RequestDataHandler\PaginationDataHandlerInterface;
15
use BitBag\SyliusElasticsearchPlugin\Controller\RequestDataHandler\SortDataHandlerInterface;
16
use BitBag\SyliusElasticsearchPlugin\Finder\ShopProductsFinderInterface;
17
use BitBag\SyliusElasticsearchPlugin\Form\Type\ShopProductsFilterType;
18
use Symfony\Component\Form\FormFactoryInterface;
19
use Symfony\Component\Form\FormInterface;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\PropertyAccess\PropertyAccess;
23
use Twig\Environment;
24
25
final class ListProductsAction
26
{
27
    /** @var FormFactoryInterface */
28
    private $formFactory;
29
30
    /** @var DataHandlerInterface */
31
    private $shopProductListDataHandler;
32
33
    /** @var SortDataHandlerInterface */
34
    private $shopProductsSortDataHandler;
35
36
    /** @var PaginationDataHandlerInterface */
37
    private $paginationDataHandler;
38
39
    /** @var ShopProductsFinderInterface */
40
    private $shopProductsFinder;
41
42
    /** @var Environment */
43
    private $twig;
44
45
    public function __construct(
46
        FormFactoryInterface $formFactory,
47
        DataHandlerInterface $shopProductListDataHandler,
48
        SortDataHandlerInterface $shopProductsSortDataHandler,
49
        PaginationDataHandlerInterface $paginationDataHandler,
50
        ShopProductsFinderInterface $shopProductsFinder,
51
        Environment $twig
52
    ) {
53
        $this->formFactory = $formFactory;
54
        $this->shopProductListDataHandler = $shopProductListDataHandler;
55
        $this->shopProductsSortDataHandler = $shopProductsSortDataHandler;
56
        $this->paginationDataHandler = $paginationDataHandler;
57
        $this->shopProductsFinder = $shopProductsFinder;
58
        $this->twig = $twig;
59
    }
60
61
    public function __invoke(Request $request): Response
62
    {
63
        $form = $this->formFactory->create(ShopProductsFilterType::class);
64
        $form->handleRequest($request);
65
        $requestData = array_merge(
66
            $form->getData(),
67
            $request->query->all(),
68
            ['slug' => $request->get('slug')]
69
        );
70
71
        if (!$form->isValid()) {
72
            $requestData = $this->clearInvalidEntries($form, $requestData);
73
        }
74
75
        $data = array_merge(
76
            $this->shopProductListDataHandler->retrieveData($requestData),
77
            $this->shopProductsSortDataHandler->retrieveData($requestData),
78
            $this->paginationDataHandler->retrieveData($requestData)
79
        );
80
81
        $template = $request->get('template');
82
        $products = $this->shopProductsFinder->find($data);
83
84
        return new Response($this->twig->render($template, [
85
            'form' => $form->createView(),
86
            'products' => $products,
87
            'taxon' => $data['taxon'],
88
        ]));
89
    }
90
91
    private function clearInvalidEntries(FormInterface $form, array $requestData): array
92
    {
93
        $propertyAccessor = PropertyAccess::createPropertyAccessor();
94
        foreach ($form->getErrors(true, true) as $error) {
95
            $errorOrigin = $error->getOrigin();
0 ignored issues
show
The method getOrigin() 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

95
            /** @scrutinizer ignore-call */ 
96
            $errorOrigin = $error->getOrigin();

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...
96
            $propertyAccessor->setValue(
97
                $requestData,
98
                ($errorOrigin->getParent()->getPropertyPath() ?? '') . $errorOrigin->getPropertyPath(),
99
                ''
100
            );
101
        }
102
103
        return $requestData;
104
    }
105
}
106