Completed
Push — master ( d1d4a9...91184f )
by Peter
12:35
created

EntityManagerTrait::findAndModify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
1
<?php
2
3
/**
4
 * This software package is licensed under AGPL or Commercial license.
5
 *
6
 * @package maslosoft/mangan
7
 * @licence AGPL or Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]>
9
 * @copyright Copyright (c) Maslosoft
10
 * @copyright Copyright (c) Others as mentioned in code
11
 * @link https://maslosoft.com/mangan/
12
 */
13
14
namespace Maslosoft\Mangan\Traits;
15
16
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
17
use Maslosoft\Mangan\EntityManager;
18
use Maslosoft\Mangan\Exceptions\ManganException;
19
use Maslosoft\Mangan\Interfaces\CriteriaInterface;
20
use Maslosoft\Mangan\Interfaces\EntityManagerInterface;
21
use Maslosoft\Mangan\Modifier;
22
use MongoCollection;
23
24
/**
25
 * This trait contains same methods as `EntityManagerInterface`, and it forwards
26
 * them to concrete Entity Manager class. Entity manager used by this trait
27
 * can be defined via EntityManager annotation.
28
 *
29
 * This is useful to create Active Document
30
 * pattern classes.
31
 *
32
 * @see EntityManagerInterface
33
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
34
 */
35
trait EntityManagerTrait
36
{
37
38
	/**
39
	 * Entity manager
40
	 * @var EntityManagerInterface|EntityManager
41
	 */
42
	private $_em = null;
43
44
	/**
45
	 * Replaces the current document.
46
	 *
47
	 * **NOTE: This will overwrite entire document.**
48
	 * 
49
	 * Any filtered out properties will be removed as well.
50
	 *
51
	 * The record is inserted as a documnent into the database collection, if exists it will be replaced.
52
	 *
53
	 * Validation will be performed before saving the record. If the validation fails,
54
	 * the record will not be saved. You can call `getErrors()` to retrieve the
55
	 * validation errors.
56
	 *
57
	 * @param boolean $runValidation whether to perform validation before saving the record.
58
	 * If the validation fails, the record will not be saved to database.
59
	 *
60
	 * @return boolean whether the saving succeeds
61
	 * @since v1.0
62
	 */
63
	public function replace($runValidation = true)
64
	{
65
		return $this->_getEm()->replace($runValidation);
66
	}
67
68
	/**
69
	 * Saves the current document.
70
	 *
71
	 * The document is inserted into collection if it is not already saved. The
72
	 * check whether to update or insert document is dony by primary key.
73
	 *
74
	 * Validation will be performed before saving the record. If the validation fails,
75
	 * the record will not be saved. You can call `getErrors()` to retrieve the
76
	 * validation errors.
77
	 *
78
	 * If the record is saved its scenario will be set to be 'update'.
79
	 * And if its primary key is of type of `MongoId`, it will be set after save.
80
	 *
81
	 * @param boolean $runValidation whether to perform validation before saving the record.
82
	 * If the validation fails, the record will not be saved to database.
83
	 *
84
	 * @return boolean whether the saving succeeds
85
	 * @since v1.0
86
	 * @Ignored
87
	 */
88 35
	public function save($runValidation = true)
89
	{
90 35
		return $this->_getEm()->save($runValidation);
91
	}
92
93
	/**
94
	 * Updates or inserts the current document. This will try to update existing fields.
95
	 * Will keep already stored data if present in document.
96
	 *
97
	 * If document does not exist, a new one will be inserted.
98
	 *
99
	 * @param boolean $runValidation
100
	 * @return boolean
101
	 * @throws ManganException
102
	 */
103
	public function upsert($runValidation = true)
104
	{
105
		return $this->_getEm()->upsert($runValidation);
106
	}
107
108
	/**
109
	 * Inserts a document into the collection based on this active document attributes.
110
	 *
111
	 * Note, validation is not performed in this method. You may call `validate()` to perform the validation.
112
	 *
113
	 * After the record is inserted to DB successfully, its  scenario will be set to be 'update'.
114
	 * 
115
	 * @param AnnotatedInterface $model if want to insert different model than set in constructor
116
	 * @return boolean whether the attributes are valid and the record is inserted successfully.
117
	 * @throws ManganException if the record is not new
118
	 * @throws ManganException on fail of insert or insert of empty document
119
	 * @throws ManganException on fail of insert, when safe flag is set to true
120
	 * @throws ManganException on timeout of db operation , when safe flag is set to true
121
	 * @since v1.0
122
	 * @Ignored
123
	 */
124 3
	public function insert(AnnotatedInterface $model = null)
125
	{
126 3
		return $this->_getEm()->insert($model);
127
	}
128
129
	/**
130
	 * Updates the document represented by this active document.
131
	 * All loaded attributes will be saved to the database.
132
	 * Note, validation is not performed in this method. You may call `validate()` to perform the validation.
133
	 * 
134
	 * @param array $attributes list of attributes that need to be updated. Defaults to null,
135
	 * meaning all attributes that are loaded from DB will be saved.
136
	 * @return boolean whether the update is successful
137
	 * @throws ManganException if the record is new
138
	 * @throws ManganException on fail of update
139
	 * @throws ManganException on timeout of db operation , when safe flag is set to true
140
	 * @since v1.0
141
	 * @Ignored
142
	 */
143 2
	public function update(array $attributes = null)
144
	{
145 2
		return $this->_getEm()->update($attributes);
146
	}
147
148
	/**
149
	 * Updates one document with the specified criteria and attributes
150
	 *
151
	 * This is more *raw* update:
152
	 *
153
	 * * Does not raise any events or signals
154
	 * * Does not perform any validation
155
	 *
156
	 * @param array|CriteriaInterface $criteria query criteria.
157
	 * @param array $attributes list of attributes that need to be saved. Defaults to null,
158
	 * meaning all attributes that are loaded from DB will be saved.
159
	 * @param bool Whether tu force update/upsert document
160
	 * @since v1.0
161
	 */
162
	public function updateOne($criteria = null, array $attributes = null, $modify = false)
163
	{
164
		return $this->_getEm()->updateOne($criteria, $attributes, $modify);
165
	}
166
167
	/**
168
	 * Atomic, in-place update method.
169
	 *
170
	 * @since v1.3.6
171
	 * @param Modifier $modifier updating rules to apply
172
	 * @param CriteriaInterface $criteria condition to limit updating rules
173
	 * @return boolean|mixed[]
174
	 * @Ignored
175
	 */
176
	public function updateAll(Modifier $modifier, CriteriaInterface $criteria = null)
177
	{
178
		return $this->_getEm()->updateAll($modifier, $criteria);
179
	}
180
181
	/**
182
	 * Find and modify single document atomically.
183
	 *
184
	 * By default this function will return updated document, ie document
185
	 * with applied Modifier operations.
186
	 *
187
	 * To return document before applied updates, set parameter
188
	 * `$returnUpdated` to false.
189
	 *
190
	 * This function will raise events and signals before operation on
191
	 * current model.
192
	 *
193
	 * The events and signals after operation will be performed
194
	 * on the returned model, depending on `$returnUpdated` parameter.
195
	 *
196
	 * @param array|CriteriaInterface $criteria
197
	 * @param Modifier                $modifier
198
	 * @param bool                    $returnUpdated
199
	 * @return AnnotatedInterface|null
200
	 */
201
	public function findAndModify($criteria, Modifier $modifier, $returnUpdated = true)
202
	{
203
		return $this->_getEm()->findAndModify($criteria, $modifier, $returnUpdated);
204
	}
205
206
	/**
207
	 * Deletes the databse document corresponding to this `Document`.
208
	 * @return boolean whether the deletion is successful.
209
	 * @throws ManganException if the record is new
210
	 * @since v1.0
211
	 * @Ignored
212
	 */
213 7
	public function delete()
214
	{
215 7
		return $this->_getEm()->delete();
216
	}
217
218
	/**
219
	 * Deletes document with the specified primary key.
220
	 * 
221
	 * Additional `$criteria` can be used to filter out which document should be deleted.
222
	 *
223
	 * See `find()` for detailed explanation about `$criteria`.
224
	 *
225
	 * @param mixed $pkValue primary key value(s). Use array for multiple primary keys. For composite key, each key value must be an array (column name=>column value).
226
	 * @param array|CriteriaInterface $criteria query criteria.
227
	 * @since v1.0
228
	 * @Ignored
229
	 */
230 1
	public function deleteByPk($pkValue, $criteria = null)
231
	{
232 1
		return $this->_getEm()->deleteByPk($pkValue, $criteria);
233
	}
234
235
	/**
236
	 * Deletes documents with the specified primary keys.
237
	 * 
238
	 * Additional `$criteria` can be used to filter out which documents should be deleted.
239
	 *
240
	 * See `find()` for detailed explanation about `$criteria`.
241
	 *
242
	 * @param mixed[] $pkValues Primary keys array
243
	 * @param array|CriteriaInterface $criteria query criteria.
244
	 * @since v1.0
245
	 * @Ignored
246
	 */
247 1
	public function deleteAllByPk($pkValues, $criteria = null)
248
	{
249 1
		return $this->_getEm()->deleteAllByPk($pkValues, $criteria);
250
	}
251
252
	/**
253
	 * Deletes documents with the specified criteria.
254
	 *
255
	 * Optional `$criteria` can be used to filter out which documents should be deleted.
256
	 *
257
	 * See `find()` for detailed explanation about $criteria.
258
	 *
259
	 * @param array|CriteriaInterface $criteria query criteria.
260
	 * @since v1.0
261
	 * @Ignored
262
	 */
263 1
	public function deleteAll($criteria = null)
264
	{
265 1
		return $this->_getEm()->deleteAll($criteria);
266
	}
267
268
	/**
269
	 * Deletes one document with the specified primary keys.
270
	 *
271
	 * Optional `$criteria` can be used to filter out which document should be deleted.
272
	 *
273
	 * **Does not raise `beforeDelete` event**
274
	 *
275
	 * See `find()` for detailed explanation about $criteria
276
	 * @param array|CriteriaInterface $criteria query criteria.
277
	 * @since v1.0
278
	 * @Ignored
279
	 */
280
	public function deleteOne($criteria = null)
281
	{
282
		return $this->_getEm()->deleteOne($criteria);
283
	}
284
285
	/**
286
	 * Repopulates this active document with the latest data.
287
	 *
288
	 * @return boolean whether the row still exists in the database. If true, the latest data will be populated to this active record.
289
	 * @since v1.0
290
	 * @Ignored
291
	 */
292
	public function refresh()
293
	{
294
		return $this->_getEm()->refresh();
295
	}
296
297
	/**
298
	 * Get working mongo collection instance.
299
	 *
300
	 * Should not be called manually in most cases.
301
	 *
302
	 * @return MongoCollection
303
	 * @Ignored
304
	 */
305
	public function getCollection()
306
	{
307
		return $this->_getEm()->getCollection();
308
	}
309
310
	/**
311
	 * Get entity manager instance
312
	 * @return EntityManagerInterface|EntityManager
313
	 */
314 40
	private function _getEm()
315
	{
316 40
		if (null === $this->_em)
317
		{
318 40
			$this->_em = EntityManager::create($this);
319
		}
320 40
		return $this->_em;
321
	}
322
323
}
324