Completed
Push — master ( 6e9755...81037b )
by Maximilian
02:35
created

src/Datagrid/ProxyQuery.php (1 issue)

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
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\DoctrinePHPCRAdminBundle\Datagrid;
13
14
use Doctrine\ODM\PHPCR\DocumentManager;
15
use Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder;
16
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
17
18
/**
19
 * This class is used to abstract the Admin Bundle from the different QueryBuilder implementations.
20
 */
21
class ProxyQuery implements ProxyQueryInterface
22
{
23
    /**
24
     * Query Builder Fluent interface for the QOM.
25
     *
26
     * @var QueryBuilder
27
     */
28
    protected $qb;
29
30
    /**
31
     * The alias name used for the document FQN.
32
     *
33
     * @var string
34
     */
35
    protected $alias;
36
37
    /**
38
     * The root path.
39
     *
40
     * @var null|string
41
     */
42
    protected $root;
43
44
    /**
45
     * Property that determines the Ordering of the results.
46
     *
47
     * @var string
48
     */
49
    protected $sortBy;
50
51
    /**
52
     * Ordering of the results (ASC, DESC).
53
     *
54
     * @var string
55
     */
56
    protected $sortOrder;
57
58
    /**
59
     * PHPCR ODM Document Manager.
60
     *
61
     * @var DocumentManager;
62
     */
63
    protected $documentManager;
64
65
    /**
66
     * Name of this document class.
67
     *
68
     * @var string
69
     */
70
    protected $documentName;
71
72
    /**
73
     * Creates a Query Builder from the QOMFactory.
74
     *
75
     * @param QueryBuilder $queryBuilder
76
     * @param string       $alias        Short name to use instead of the FQN
77
     *                                   of the document
78
     *
79
     * @throws \InvalidArgumentException if alias is not a string or an empty string
80
     */
81
    public function __construct(QueryBuilder $queryBuilder, $alias)
82
    {
83
        if (!is_string($alias) || '' === $alias) {
84
            throw new \InvalidArgumentException('$alias must be a non empty string');
85
        }
86
87
        $this->qb = $queryBuilder;
88
        $this->alias = $alias;
89
    }
90
91
    /**
92
     * Allows for direct calls to the QueryBuilder.
93
     *
94
     * @param string $name name of the method
95
     * @param array  $args arguments of the call
96
     *
97
     * @codeCoverageIgnore
98
     */
99
    public function __call($name, $args)
100
    {
101
        return call_user_func_array(array($this->qb, $name), $args);
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    public function getAlias()
108
    {
109
        return $this->alias;
110
    }
111
112
    /**
113
     * @param string $root root path to restrict what documents to find
114
     */
115
    public function setRootPath($root)
116
    {
117
        $this->root = $root;
118
    }
119
120
    /**
121
     * Executes the query, applying the source, the constraint of documents being of the phpcr:class of
122
     * this kind of document and builds an array of retrieved documents.
123
     *
124
     * @param array $params        doesn't have any effect
125
     * @param mixed $hydrationMode doesn't have any effect
126
     *
127
     * @return array of documents
128
     *
129
     * @throws \Exception if $this->sortOrder is not ASC or DESC
130
     */
131
    public function execute(array $params = array(), $hydrationMode = null)
132
    {
133
        if ($this->getSortBy()) {
134
            switch ($this->sortOrder) {
135
                case 'DESC':
136
                    $this->qb->orderBy()->desc()->field($this->alias.'.'.$this->sortBy);
137
                    break;
138
                case 'ASC':
139
                    $this->qb->orderBy()->asc()->field($this->alias.'.'.$this->sortBy);
140
                    break;
141
                default:
142
                    throw new \Exception('Unsupported sort order direction: '.$this->sortOrder);
143
            }
144
        }
145
146
        if ($this->root) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->root of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
147
            $this->qb->andWhere()->descendant($this->root, $this->alias);
148
        }
149
150
        return $this->qb->getQuery()->execute();
151
    }
152
153
    /**
154
     * Set the property to be sorted by.
155
     *
156
     * {@inheritdoc}
157
     */
158
    public function setSortBy($parentAssociationMappings, $fieldMapping)
159
    {
160
        $this->sortBy = $fieldMapping['fieldName'];
161
162
        return $this;
163
    }
164
165
    /**
166
     * Gets the property that defines the ordering.
167
     *
168
     * @return string the property to be sorted by
169
     */
170
    public function getSortBy()
171
    {
172
        return $this->sortBy;
173
    }
174
175
    /**
176
     * Set the sort ordering.
177
     *
178
     * {@inheritdoc}
179
     *
180
     * @param string $sortOrder (ASC|DESC)
181
     *
182
     * @throws \InvalidArgumentException if $sortOrder is not one of ASC or DESC
183
     */
184
    public function setSortOrder($sortOrder)
185
    {
186
        if (!in_array($sortOrder, array('ASC', 'DESC'))) {
187
            throw new \InvalidArgumentException(sprintf('The parameter $sortOrder must be one of "ASC" or "DESC", got "%s"', $sortOrder));
188
        }
189
        $this->sortOrder = $sortOrder;
190
191
        return $this;
192
    }
193
194
    /**
195
     * Get the ordering.
196
     *
197
     * @return string ASC or DESC
198
     */
199
    public function getSortOrder()
200
    {
201
        return $this->sortOrder;
202
    }
203
204
    /**
205
     * @codeCoverageIgnore
206
     *
207
     * @throws \Exception
208
     */
209
    public function getSingleScalarResult()
210
    {
211
        /* TODO: Figure out who calls this method and what to do here in context of PHPCR */
212
        throw new \Exception('Used by what??');
213
    }
214
215
    /**
216
     * Gets the QueryBuilder.
217
     *
218
     * @return QueryBuilder
219
     */
220
    public function getQueryBuilder()
221
    {
222
        return $this->qb;
223
    }
224
225
    /**
226
     * Sets the first result (offset).
227
     *
228
     * {@inheritdoc}
229
     */
230
    public function setFirstResult($firstResult)
231
    {
232
        $this->qb->setFirstResult($firstResult);
233
234
        return $this;
235
    }
236
237
    /**
238
     * Gets the first result (offset).
239
     *
240
     * @return int the offset
241
     */
242
    public function getFirstResult()
243
    {
244
        return $this->qb->getFirstResult();
245
    }
246
247
    /**
248
     * Set maximum number of results to retrieve.
249
     *
250
     * {@inheritdoc}
251
     */
252
    public function setMaxResults($maxResults)
253
    {
254
        $this->qb->setMaxResults($maxResults);
255
256
        return $this;
257
    }
258
259
    /**
260
     * Gets the maximum number of results to retrieve.
261
     *
262
     * @return int
263
     */
264
    public function getMaxResults()
265
    {
266
        return $this->qb->getMaxResults();
267
    }
268
269
    /**
270
     * Sets the document manager.
271
     *
272
     * @param DocumentManager $documentManager
273
     */
274
    public function setDocumentManager(DocumentManager $documentManager)
275
    {
276
        $this->documentManager = $documentManager;
277
278
        return $this;
279
    }
280
281
    /**
282
     * Gets the document manager.
283
     *
284
     * @return DocumentManager $documentManager
285
     */
286
    public function getDocumentManager()
287
    {
288
        return $this->documentManager;
289
    }
290
291
    public function getUniqueParameterId()
292
    {
293
    }
294
295
    /**
296
     * @param array $associationMappings
297
     */
298
    public function entityJoin(array $associationMappings)
299
    {
300
    }
301
}
302