Completed
Push — master ( 3f5189...11d34c )
by
unknown
32s queued 11s
created

ListProductsAction::clearInvalidEntries()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 2
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusElasticsearchPlugin\Controller\Action\Shop;
14
15
use BitBag\SyliusElasticsearchPlugin\Controller\RequestDataHandler\DataHandlerInterface;
16
use BitBag\SyliusElasticsearchPlugin\Controller\RequestDataHandler\PaginationDataHandlerInterface;
17
use BitBag\SyliusElasticsearchPlugin\Controller\RequestDataHandler\SortDataHandlerInterface;
18
use BitBag\SyliusElasticsearchPlugin\Finder\ShopProductsFinderInterface;
19
use BitBag\SyliusElasticsearchPlugin\Form\Type\ShopProductsFilterType;
20
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
21
use Symfony\Component\Form\FormFactoryInterface;
22
use Symfony\Component\Form\FormInterface;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpFoundation\Response;
25
use Symfony\Component\PropertyAccess\PropertyAccess;
26
27
final class ListProductsAction
28
{
29
    /** @var FormFactoryInterface */
30
    private $formFactory;
31
32
    /** @var DataHandlerInterface */
33
    private $shopProductListDataHandler;
34
35
    /** @var SortDataHandlerInterface */
36
    private $shopProductsSortDataHandler;
37
38
    /** @var PaginationDataHandlerInterface */
39
    private $paginationDataHandler;
40
41
    /** @var ShopProductsFinderInterface */
42
    private $shopProductsFinder;
43
44
    /** @var EngineInterface */
45
    private $templatingEngine;
46
47
    public function __construct(
48
        FormFactoryInterface $formFactory,
49
        DataHandlerInterface $shopProductListDataHandler,
50
        SortDataHandlerInterface $shopProductsSortDataHandler,
51
        PaginationDataHandlerInterface $paginationDataHandler,
52
        ShopProductsFinderInterface $shopProductsFinder,
53
        EngineInterface $templatingEngine
54
    ) {
55
        $this->formFactory = $formFactory;
56
        $this->shopProductListDataHandler = $shopProductListDataHandler;
57
        $this->shopProductsSortDataHandler = $shopProductsSortDataHandler;
58
        $this->paginationDataHandler = $paginationDataHandler;
59
        $this->shopProductsFinder = $shopProductsFinder;
60
        $this->templatingEngine = $templatingEngine;
61
    }
62
63
    public function __invoke(Request $request): Response
64
    {
65
        $form = $this->formFactory->createNamed(null, ShopProductsFilterType::class);
66
        $form->handleRequest($request);
67
        $requestData = array_merge(
68
            $form->getData(),
69
            $request->query->all(),
70
            ['slug' => $request->get('slug')]
71
        );
72
73
        if (!$form->isValid()) {
74
            $requestData = $this->clearInvalidEntries($form, $requestData);
75
        }
76
77
        $data = array_merge(
78
            $this->shopProductListDataHandler->retrieveData($requestData),
79
            $this->shopProductsSortDataHandler->retrieveData($requestData),
80
            $this->paginationDataHandler->retrieveData($requestData)
81
        );
82
83
        $template = $request->get('template');
84
        $products = $this->shopProductsFinder->find($data);
85
86
        return $this->templatingEngine->renderResponse($template, [
87
            'form' => $form->createView(),
88
            'products' => $products,
89
            'taxon' => $data['taxon'],
90
        ]);
91
    }
92
93
    private function clearInvalidEntries(FormInterface $form, array $requestData): array
94
    {
95
        $propertyAccessor = PropertyAccess::createPropertyAccessor();
96
        foreach ($form->getErrors(true, true) as $error) {
97
            $errorOrigin = $error->getOrigin();
0 ignored issues
show
Bug introduced by
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

97
            /** @scrutinizer ignore-call */ 
98
            $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...
98
            $propertyAccessor->setValue(
99
                $requestData,
100
                ($errorOrigin->getParent()->getPropertyPath() ?? '') . $errorOrigin->getPropertyPath(),
101
                ''
102
            );
103
        }
104
105
        return $requestData;
106
    }
107
}
108