1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Lug package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Lug\Component\Resource\Repository\Doctrine\MongoDB; |
13
|
|
|
|
14
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
15
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
16
|
|
|
use Doctrine\ODM\MongoDB\Query\Builder; |
17
|
|
|
use Doctrine\ODM\MongoDB\UnitOfWork; |
18
|
|
|
use Lug\Component\Grid\DataSource\Doctrine\MongoDB\DataSourceBuilder; |
19
|
|
|
use Lug\Component\Resource\Model\ResourceInterface; |
20
|
|
|
use Pagerfanta\Adapter\DoctrineODMMongoDBAdapter; |
21
|
|
|
use Pagerfanta\Pagerfanta; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* WARNING - This trait should only be used with a class extending an DocumentRepository. |
25
|
|
|
* |
26
|
|
|
* @author GeLo <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
trait RepositoryTrait |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var ResourceInterface |
32
|
|
|
*/ |
33
|
|
|
private $resource; |
34
|
|
|
|
35
|
|
|
/*** |
36
|
|
|
* @param DocumentManager $dm |
37
|
|
|
* @param UnitOfWork $uow |
38
|
|
|
* @param ClassMetadata $class |
39
|
|
|
* @param ResourceInterface $resource |
40
|
|
|
*/ |
41
|
16 |
|
public function __construct( |
42
|
|
|
DocumentManager $dm, |
43
|
|
|
UnitOfWork $uow, |
44
|
|
|
ClassMetadata $class, |
45
|
|
|
ResourceInterface $resource |
46
|
|
|
) { |
47
|
16 |
|
parent::__construct($dm, $uow, $class); |
48
|
|
|
|
49
|
16 |
|
$this->resource = $resource; |
50
|
16 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
1 |
|
public function findForIndex(array $criteria, array $orderBy = []) |
56
|
|
|
{ |
57
|
1 |
|
return new Pagerfanta(new DoctrineODMMongoDBAdapter($this->buildQueryBuilder($criteria, $orderBy, true))); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
1 |
|
public function findForShow(array $criteria, array $orderBy = []) |
64
|
|
|
{ |
65
|
1 |
|
return $this->findOneBy($criteria, $orderBy); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
1 |
|
public function findForUpdate(array $criteria, array $orderBy = []) |
72
|
|
|
{ |
73
|
1 |
|
return $this->findOneBy($criteria, $orderBy); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
1 |
|
public function findForDelete(array $criteria, array $orderBy = []) |
80
|
|
|
{ |
81
|
1 |
|
return $this->findOneBy($criteria, $orderBy); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
6 |
|
public function findOneBy(array $criteria, array $orderBy = []) |
88
|
|
|
{ |
89
|
6 |
|
return $this->buildQueryBuilder($criteria, $orderBy)->getQuery()->getSingleResult(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
1 |
View Code Duplication |
public function findBy(array $criteria, array $orderBy = [], $limit = null, $offset = null) |
|
|
|
|
96
|
|
|
{ |
97
|
1 |
|
$queryBuilder = $this->buildQueryBuilder($criteria, $orderBy); |
98
|
|
|
|
99
|
1 |
|
if ($limit !== null) { |
100
|
1 |
|
$queryBuilder->limit($limit); |
101
|
1 |
|
} |
102
|
|
|
|
103
|
1 |
|
if ($offset !== null) { |
104
|
1 |
|
$queryBuilder->skip($offset); |
105
|
1 |
|
} |
106
|
|
|
|
107
|
1 |
|
return $queryBuilder->getQuery()->getIterator()->toArray(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
4 |
|
public function createQueryBuilderForCollection() |
114
|
|
|
{ |
115
|
4 |
|
return $this->createQueryBuilder(); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
1 |
|
public function createDataSourceBuilder(array $options = []) |
122
|
|
|
{ |
123
|
1 |
|
return new DataSourceBuilder($this, $options); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
9 |
|
public function getProperty($property, $root = null) |
130
|
|
|
{ |
131
|
9 |
|
if (is_string($root) && !empty($root)) { |
132
|
1 |
|
return $root.'.'.$property; |
133
|
|
|
} |
134
|
|
|
|
135
|
8 |
|
return $property; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param mixed[] $criteria |
140
|
|
|
* @param string[] $orderBy |
141
|
|
|
* @param bool $collection |
142
|
|
|
* |
143
|
|
|
* @return Builder |
144
|
|
|
*/ |
145
|
8 |
View Code Duplication |
protected function buildQueryBuilder(array $criteria, array $orderBy, $collection = false) |
|
|
|
|
146
|
|
|
{ |
147
|
8 |
|
$queryBuilder = $collection ? $this->createQueryBuilderForCollection() : $this->createQueryBuilder(); |
|
|
|
|
148
|
|
|
|
149
|
8 |
|
$this->applyCriteria($queryBuilder, $criteria); |
150
|
8 |
|
$this->applySorting($queryBuilder, $orderBy); |
151
|
|
|
|
152
|
8 |
|
return $queryBuilder; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param Builder $queryBuilder |
157
|
|
|
* @param mixed[] $criteria |
158
|
|
|
*/ |
159
|
8 |
|
private function applyCriteria(Builder $queryBuilder, array $criteria = null) |
160
|
|
|
{ |
161
|
8 |
|
foreach ($criteria as $property => $value) { |
|
|
|
|
162
|
8 |
|
if ($value === null) { |
163
|
1 |
|
$queryBuilder->field($this->getProperty($property, $queryBuilder))->equals(null); |
164
|
8 |
|
} elseif (is_array($value)) { |
165
|
1 |
|
$queryBuilder->field($this->getProperty($property, $queryBuilder))->in($value); |
166
|
7 |
|
} elseif ($value !== null) { |
167
|
6 |
|
$queryBuilder->field($this->getProperty($property, $queryBuilder))->equals($value); |
168
|
6 |
|
} |
169
|
8 |
|
} |
170
|
8 |
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param Builder $queryBuilder |
174
|
|
|
* @param string[] $orderBy |
175
|
|
|
*/ |
176
|
8 |
View Code Duplication |
private function applySorting(Builder $queryBuilder, array $orderBy = []) |
|
|
|
|
177
|
|
|
{ |
178
|
8 |
|
foreach ($orderBy as $property => $order) { |
179
|
8 |
|
if (!empty($order)) { |
180
|
8 |
|
$queryBuilder->sort($this->getProperty($property, $queryBuilder), $order); |
181
|
8 |
|
} |
182
|
8 |
|
} |
183
|
8 |
|
} |
184
|
|
|
} |
185
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.