ProxyQuery   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 0
loc 110
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __call() 0 4 1
A __clone() 0 4 1
A execute() 0 13 2
A setSortBy() 0 4 1
A getSortBy() 0 4 1
A setSortOrder() 0 4 1
A getSortOrder() 0 4 1
A getSingleScalarResult() 0 6 1
A getQueryBuilder() 0 4 1
A setFirstResult() 0 5 1
A getFirstResult() 0 4 1
A setMaxResults() 0 5 1
A getMaxResults() 0 4 1
A getUniqueParameterId() 0 4 1
A entityJoin() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrineMongoDBAdminBundle\Datagrid;
15
16
use Doctrine\ODM\MongoDB\Query\Builder;
17
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
18
19
/**
20
 * This class try to unify the query usage with Doctrine.
21
 */
22
class ProxyQuery implements ProxyQueryInterface
23
{
24
    protected $queryBuilder;
25
    protected $sortBy;
26
    protected $sortOrder;
27
    protected $firstResult;
28
    protected $maxResults;
29
30
    public function __construct(Builder $queryBuilder)
31
    {
32
        $this->queryBuilder = $queryBuilder;
33
    }
34
35
    public function __call($name, $args)
36
    {
37
        return \call_user_func_array([$this->queryBuilder, $name], $args);
38
    }
39
40
    public function __clone()
41
    {
42
        $this->queryBuilder = clone $this->queryBuilder;
43
    }
44
45
    /**
46
     * @return mixed
47
     */
48
    public function execute(array $params = [], $hydrationMode = null)
49
    {
50
        // always clone the original queryBuilder.
51
        $queryBuilder = clone $this->queryBuilder;
52
53
        // todo : check how doctrine behave, potential SQL injection here ...
54
        $sortBy = $this->getSortBy();
55
        if ($sortBy) {
56
            $queryBuilder->sort($sortBy, $this->getSortOrder());
57
        }
58
59
        return $queryBuilder->getQuery()->execute($params, $hydrationMode);
0 ignored issues
show
Unused Code introduced by Thomas Rabaix
The call to Query::execute() has too many arguments starting with $params.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
60
    }
61
62
    public function setSortBy($parentAssociationMappings, $fieldMapping): void
63
    {
64
        $this->sortBy = $fieldMapping['fieldName'];
65
    }
66
67
    public function getSortBy()
68
    {
69
        return $this->sortBy;
70
    }
71
72
    public function setSortOrder($sortOrder): void
73
    {
74
        $this->sortOrder = $sortOrder;
75
    }
76
77
    public function getSortOrder()
78
    {
79
        return $this->sortOrder;
80
    }
81
82
    public function getSingleScalarResult()
83
    {
84
        $query = $this->queryBuilder->getQuery();
85
86
        return $query->getSingleResult();
87
    }
88
89
    public function getQueryBuilder()
90
    {
91
        return $this->queryBuilder;
92
    }
93
94
    public function setFirstResult($firstResult): void
95
    {
96
        $this->firstResult = $firstResult;
97
        $this->queryBuilder->skip($firstResult);
98
    }
99
100
    public function getFirstResult()
101
    {
102
        return $this->firstResult;
103
    }
104
105
    public function setMaxResults($maxResults): void
106
    {
107
        $this->maxResults = $maxResults;
108
        $this->queryBuilder->limit($maxResults);
109
    }
110
111
    public function getMaxResults()
112
    {
113
        return $this->maxResults;
114
    }
115
116
    /**
117
     * @return mixed
118
     */
119
    public function getUniqueParameterId()
120
    {
121
        // TODO: Implement getUniqueParameterId() method.
122
    }
123
124
    /**
125
     * @return mixed
126
     */
127
    public function entityJoin(array $associationMappings)
128
    {
129
        // TODO: Implement entityJoin() method.
130
    }
131
}
132