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