Completed
Push — 3.x ( 064670...3a8e9d )
by Vincent
01:24
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
    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
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)
63
    {
64
        $this->sortBy = $fieldMapping['fieldName'];
65
    }
66
67
    public function getSortBy()
68
    {
69
        return $this->sortBy;
70
    }
71
72
    public function setSortOrder($sortOrder)
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)
95
    {
96
        $this->firstResult = $firstResult;
97
        $this->queryBuilder->skip($firstResult ?? 0);
98
    }
99
100
    public function getFirstResult()
101
    {
102
        return $this->firstResult;
103
    }
104
105
    public function setMaxResults($maxResults)
106
    {
107
        $this->maxResults = $maxResults;
108
109
        // @see https://docs.mongodb.com/manual/reference/method/cursor.limit/#zero-value
110
        $this->queryBuilder->limit($maxResults ?? 0);
111
    }
112
113
    public function getMaxResults()
114
    {
115
        return $this->maxResults;
116
    }
117
118
    /**
119
     * @return mixed
120
     */
121
    public function getUniqueParameterId()
122
    {
123
        // TODO: Implement getUniqueParameterId() method.
124
    }
125
126
    /**
127
     * @return mixed
128
     */
129
    public function entityJoin(array $associationMappings)
130
    {
131
        // TODO: Implement entityJoin() method.
132
    }
133
}
134