1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
5
|
|
|
* Date: 7/02/16 |
6
|
|
|
* Time: 15:58. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace NilPortugues\Foundation\Infrastructure\Model\Repository\EloquentMongoDB; |
12
|
|
|
|
13
|
|
|
use Jenssegers\Mongodb\Eloquent\Model; |
14
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Fields; |
15
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Filter; |
16
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Identity; |
17
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Page; |
18
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Pageable; |
19
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\PageRepository; |
20
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\ReadRepository; |
21
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Sort; |
22
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\WriteRepository; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class EloquentRepository. |
26
|
|
|
*/ |
27
|
|
|
abstract class EloquentRepository implements ReadRepository, WriteRepository, PageRepository |
28
|
|
|
{ |
29
|
|
|
/** @var Model */ |
30
|
|
|
protected static $instance; |
31
|
|
|
/** @var EloquentReadRepository */ |
32
|
|
|
protected $readRepository; |
33
|
|
|
/** @var EloquentWriteRepository */ |
34
|
|
|
protected $writeRepository; |
35
|
|
|
/** @var EloquentPageRepository */ |
36
|
|
|
protected $pageRepository; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* EloquentRepository constructor. |
40
|
|
|
*/ |
41
|
|
|
public function __construct() |
42
|
|
|
{ |
43
|
|
|
$eloquentModel = $this->getModelInstance(); |
44
|
|
|
|
45
|
|
|
$this->readRepository = EloquentReadRepository::create($eloquentModel); |
46
|
|
|
$this->writeRepository = EloquentWriteRepository::create($eloquentModel); |
47
|
|
|
$this->pageRepository = EloquentPageRepository::create($eloquentModel); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Retrieves an entity by its id. |
52
|
|
|
* |
53
|
|
|
* @param Identity|Model $id |
54
|
|
|
* @param Fields|null $fields |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
public function find(Identity $id, Fields $fields = null) |
59
|
|
|
{ |
60
|
|
|
return $this->readRepository->find($id, $fields); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns all instances of the type. |
65
|
|
|
* |
66
|
|
|
* @param Filter|null $filter |
67
|
|
|
* @param Sort|null $sort |
68
|
|
|
* @param Fields|null $fields |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
public function findBy(Filter $filter = null, Sort $sort = null, Fields $fields = null) |
73
|
|
|
{ |
74
|
|
|
return $this->readRepository->findBy($filter, $sort, $fields); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns an EloquentMongoDB Model instance. |
79
|
|
|
* |
80
|
|
|
* @return Model |
81
|
|
|
*/ |
82
|
|
|
protected function getModelInstance() |
83
|
|
|
{ |
84
|
|
|
if (null === self::$instance) { |
85
|
|
|
$modelInstance = $this->modelClassName(); |
86
|
|
|
self::$instance = new $modelInstance(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return self::$instance; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Must return the EloquentMongoDB Model Fully Qualified Class Name as a string. |
94
|
|
|
* |
95
|
|
|
* eg: return App\Model\User::class |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
abstract protected function modelClassName(); |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns whether an entity with the given id exists. |
103
|
|
|
* |
104
|
|
|
* @param Identity|Model $id |
105
|
|
|
* |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
|
|
public function exists(Identity $id) |
109
|
|
|
{ |
110
|
|
|
return $this->writeRepository->exists($id); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns the total amount of elements in the repository given the restrictions provided by the Filter object. |
115
|
|
|
* |
116
|
|
|
* @param Filter|null $filter |
117
|
|
|
* |
118
|
|
|
* @return int |
119
|
|
|
*/ |
120
|
|
|
public function count(Filter $filter = null) |
121
|
|
|
{ |
122
|
|
|
return $this->writeRepository->count($filter); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Adds a new entity to the storage. |
127
|
|
|
* |
128
|
|
|
* @param Identity|Model $value |
129
|
|
|
* |
130
|
|
|
* @return mixed |
131
|
|
|
*/ |
132
|
|
|
public function add(Identity $value) |
133
|
|
|
{ |
134
|
|
|
return $this->writeRepository->add($value); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Adds a collections of entities to the storage. |
139
|
|
|
* |
140
|
|
|
* @param Model[] $values |
141
|
|
|
* |
142
|
|
|
* @return mixed |
143
|
|
|
* |
144
|
|
|
* @throws \Exception |
145
|
|
|
*/ |
146
|
|
|
public function addAll(array $values) |
147
|
|
|
{ |
148
|
|
|
return $this->writeRepository->addAll($values); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Removes the entity with the given id. |
153
|
|
|
* |
154
|
|
|
* @param Identity|Model $id |
155
|
|
|
* |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
|
|
public function remove(Identity $id) |
159
|
|
|
{ |
160
|
|
|
return $this->writeRepository->remove($id); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Removes all elements in the repository given the restrictions provided by the Filter object. |
165
|
|
|
* If $filter is null, all the repository data will be deleted. |
166
|
|
|
* |
167
|
|
|
* @param Filter $filter |
168
|
|
|
* |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
|
|
public function removeAll(Filter $filter = null) |
172
|
|
|
{ |
173
|
|
|
return $this->writeRepository->removeAll($filter); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Returns a Page of entities meeting the paging restriction provided in the Pageable object. |
178
|
|
|
* |
179
|
|
|
* @param Pageable $pageable |
180
|
|
|
* |
181
|
|
|
* @return Page |
182
|
|
|
*/ |
183
|
|
|
public function findAll(Pageable $pageable = null) |
184
|
|
|
{ |
185
|
|
|
return $this->pageRepository->findAll($pageable); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Returns all instances of the type meeting $distinctFields values. |
190
|
|
|
* |
191
|
|
|
* @param Fields $distinctFields |
192
|
|
|
* @param Filter|null $filter |
193
|
|
|
* @param Sort|null $sort |
194
|
|
|
* |
195
|
|
|
* @return array |
196
|
|
|
*/ |
197
|
|
|
public function findByDistinct(Fields $distinctFields, Filter $filter = null, Sort $sort = null) |
198
|
|
|
{ |
199
|
|
|
return $this->readRepository->findByDistinct($distinctFields, $filter, $sort); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Repository data is added or removed as a whole block. |
204
|
|
|
* Must work or fail and rollback any persisted/erased data. |
205
|
|
|
* |
206
|
|
|
* @param callable $transaction |
207
|
|
|
* |
208
|
|
|
* @throws \Exception |
209
|
|
|
*/ |
210
|
|
|
public function transactional(callable $transaction) |
211
|
|
|
{ |
212
|
|
|
$this->writeRepository->transactional($transaction); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|