RequestHandler   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 22
eloc 38
c 1
b 0
f 0
dl 0
loc 134
ccs 44
cts 44
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handleRequest() 0 4 1
A handleFilterRequest() 0 10 4
A getRequest() 0 3 1
A getLength() 0 5 2
A getName() 0 3 1
A getCurrentPage() 0 5 2
A getValue() 0 5 1
B handleListRequest() 0 28 9
1
<?php
2
3
namespace Povs\ListerBundle\Service;
4
5
use Povs\ListerBundle\Mapper\FilterMapper;
6
use Povs\ListerBundle\Mapper\ListField;
7
use Povs\ListerBundle\Mapper\ListMapper;
8
use Symfony\Component\Form\FormInterface;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\RequestStack;
11
12
/**
13
 * @author Povilas Margaiatis <[email protected]>
14
 */
15
class RequestHandler
16
{
17
    /**
18
     * @var Request
19
     */
20
    private $request;
21
22
    /**
23
     * @var ConfigurationResolver
24
     */
25
    private $configuration;
26
27
    /**
28
     * ListRequestHandler constructor.
29
     *
30
     * @param RequestStack          $requestStack
31
     * @param ConfigurationResolver $configurationResolver
32
     */
33 6
    public function __construct(RequestStack $requestStack, ConfigurationResolver $configurationResolver)
34
    {
35 6
        $this->request = $requestStack->getCurrentRequest();
36 6
        $this->configuration = $configurationResolver;
37
    }
38
39
    /**
40
     * @param ListMapper    $listMapper
41
     * @param FilterMapper  $filterMapper
42
     * @param FormInterface $form
43
     */
44 1
    public function handleRequest(ListMapper $listMapper, FilterMapper $filterMapper, FormInterface $form): void
45
    {
46 1
        $this->handleListRequest($listMapper);
47 1
        $this->handleFilterRequest($filterMapper, $form);
48
    }
49
50
    /**
51
     * @return int|null
52
     */
53 1
    public function getCurrentPage(): ?int
54
    {
55 1
        $currentPage = $this->getValue('page');
56
57 1
        return $currentPage ? (int) $currentPage : null;
58
    }
59
60
    /**
61
     * @return int|null
62
     */
63 1
    public function getLength(): ?int
64
    {
65 1
        $perPage = $this->getValue('length');
66
67 1
        return $perPage ? (int) $perPage : null;
68
    }
69
70
    /**
71
     * @param string $name
72
     *
73
     * @return mixed|null
74
     */
75 4
    public function getValue(string $name)
76
    {
77 4
        $name = $this->getName($name);
78
79 4
        return $this->request->query->get($name);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type null; however, parameter $key of Symfony\Component\HttpFoundation\InputBag::get() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

79
        return $this->request->query->get(/** @scrutinizer ignore-type */ $name);
Loading history...
80
    }
81
82
    /**
83
     * @param string $name
84
     *
85
     * @return string|null
86
     */
87 5
    public function getName(string $name): ?string
88
    {
89 5
        return $this->configuration->getRequestConfiguration($name);
90
    }
91
92
    /**
93
     * @return Request
94
     */
95 1
    public function getRequest(): Request
96
    {
97 1
        return $this->request;
98
    }
99
100
    /**
101
     * @param FilterMapper  $filterMapper
102
     * @param FormInterface $form
103
     */
104 1
    private function handleFilterRequest(FilterMapper $filterMapper, FormInterface $form): void
105
    {
106 1
        $form->handleRequest($this->request);
107
108 1
        if ($form->isSubmitted() && $form->isValid()) {
109 1
            $data = $form->getData();
110
111 1
            foreach ($data as $name => $datum) {
112 1
                $field = $filterMapper->get($name);
113 1
                $field->setValue($datum);
114
            }
115
        }
116
    }
117
118
    /**
119
     * @param ListMapper $listMapper
120
     */
121 1
    private function handleListRequest(ListMapper $listMapper): void
122
    {
123 1
        $sort = $this->getValue('sort') ?? [];
124
125 1
        if ($sort) {
126 1
            $fieldsSet = null;
127
128 1
            foreach ($sort as $id => $direction) {
129 1
                $direction = strtoupper($direction);
130
131 1
                if (!in_array($direction, [ListField::SORT_DESC, ListField::SORT_ASC], true) || !$listMapper->has($id)) {
132 1
                    continue;
133
                }
134
135 1
                $field = $listMapper->get($id);
136 1
                $field->setOption(ListField::OPTION_SORT_VALUE, $direction);
137 1
                $field->setOption(ListField::OPTION_LAZY, false);
138
139 1
                if (false === $this->configuration->isMultiColumnSortable()) {
140 1
                    $fieldsSet = $field->getId();
141 1
                    break;
142
                }
143
            }
144
145 1
            if (null !== $fieldsSet) {
146 1
                foreach ($listMapper->getFields() as $field) {
147 1
                    if ($field->getId() !== $fieldsSet) {
148 1
                        $field->setOption(ListField::OPTION_SORT_VALUE, null);
149
                    }
150
                }
151
            }
152
        }
153
    }
154
}
155