GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

QueryOptionsBuilder::fromRequest()   B
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 54
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 5.0006

Importance

Changes 0
Metric Value
cc 5
eloc 39
nc 6
nop 1
dl 0
loc 54
ccs 33
cts 34
cp 0.9706
crap 5.0006
rs 8.9848
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Mado\QueryBundle\Queries\Options;
4
5
use Mado\QueryBundle\Exceptions\InvalidFiltersException;
6
use Mado\QueryBundle\Queries\QueryBuilderOptions;
7
use Symfony\Component\HttpFoundation\Request;
8
9
class QueryOptionsBuilder
10
{
11
    private $entityAlias;
12
13 10
    public function setEntityAlias(string $entityAlias)
14
    {
15 10
        $this->entityAlias = $entityAlias;
16 10
    }
17
18 10
    public function getEntityAlias()
19
    {
20 10
        return $this->entityAlias;
21
    }
22
23 3
    public function fromRequest(Request $request = null)
24
    {
25 3
        $this->ensureEntityAliasIsDefined();
26
27 3
        $requestAttributes = [];
28 3
        foreach ($request->attributes->all() as $attributeName => $attributeValue) {
29
            $requestAttributes[$attributeName] = $request->attributes->get(
30
                $attributeName,
31
                $attributeValue);
32
        }
33
34 3
        $filters     = $request->query->get('filtering', []);
35 3
        $orFilters   = $request->query->get('filtering_or', []);
36 3
        $sorting     = $request->query->get('sorting', []);
37 3
        $printing    = $request->query->get('printing', []);
38 3
        $rel         = $request->query->get('rel', '');
39 3
        $page        = $request->query->get('page', '');
40 3
        $select      = $request->query->get('select', $this->getEntityAlias());
41 3
        $filtering   = $request->query->get('filtering', '');
42 3
        $limit       = $request->query->get('limit', '');
43
44 3
        $filterOrCorrected = [];
45
46 3
        $count = 0;
47 3
        foreach ($orFilters as $key => $filter) {
48 2
            if (is_array($filter)) {
49 1
                foreach ($filter as $keyInternal => $internal) {
50 1
                    $filterOrCorrected[$keyInternal . '|' . $count] = $internal;
51 1
                    $count += 1;
52
                }
53
            } else {
54 2
                $filterOrCorrected[$key] = $filter;
55
            }
56
        }
57
58
        $requestProperties = [
59 3
            'filtering'   => $filtering,
60 3
            'orFiltering' => $filterOrCorrected,
61 3
            'limit'       => $limit,
62 3
            'page'        => $page,
63 3
            'filters'     => $filters,
64 3
            'orFilters'   => $filterOrCorrected,
65 3
            'sorting'     => $sorting,
66 3
            'rel'         => $rel,
67 3
            'printing'    => $printing,
68 3
            'select'      => $select,
69
        ];
70
71 3
        $options = array_merge(
72
            $requestAttributes,
73
            $requestProperties
74
        );
75
76 3
        return QueryBuilderOptions::fromArray($options);
77
    }
78
79 10
    public function ensureEntityAliasIsDefined()
80
    {
81 10
        if (!$this->entityAlias) {
82 1
            throw new \RuntimeException(
83 1
                'Oops! Entity alias is missing'
84
            );
85
        }
86 9
    }
87
88 4
    public function buildFromRequestAndCustomFilter(Request $request, $filter)
89
    {
90 4
        $this->ensureEntityAliasIsDefined();
91
92 4
        $filters   = $request->query->get('filtering', []);
93 4
        $orFilters = $request->query->get('filtering_or', []);
94 4
        $sorting   = $request->query->get('sorting', []);
95 4
        $printing  = $request->query->get('printing', []);
96 4
        $rel       = $request->query->get('rel', '');
97 4
        $page      = $request->query->get('page', '');
98 4
        $select    = $request->query->get('select', $this->getEntityAlias());
99 4
        $filtering = $request->query->get('filtering', '');
100 4
        $limit     = $request->query->get('limit', '');
101 4
        $justCount = $request->query->get('justCount', 'false');
102
103 4
        $this->ensureFilterIsValid($filters);
104
105 3
        $filters = array_merge($filters, $filter);
106
107 3
        $filterOrCorrected = [];
108
109 3
        $count = 0;
110 3
        foreach ($orFilters as $key => $filterValue) {
111 2
            if (is_array($filterValue)) {
112 1
                foreach ($filterValue as $keyInternal => $internal) {
113 1
                    $filterOrCorrected[$keyInternal . '|' . $count] = $internal;
114 1
                    $count += 1;
115
                }
116
            } else {
117 2
                $filterOrCorrected[$key] = $filterValue;
118
            }
119
        }
120
121 3
        return QueryBuilderOptions::fromArray([
122 3
            '_route'        => $request->attributes->get('_route'),
123 3
            '_route_params' => $request->attributes->get('_route_params', []),
124 3
            'id'            => $request->attributes->get('id'),
125 3
            'filtering'     => $filtering,
126 3
            'limit'         => $limit,
127 3
            'page'          => $page,
128 3
            'filters'       => $filters,
129 3
            'orFilters'     => $filterOrCorrected,
130 3
            'sorting'       => $sorting,
131 3
            'rel'           => $rel,
132 3
            'printing'      => $printing,
133 3
            'select'        => $select,
134 3
            'justCount'     => $justCount,
135
        ]);
136
    }
137
138 4
    private function ensureFilterIsValid($filters)
139
    {
140 4
        if (!is_array($filters)) {
141 1
            throw new InvalidFiltersException(
142
                "Wrong query string exception: "
143 1
                . var_export($filters, true) . "\n\n"
144 1
                . "Please check query string should be something like "
145 1
                . "http://<host>:<port>/?filtering[<field>|<operator>]=<value>"
146
            );
147
        }
148 3
    }
149
150 2
    public function buildForOrFilter(Request $request, array $orFilter)
151
    {
152 2
        $this->ensureEntityAliasIsDefined();
153
154 2
        $filters   = $request->query->get('filtering', []);
155 2
        $orFilters = $request->query->get('filtering_or', []);
156 2
        $sorting   = $request->query->get('sorting', []);
157 2
        $printing  = $request->query->get('printing', []);
158 2
        $rel       = $request->query->get('rel', '');
159 2
        $page      = $request->query->get('page', '');
160 2
        $select    = $request->query->get('select', $this->getEntityAlias());
161 2
        $filtering = $request->query->get('filtering', '');
162 2
        $limit     = $request->query->get('limit', '');
163
164 2
        $orFilters = array_merge($orFilters, $orFilter);
165
166 2
        $filterOrCorrected = [];
167
168 2
        $count = 0;
169 2
        foreach ($orFilters as $key => $filter) {
170 2
            if (is_array($filter)) {
171 1
                foreach ($filter as $keyInternal => $internal) {
172 1
                    $filterOrCorrected[$keyInternal . '|' . $count] = $internal;
173 1
                    $count += 1;
174
                }
175
            } else {
176 2
                $filterOrCorrected[$key] = $filter;
177
            }
178
        }
179
180 2
        return QueryBuilderOptions::fromArray([
181 2
            '_route'        => $request->attributes->get('_route'),
182 2
            '_route_params' => $request->attributes->get('_route_params', []),
183 2
            'id'            => $request->attributes->get('id'),
184 2
            'filtering'     => $filtering,
185 2
            'limit'         => $limit,
186 2
            'page'          => $page,
187 2
            'filters'       => $filters,
188 2
            'orFilters'     => $filterOrCorrected,
189 2
            'sorting'       => $sorting,
190 2
            'rel'           => $rel,
191 2
            'printing'      => $printing,
192 2
            'select'        => $select,
193
        ]);
194
    }
195
}
196