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