1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
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 Cubiche\Infrastructure\Repository\Doctrine\ODM\MongoDB; |
13
|
|
|
|
14
|
|
|
use Cubiche\Core\Collections\DataSource\IteratorDataSource; |
15
|
|
|
use Cubiche\Core\Collections\DataSourceSet; |
16
|
|
|
use Cubiche\Core\Comparable\ComparatorInterface; |
17
|
|
|
use Cubiche\Core\Specification\SpecificationInterface; |
18
|
|
|
use Cubiche\Domain\Model\IdInterface; |
19
|
|
|
use Cubiche\Domain\Repository\QueryRepository; |
20
|
|
|
use Doctrine\ODM\MongoDB\DocumentRepository as MongoDBDocumentRepository; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* DocumentQueryRepository Class. |
24
|
|
|
* |
25
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
26
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class DocumentQueryRepository extends QueryRepository |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var MongoDBDocumentRepository |
32
|
|
|
*/ |
33
|
|
|
protected $repository; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var DocumentDataSourceFactoryInterface |
37
|
|
|
*/ |
38
|
|
|
protected $documentDataSourceFactory; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var DocumentDataSource |
42
|
|
|
*/ |
43
|
|
|
private $documentDataSource = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param MongoDBDocumentRepository $repository |
47
|
|
|
* @param DocumentDataSourceFactoryInterface $documentDataSourceFactory |
48
|
|
|
*/ |
49
|
|
|
public function __construct( |
50
|
|
|
MongoDBDocumentRepository $repository, |
51
|
|
|
DocumentDataSourceFactoryInterface $documentDataSourceFactory |
52
|
|
|
) { |
53
|
|
|
$this->entityReflectionClass = new \ReflectionClass($repository->getDocumentName()); |
54
|
|
|
|
55
|
|
|
$this->repository = $repository; |
56
|
|
|
$this->documentDataSourceFactory = $documentDataSourceFactory; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function clear() |
63
|
|
|
{ |
64
|
|
|
$this->repository |
65
|
|
|
->createQueryBuilder() |
66
|
|
|
->remove() |
67
|
|
|
->getQuery() |
68
|
|
|
->execute() |
69
|
|
|
; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function isEmpty() |
76
|
|
|
{ |
77
|
|
|
return $this->count() === 0; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function toArray() |
84
|
|
|
{ |
85
|
|
|
return $this->repository->createQueryBuilder()->getQuery()->toArray(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function getIterator() |
92
|
|
|
{ |
93
|
|
|
return $this->repository->createQueryBuilder()->getQuery()->getIterator(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function sorted(ComparatorInterface $criteria) |
100
|
|
|
{ |
101
|
|
|
return new DataSourceSet($this->documentDataSource()->sortedDataSource($criteria)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
public function slice($offset, $length = null) |
108
|
|
|
{ |
109
|
|
|
$queryBuilder = $this->repository->createQueryBuilder(); |
110
|
|
|
$queryBuilder->skip($offset); |
111
|
|
|
|
112
|
|
|
if ($length !== null) { |
113
|
|
|
$queryBuilder->limit($length); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return new DataSourceSet(new IteratorDataSource($queryBuilder->getQuery()->getIterator())); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
|
|
public function find(SpecificationInterface $criteria) |
123
|
|
|
{ |
124
|
|
|
return new DataSourceSet($this->documentDataSource()->filteredDataSource($criteria)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
|
|
public function findOne(SpecificationInterface $criteria) |
131
|
|
|
{ |
132
|
|
|
return $this->documentDataSource()->filteredDataSource($criteria)->findOne(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
|
|
public function get(IdInterface $id) |
139
|
|
|
{ |
140
|
|
|
return $this->repository->find($id->toNative()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
|
|
public function persist($element) |
147
|
|
|
{ |
148
|
|
|
$this->checkType($element); |
149
|
|
|
|
150
|
|
|
$this->dm()->persist($element); |
151
|
|
|
$this->dm()->flush(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritdoc} |
156
|
|
|
*/ |
157
|
|
|
public function persistAll($elements) |
158
|
|
|
{ |
159
|
|
|
foreach ($elements as $element) { |
160
|
|
|
$this->checkType($element); |
161
|
|
|
$this->dm()->persist($element); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$this->dm()->flush(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* {@inheritdoc} |
169
|
|
|
*/ |
170
|
|
|
public function remove($element) |
171
|
|
|
{ |
172
|
|
|
$this->checkType($element); |
173
|
|
|
|
174
|
|
|
$this->dm()->remove($element); |
175
|
|
|
$this->dm()->flush(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* {@inheritdoc} |
180
|
|
|
*/ |
181
|
|
|
public function count() |
182
|
|
|
{ |
183
|
|
|
return $this->repository->createQueryBuilder()->getQuery()->count(true); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return \Doctrine\ODM\MongoDB\DocumentManager |
188
|
|
|
*/ |
189
|
|
|
protected function dm() |
190
|
|
|
{ |
191
|
|
|
return $this->repository->getDocumentManager(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return \Cubiche\Infrastructure\Repository\Doctrine\ODM\MongoDB\DocumentDataSource |
196
|
|
|
*/ |
197
|
|
|
protected function documentDataSource() |
198
|
|
|
{ |
199
|
|
|
if ($this->documentDataSource === null) { |
200
|
|
|
$this->documentDataSource = $this->documentDataSourceFactory->create($this->repository->getDocumentName()); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $this->documentDataSource; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|