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
Push — master ( 8bf58c...2661f4 )
by Simone
08:08 queued 06:15
created

BaseRepository::noExistsJoin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
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 2
dl 0
loc 4
ccs 0
cts 1
cp 0
crap 2
rs 10
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 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...
11
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...
12
use Symfony\Component\HttpFoundation\Request;
13
14
class BaseRepository extends EntityRepository
15
{
16
    protected $fields;
17
18
    protected $request;
19
20
    protected $use_result_cache = false;
21
22
    protected $entityAlias;
23
24
    protected $route_name;
25
26
    protected $currentEntityAlias;
27
28
    protected $embeddedFields;
29
30
    protected $joins = [];
31
32
    protected $queryBuilderFactory;
33
34
    protected $queryOptions;
35
36 5
    public function __construct($manager, $class)
37
    {
38 5
        parent::__construct($manager, $class);
39
40 5
        $this->fields = array_keys($this->getClassMetadata()->fieldMappings);
41
42 5
        $entityName = explode('\\', strtolower($this->getEntityName()) );
43 5
        $entityName = $entityName[count($entityName)-1];
44 5
        $entityAlias = $entityName[0];
45 5
        $this->entityAlias = $entityAlias;
46
47 5
        $this->queryBuilderFactory = new QueryBuilderFactory($this->getEntityManager());
48 5
    }
49
50
    public function initFromQueryBuilderOptions(QueryBuilderOptions $options)
51
    {
52
        $this->queryBuilderFactory->createQueryBuilder($this->getEntityName(), $this->entityAlias);
53
54
        $fieldMappings = $this->getClassMetadata()->fieldMappings;
55
        $this->fields = array_keys($fieldMappings);
56
57
        $this->queryBuilderFactory->setFields($this->fields ?? []);
58
        $this->queryBuilderFactory->setFilters($options->getFilters());
59
        $this->queryBuilderFactory->setOrFilters($options->getOrFilters());
60
        $this->queryBuilderFactory->setSorting($options->getSorting());
61
        $this->queryBuilderFactory->setRel($options->getRel());
62
        $this->queryBuilderFactory->setPrinting($options->getPrinting());
63
        $this->queryBuilderFactory->setSelect($options->getSelect());
64
    }
65
66
    public function getQueryBuilderFactory()
67
    {
68
        $this->initFromQueryBuilderOptions($this->queryOptions);
69
70
        return $this->queryBuilderFactory;
71
    }
72
73
    public function useResultCache($bool)
74
    {
75
        $this->use_result_cache = $bool;
76
    }
77
78 1
    public function setRequest(Request $request)
79
    {
80 1
        return $this->setQueryOptionsFromRequest($request);
81
    }
82
83
    public function setRequestWithFilter(Request $request, $filter)
84
    {
85
        return $this->setQueryOptionsFromRequestWithCustomFilter($request, $filter);
86
    }
87
88
    public function setRequestWithOrFilter(Request $request, $orFilter)
89
    {
90
        return $this->setQueryOptionsFromRequestWithCustomOrFilter($request, $orFilter);
91
    }
92
93
    public function setQueryOptions(QueryBuilderOptions $options)
94
    {
95
        $this->queryOptions = $options;
96
    }
97
98 3
    public function setQueryOptionsFromRequest(Request $request = null)
99
    {
100 3
        $requestAttributes = [];
101
        foreach ($request->attributes->all() as $attributeName => $attributeValue) {
102 3
            $requestAttributes[$attributeName] = $request->attributes->get(
103 3
                $attributeName,
104 3
                $attributeValue
105 3
            );
106 3
        }
107 3
108 3
        $filters     = $request->query->get('filtering', []);
109 3
        $orFilters   = $request->query->get('filtering_or', []);
110 3
        $sorting     = $request->query->get('sorting', []);
111
        $printing    = $request->query->get('printing', []);
112 3
        $rel         = $request->query->get('rel', '');
113
        $page        = $request->query->get('page', '');
114 3
        $select      = $request->query->get('select', $this->entityAlias);
115 3
        $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...
116
        $filtering   = $request->query->get('filtering', '');
117
        $limit       = $request->query->get('limit', '');
118
119
        $filterOrCorrected = [];
120
121
        $count = 0;
122
        foreach ($orFilters as $key => $filter) {
123
            if (is_array($filter)) {
124
                foreach ($filter as $keyInternal => $internal) {
125
                    $filterOrCorrected[$keyInternal .'|' . $count] = $internal;
126
                    $count = $count + 1;
127 3
                }
128 3
            } else {
129 3
                $filterOrCorrected[$key] = $filter;
130 3
            }
131 3
        }
132 3
133 3
        $requestProperties = [
134 3
            'filtering'   => $filtering,
135 3
            'orFiltering' => $filterOrCorrected,
136 3
            'limit'       => $limit,
137
            'page'        => $page,
138
            'filters'     => $filters,
139 3
            'orFilters'   => $filterOrCorrected,
140 3
            'sorting'     => $sorting,
141 3
            'rel'         => $rel,
142
            'printing'    => $printing,
143
            'select'      => $select,
144 3
        ];
145
146 3
        $options = array_merge(
147
            $requestAttributes,
148
            $requestProperties
149 4
        );
150
151 4
        $this->queryOptions = QueryBuilderOptions::fromArray($options);
152
153 4
        return $this;
154
    }
155
156 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...
157
    {
158
        $filters = $request->query->get('filtering', []);
159
        $orFilters = $request->query->get('filtering_or', []);
160 4
        $sorting = $request->query->get('sorting', []);
161
        $printing = $request->query->get('printing', []);
162
        $rel = $request->query->get('rel', '');
163
        $page = $request->query->get('page', '');
164
        $select = $request->query->get('select', $this->entityAlias);
165
        $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...
166
        $filtering = $request->query->get('filtering', '');
167
        $limit = $request->query->get('limit', '');
168
169
        $filters = array_merge($filters, $filter);
170
171
        $filterOrCorrected = [];
172
173
        $count = 0;
174
        foreach ($orFilters as $key => $filter) {
175
            if (is_array($filter)) {
176
                foreach ($filter as $keyInternal => $internal) {
177
                    $filterOrCorrected[$keyInternal .'|' . $count] = $internal;
178
                    $count = $count + 1;
179
                }
180
            } else {
181
                $filterOrCorrected[$key] = $filter;
182
            }
183
        }
184
185
        $this->queryOptions = QueryBuilderOptions::fromArray([
186
            '_route' => $request->attributes->get('_route'),
187
            'customer_id' => $request->attributes->get('customer_id'),
188
            'id' => $request->attributes->get('id'),
189
            'filtering' => $filtering,
190
            'limit' => $limit,
191
            'page' => $page,
192
            'filters' => $filters,
193
            'orFilters' => $filterOrCorrected,
194
            'sorting' => $sorting,
195
            'rel' => $rel,
196
            'printing' => $printing,
197
            'select' => $select,
198
        ]);
199
200
        return $this;
201
    }
202
203 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...
204
    {
205
        $filters = $request->query->get('filtering', []);
206
        $orFilters = $request->query->get('filtering_or', []);
207
        $sorting = $request->query->get('sorting', []);
208
        $printing = $request->query->get('printing', []);
209
        $rel = $request->query->get('rel', '');
210
        $page = $request->query->get('page', '');
211
        $select = $request->query->get('select', $this->entityAlias);
212
        $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...
213
        $filtering = $request->query->get('filtering', '');
214
        $limit = $request->query->get('limit', '');
215
216
        $orFilters = array_merge($orFilters, $orFilter);
217
218 1
        $filterOrCorrected = [];
219
220 1
        $count = 0;
221
        foreach ($orFilters as $key => $filter) {
222 1
            if (is_array($filter)) {
223 1
                foreach ($filter as $keyInternal => $internal) {
224 1
                    $filterOrCorrected[$keyInternal .'|' . $count] = $internal;
225 1
                    $count = $count + 1;
226 1
                }
227 1
            } else {
228 1
                $filterOrCorrected[$key] = $filter;
229 1
            }
230 1
        }
231
232 1
        $this->queryOptions = QueryBuilderOptions::fromArray([
233
            '_route' => $request->attributes->get('_route'),
234 1
            'customer_id' => $request->attributes->get('customer_id'),
235
            'id' => $request->attributes->get('id'),
236 1
            'filtering' => $filtering,
237 1
            'limit' => $limit,
238
            'page' => $page,
239
            'filters' => $filters,
240
            'orFilters' => $filterOrCorrected,
241
            'sorting' => $sorting,
242
            'rel' => $rel,
243
            'printing' => $printing,
244
            'select' => $select,
245
        ]);
246
247
        return $this;
248
    }
249 1
250 1
    public function getRequest()
251 1
    {
252 1
        return $this->request;
253 1
    }
254 1
255 1
    public function setRouteName($route_name = '')
256 1
    {
257 1
        $this->route_name = $route_name;
258 1
        return $this;
259 1
    }
260 1
261
    public function findAllPaginated()
262
    {
263 1
        $this->initFromQueryBuilderOptions($this->queryOptions);
264 1
265 1
        $this->queryBuilderFactory->filter();
266
        $this->queryBuilderFactory->sort();
267
268 1
        return $this->paginateResults($this->queryBuilderFactory->getQueryBuilder());
269
    }
270 1
271
    protected function paginateResults(
272
        \Doctrine\ORM\QueryBuilder $queryBuilder
273 4
    ) {
274
        $limit = $this->queryOptions->get('limit', 10);
275 4
        $page = $this->queryOptions->get('page', 1);
276
277
278
        $pagerAdapter = new DoctrineORMAdapter($queryBuilder);
279
280
        $query = $pagerAdapter->getQuery();
281
        if(isset($this->use_result_cache) and $this->use_result_cache){
282
            $query->useResultCache(true, 600);
283
        }
284
285
        $pager = new Pagerfanta($pagerAdapter);
286
        $pager->setNormalizeOutOfRangePages(true);
287
        $pager->setMaxPerPage($limit);
288
        $pager->setCurrentPage($page);
289
290
        $pagerFactory = new PagerfantaFactory();
291
292
        $router = $this->createRouter();
293
294
        $results = $pagerFactory->createRepresentation($pager, $router);
295
296
        return $results;
297
    }
298
299
    protected function customQueryStringValues()
300
    {
301
        return [];
302
    }
303
304
    protected function createRouter()
305
    {
306
        $request = $this->getRequest();
0 ignored issues
show
Unused Code introduced by
The assignment to $request is dead and can be removed.
Loading history...
307
        $params = [];
308
309
        $list = array_merge([
310
            'filtering',
311
            'limit',
312
            'page',
313
            'sorting',
314
        ], $this->customQueryStringValues());
315
316
        foreach ($list as $itemKey => $itemValue) {
317
            $params[$itemValue] = $this->queryOptions->get($itemValue);
318
        }
319
320
        if(!isset($this->route_name)){
321
            $this->route_name = $this->queryOptions->get('_route');
322
        }
323
324
        return new Route($this->route_name, $params);
325
    }
326
327
    protected function getCurrentEntityAlias() : string
328
    {
329
        return $this->currentEntityAlias;
330
    }
331
332
    protected function setCurrentEntityAlias(string $currentEntityAlias) 
333
    {
334
        $this->currentEntityAlias = $currentEntityAlias;
335
    }
336
337
    protected function getEmbeddedFields() : array
338
    {
339
        return $this->embeddedFields;
340
    }
341
342
    protected function setEmbeddedFields(array $embeddedFields) 
343
    {
344
        $this->embeddedFields = $embeddedFields;
345
    }
346
347
    public function getEntityAlias(string $entityName) : string
348
    {
349
        $arrayEntityName = explode('\\', strtolower($entityName) );
350
        return $arrayEntityName[count($arrayEntityName)-1];
351
    }
352
353
    protected function relationship($queryBuilder)
354
    {
355
        return $queryBuilder;
356
    }
357
358
    public function getQueryBuilderFactoryWithoutInitialization()
359
    {
360
        return $this->queryBuilderFactory;
361
    }
362
}
363