Completed
Push — master ( 3b4b20...e80e3c )
by
unknown
12:23
created

src/Datagrid/ProxyQuery.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
    /**
31
     * @param \Doctrine\ODM\MongoDB\Query\Builder $queryBuilder
32
     */
33
    public function __construct(Builder $queryBuilder)
34
    {
35
        $this->queryBuilder = $queryBuilder;
36
    }
37
38
    public function __call($name, $args)
39
    {
40
        return \call_user_func_array([$this->queryBuilder, $name], $args);
41
    }
42
43
    public function __clone()
44
    {
45
        $this->queryBuilder = clone $this->queryBuilder;
46
    }
47
48
    /**
49
     * @param array $params
50
     * @param null  $hydrationMode
51
     *
52
     * @return mixed
53
     */
54
    public function execute(array $params = [], $hydrationMode = null)
55
    {
56
        // always clone the original queryBuilder.
57
        $queryBuilder = clone $this->queryBuilder;
58
59
        // todo : check how doctrine behave, potential SQL injection here ...
60
        $sortBy = $this->getSortBy();
61
        if ($sortBy) {
62
            $queryBuilder->sort($sortBy, $this->getSortOrder());
63
        }
64
65
        return $queryBuilder->getQuery()->execute($params, $hydrationMode);
0 ignored issues
show
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...
66
    }
67
68
    public function setSortBy($parentAssociationMappings, $fieldMapping): void
69
    {
70
        $this->sortBy = $fieldMapping['fieldName'];
71
    }
72
73
    public function getSortBy()
74
    {
75
        return $this->sortBy;
76
    }
77
78
    public function setSortOrder($sortOrder): void
79
    {
80
        $this->sortOrder = $sortOrder;
81
    }
82
83
    public function getSortOrder()
84
    {
85
        return $this->sortOrder;
86
    }
87
88
    public function getSingleScalarResult()
89
    {
90
        $query = $this->queryBuilder->getQuery();
91
92
        return $query->getSingleResult();
93
    }
94
95
    public function getQueryBuilder()
96
    {
97
        return $this->queryBuilder;
98
    }
99
100
    public function setFirstResult($firstResult): void
101
    {
102
        $this->firstResult = $firstResult;
103
        $this->queryBuilder->skip($firstResult);
104
    }
105
106
    public function getFirstResult()
107
    {
108
        return $this->firstResult;
109
    }
110
111
    public function setMaxResults($maxResults): void
112
    {
113
        $this->maxResults = $maxResults;
114
        $this->queryBuilder->limit($maxResults);
115
    }
116
117
    public function getMaxResults()
118
    {
119
        return $this->maxResults;
120
    }
121
122
    /**
123
     * @return mixed
124
     */
125
    public function getUniqueParameterId()
126
    {
127
        // TODO: Implement getUniqueParameterId() method.
128
    }
129
130
    /**
131
     * @param array $associationMappings
132
     *
133
     * @return mixed
134
     */
135
    public function entityJoin(array $associationMappings)
136
    {
137
        // TODO: Implement entityJoin() method.
138
    }
139
}
140