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.
Passed
Pull Request — master (#61)
by Simone
08:38 queued 06:59
created

BaseRepository::setConfigProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Mado\QueryBundle\Repositories;
4
5
use Doctrine\ORM\EntityRepository;
6
use Hateoas\Configuration\Route;
0 ignored issues
show
Bug introduced by
The type Hateoas\Configuration\Route was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Hateoas\Representation\Factory\PagerfantaFactory;
0 ignored issues
show
Bug introduced by
The type Hateoas\Representation\Factory\PagerfantaFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Mado\QueryBundle\Queries\QueryBuilderFactory;
9
use Mado\QueryBundle\Queries\QueryBuilderOptions;
10
use Mado\QueryBundle\Component\ConfigProvider;
11
use Pagerfanta\Adapter\DoctrineORMAdapter;
0 ignored issues
show
Bug introduced by
The type Pagerfanta\Adapter\DoctrineORMAdapter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Pagerfanta\Pagerfanta;
0 ignored issues
show
Bug introduced by
The type Pagerfanta\Pagerfanta was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Symfony\Component\HttpFoundation\Request;
14
15
class BaseRepository extends EntityRepository
16
{
17
    protected $fields;
18
19
    protected $request;
20
21
    protected $use_result_cache = false;
22
23
    protected $entityAlias;
24
25
    protected $route_name;
26
27
    protected $currentEntityAlias;
28
29
    protected $embeddedFields;
30
31
    protected $joins = [];
32
33
    protected $queryBuilderFactory;
34
35
    protected $queryOptions;
36 5
37
    protected $configProvider;
38 5
39
    public function __construct($manager, $class)
40 5
    {
41
        parent::__construct($manager, $class);
42 5
43 5
        $this->fields = array_keys($this->getClassMetadata()->fieldMappings);
44 5
45 5
        $entityName = explode('\\', strtolower($this->getEntityName()) );
46
        $entityName = $entityName[count($entityName)-1];
47 5
        $entityAlias = $entityName[0];
48 5
        $this->entityAlias = $entityAlias;
49
50
        $this->queryBuilderFactory = new QueryBuilderFactory($this->getEntityManager());
51
    }
52
53
    public function initFromQueryBuilderOptions(QueryBuilderOptions $options)
54
    {
55
        $this->queryBuilderFactory->createQueryBuilder($this->getEntityName(), $this->entityAlias);
56
57
        $fieldMappings = $this->getClassMetadata()->fieldMappings;
58
        $this->fields = array_keys($fieldMappings);
59
60
        $this->queryBuilderFactory->setFields($this->fields ?? []);
61
        $this->queryBuilderFactory->setFilters($options->getFilters());
62
        $this->queryBuilderFactory->setOrFilters($options->getOrFilters());
63
        $this->queryBuilderFactory->setSorting($options->getSorting());
64
        $this->queryBuilderFactory->setRel($options->getRel());
65
        $this->queryBuilderFactory->setPrinting($options->getPrinting());
66
        $this->queryBuilderFactory->setSelect($options->getSelect());
67
    }
68
69
    public function getQueryBuilderFactory()
70
    {
71
        $this->ensureQueryOptionIsDefined();
72
73
        $this->initFromQueryBuilderOptions($this->queryOptions);
74
75
        return $this->queryBuilderFactory;
76
    }
77
78 1
    public function useResultCache($bool)
79
    {
80 1
        $this->use_result_cache = $bool;
81
    }
82
83
    public function setRequest(Request $request)
84
    {
85
        return $this->setQueryOptionsFromRequest($request);
86
    }
87
88
    public function setRequestWithFilter(Request $request, $filter)
89
    {
90
        return $this->setQueryOptionsFromRequestWithCustomFilter($request, $filter);
91
    }
92
93
    public function setRequestWithOrFilter(Request $request, $orFilter)
94
    {
95
        return $this->setQueryOptionsFromRequestWithCustomOrFilter($request, $orFilter);
96
    }
97
98 3
    public function setQueryOptions(QueryBuilderOptions $options)
99
    {
100 3
        $this->queryOptions = $options;
101
    }
102 3
103 3
    public function setQueryOptionsFromRequest(Request $request = null)
104 3
    {
105 3
        $requestAttributes = [];
106 3
        foreach ($request->attributes->all() as $attributeName => $attributeValue) {
107 3
            $requestAttributes[$attributeName] = $request->attributes->get(
108 3
                $attributeName,
109 3
                $attributeValue
110 3
            );
111
        }
112 3
113
        $filters     = $request->query->get('filtering', []);
114 3
        $orFilters   = $request->query->get('filtering_or', []);
115 3
        $sorting     = $request->query->get('sorting', []);
116
        $printing    = $request->query->get('printing', []);
117
        $rel         = $request->query->get('rel', '');
118
        $page        = $request->query->get('page', '');
119
        $select      = $request->query->get('select', $this->entityAlias);
120
        $pageLength  = $request->query->get('limit', 666);
0 ignored issues
show
Unused Code introduced by
The assignment to $pageLength is dead and can be removed.
Loading history...
121
        $filtering   = $request->query->get('filtering', '');
122
        $limit       = $request->query->get('limit', '');
123
124
        $filterOrCorrected = [];
125
126
        $count = 0;
127 3
        foreach ($orFilters as $key => $filter) {
128 3
            if (is_array($filter)) {
129 3
                foreach ($filter as $keyInternal => $internal) {
130 3
                    $filterOrCorrected[$keyInternal .'|' . $count] = $internal;
131 3
                    $count = $count + 1;
132 3
                }
133 3
            } else {
134 3
                $filterOrCorrected[$key] = $filter;
135 3
            }
136 3
        }
137
138
        $requestProperties = [
139 3
            'filtering'   => $filtering,
140 3
            'orFiltering' => $filterOrCorrected,
141 3
            'limit'       => $limit,
142
            'page'        => $page,
143
            'filters'     => $filters,
144 3
            'orFilters'   => $filterOrCorrected,
145
            'sorting'     => $sorting,
146 3
            'rel'         => $rel,
147
            'printing'    => $printing,
148
            'select'      => $select,
149 4
        ];
150
151 4
        $options = array_merge(
152
            $requestAttributes,
153 4
            $requestProperties
154
        );
155
156
        $this->queryOptions = QueryBuilderOptions::fromArray($options);
157
158
        return $this;
159
    }
160 4
161 View Code Duplication
    public function setQueryOptionsFromRequestWithCustomFilter(Request $request = null, $filter)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
162
    {
163
        $filters = $request->query->get('filtering', []);
164
        $orFilters = $request->query->get('filtering_or', []);
165
        $sorting = $request->query->get('sorting', []);
166
        $printing = $request->query->get('printing', []);
167
        $rel = $request->query->get('rel', '');
168
        $page = $request->query->get('page', '');
169
        $select = $request->query->get('select', $this->entityAlias);
170
        $pageLength = $request->query->get('limit', 666);
0 ignored issues
show
Unused Code introduced by
The assignment to $pageLength is dead and can be removed.
Loading history...
171
        $filtering = $request->query->get('filtering', '');
172
        $limit = $request->query->get('limit', '');
173
174
        $filters = array_merge($filters, $filter);
175
176
        $filterOrCorrected = [];
177
178
        $count = 0;
179
        foreach ($orFilters as $key => $filter) {
180
            if (is_array($filter)) {
181
                foreach ($filter as $keyInternal => $internal) {
182
                    $filterOrCorrected[$keyInternal .'|' . $count] = $internal;
183
                    $count = $count + 1;
184
                }
185
            } else {
186
                $filterOrCorrected[$key] = $filter;
187
            }
188
        }
189
190
        $this->queryOptions = QueryBuilderOptions::fromArray([
191
            '_route' => $request->attributes->get('_route'),
192
            'customer_id' => $request->attributes->get('customer_id'),
193
            'id' => $request->attributes->get('id'),
194
            'filtering' => $filtering,
195
            'limit' => $limit,
196
            'page' => $page,
197
            'filters' => $filters,
198
            'orFilters' => $filterOrCorrected,
199
            'sorting' => $sorting,
200
            'rel' => $rel,
201
            'printing' => $printing,
202
            'select' => $select,
203
        ]);
204
205
        return $this;
206
    }
207
208 View Code Duplication
    public function setQueryOptionsFromRequestWithCustomOrFilter(Request $request = null, $orFilter)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
209
    {
210
        $filters = $request->query->get('filtering', []);
211
        $orFilters = $request->query->get('filtering_or', []);
212
        $sorting = $request->query->get('sorting', []);
213
        $printing = $request->query->get('printing', []);
214
        $rel = $request->query->get('rel', '');
215
        $page = $request->query->get('page', '');
216
        $select = $request->query->get('select', $this->entityAlias);
217
        $pageLength = $request->query->get('limit', 666);
0 ignored issues
show
Unused Code introduced by
The assignment to $pageLength is dead and can be removed.
Loading history...
218 1
        $filtering = $request->query->get('filtering', '');
219
        $limit = $request->query->get('limit', '');
220 1
221
        $orFilters = array_merge($orFilters, $orFilter);
222 1
223 1
        $filterOrCorrected = [];
224 1
225 1
        $count = 0;
226 1
        foreach ($orFilters as $key => $filter) {
227 1
            if (is_array($filter)) {
228 1
                foreach ($filter as $keyInternal => $internal) {
229 1
                    $filterOrCorrected[$keyInternal .'|' . $count] = $internal;
230 1
                    $count = $count + 1;
231
                }
232 1
            } else {
233
                $filterOrCorrected[$key] = $filter;
234 1
            }
235
        }
236 1
237 1
        $this->queryOptions = QueryBuilderOptions::fromArray([
238
            '_route' => $request->attributes->get('_route'),
239
            'customer_id' => $request->attributes->get('customer_id'),
240
            'id' => $request->attributes->get('id'),
241
            'filtering' => $filtering,
242
            'limit' => $limit,
243
            'page' => $page,
244
            'filters' => $filters,
245
            'orFilters' => $filterOrCorrected,
246
            'sorting' => $sorting,
247
            'rel' => $rel,
248
            'printing' => $printing,
249 1
            'select' => $select,
250 1
        ]);
251 1
252 1
        return $this;
253 1
    }
254 1
255 1
    public function getRequest()
256 1
    {
257 1
        return $this->request;
258 1
    }
259 1
260 1
    public function setRouteName($route_name = '')
261
    {
262
        $this->route_name = $route_name;
263 1
        return $this;
264 1
    }
265 1
266
    public function findAllPaginated()
267
    {
268 1
        if ($this->configProvider) {
269
            $this->setRequest($this->configProvider->getRequest());
270 1
            $this->queryBuilderFactory->setConfigProvider($this->configProvider);
271
        }
272
273 4
        $this->ensureQueryOptionIsDefined();
274
275 4
        $this->initFromQueryBuilderOptions($this->queryOptions);
276
277
        $this->queryBuilderFactory->filter();
278
        $this->queryBuilderFactory->sort();
279
280
        return $this->paginateResults($this->queryBuilderFactory->getQueryBuilder());
281
    }
282
283
    protected function paginateResults(
284
        \Doctrine\ORM\QueryBuilder $queryBuilder
285
    ) {
286
        $this->ensureQueryOptionIsDefined();
287
288
        $limit = $this->queryOptions->get('limit', 10);
289
        $page = $this->queryOptions->get('page', 1);
290
291
292
        $pagerAdapter = new DoctrineORMAdapter($queryBuilder);
293
294
        $query = $pagerAdapter->getQuery();
295
        if(isset($this->use_result_cache) and $this->use_result_cache){
296
            $query->useResultCache(true, 600);
297
        }
298
299
        $pager = new Pagerfanta($pagerAdapter);
300
        $pager->setNormalizeOutOfRangePages(true);
301
        $pager->setMaxPerPage($limit);
302
        $pager->setCurrentPage($page);
303
304
        $pagerFactory = new PagerfantaFactory();
305
306
        $router = $this->createRouter();
307
308
        $results = $pagerFactory->createRepresentation($pager, $router);
309
310
        return $results;
311
    }
312
313
    protected function customQueryStringValues()
314
    {
315
        return [];
316
    }
317
318
    protected function createRouter()
319
    {
320
        $request = $this->getRequest();
0 ignored issues
show
Unused Code introduced by
The assignment to $request is dead and can be removed.
Loading history...
321
        $params = [];
322
323
        $list = array_merge([
324
            'filtering',
325
            'limit',
326
            'page',
327
            'sorting',
328
        ], $this->customQueryStringValues());
329
330
        $this->ensureQueryOptionIsDefined();
331
332
        foreach ($list as $itemKey => $itemValue) {
333
            $params[$itemValue] = $this->queryOptions->get($itemValue);
334
        }
335
336
        if(!isset($this->route_name)){
337
            $this->route_name = $this->queryOptions->get('_route');
338
        }
339
340
        return new Route($this->route_name, $params);
341
    }
342
343
    protected function getCurrentEntityAlias() : string
344
    {
345
        return $this->currentEntityAlias;
346
    }
347
348
    protected function setCurrentEntityAlias(string $currentEntityAlias) 
349
    {
350
        $this->currentEntityAlias = $currentEntityAlias;
351
    }
352
353
    protected function getEmbeddedFields() : array
354
    {
355
        return $this->embeddedFields;
356
    }
357
358
    protected function setEmbeddedFields(array $embeddedFields) 
359
    {
360
        $this->embeddedFields = $embeddedFields;
361
    }
362
363
    public function getEntityAlias(string $entityName) : string
364
    {
365
        $arrayEntityName = explode('\\', strtolower($entityName) );
366
        return $arrayEntityName[count($arrayEntityName)-1];
367
    }
368
369
    protected function relationship($queryBuilder)
370
    {
371
        return $queryBuilder;
372
    }
373
374
    public function getQueryBuilderFactoryWithoutInitialization()
375
    {
376
        return $this->queryBuilderFactory;
377
    }
378
379
    public function setConfigProvider(ConfigProvider $provider)
380
    {
381
        $this->configProvider = $provider;
382
383
        return $this;
384
    }
385
386
    public function ensureQueryOptionIsDefined()
387
    {
388
        if (!$this->queryOptions) {
389
            throw new \RuntimeException(
390
                'Oops! QueryBuilderOptions was never defined.'
391
            );
392
        }
393
    }
394
}
395