Complex classes like EntityManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EntityManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
43 | class EntityManager implements EntityManagerInterface |
||
44 | { |
||
45 | |||
46 | /** |
||
47 | * Model |
||
48 | * @var AnnotatedInterface |
||
49 | */ |
||
50 | public $model = null; |
||
51 | |||
52 | /** |
||
53 | * |
||
54 | * @var EventDispatcher |
||
55 | */ |
||
56 | public $ed = null; |
||
57 | |||
58 | /** |
||
59 | * |
||
60 | * @var ScopeManager |
||
61 | */ |
||
62 | private $sm = null; |
||
63 | |||
64 | /** |
||
65 | * |
||
66 | * @var |
||
67 | */ |
||
68 | public $meta = null; |
||
69 | |||
70 | /** |
||
71 | * Options |
||
72 | * @var EntityOptions |
||
73 | */ |
||
74 | public $options = null; |
||
75 | |||
76 | /** |
||
77 | * Current collection name |
||
78 | * @var string |
||
79 | */ |
||
80 | public $collectionName = ''; |
||
81 | |||
82 | /** |
||
83 | * Validator instance |
||
84 | * @var Validator |
||
85 | */ |
||
86 | private $validator = null; |
||
87 | |||
88 | /** |
||
89 | * Current collection |
||
90 | * @var MongoCollection |
||
91 | */ |
||
92 | private $_collection = null; |
||
93 | |||
94 | /** |
||
95 | * Create entity manager |
||
96 | * @param AnnotatedInterface $model |
||
97 | * @param Mangan $mangan |
||
98 | * @throws ManganException |
||
99 | */ |
||
100 | 102 | public function __construct(AnnotatedInterface $model, Mangan $mangan = null) |
|
118 | |||
119 | /** |
||
120 | * Create model related entity manager. |
||
121 | * This will create customized entity manger if defined in model with EntityManager annotation. |
||
122 | * If no custom entity manager is defined this will return default EntityManager. |
||
123 | * @param AnnotatedInterface $model |
||
124 | * @param Mangan $mangan |
||
125 | * @return EntityManagerInterface |
||
126 | */ |
||
127 | 94 | public static function create($model, Mangan $mangan = null) |
|
132 | |||
133 | /** |
||
134 | * Set attributes en masse. |
||
135 | * Attributes will be filtered according to SafeAnnotation. |
||
136 | * Only attributes marked as safe will be set, other will be ignored. |
||
137 | * |
||
138 | * @param mixed[] $atributes |
||
139 | */ |
||
140 | public function setAttributes($atributes) |
||
144 | |||
145 | /** |
||
146 | * Inserts a row into the table based on this active record attributes. |
||
147 | * If the table's primary key is auto-incremental and is null before insertion, |
||
148 | * it will be populated with the actual value after insertion. |
||
149 | * |
||
150 | * Note, validation is not performed in this method. You may call {@link validate} to perform the validation. |
||
151 | * After the record is inserted to DB successfully, its {@link isNewRecord} property will be set false, |
||
152 | * and its {@link scenario} property will be set to be 'update'. |
||
153 | * |
||
154 | * @param AnnotatedInterface $model if want to insert different model than set in constructor |
||
155 | * |
||
156 | * @return boolean whether the attributes are valid and the record is inserted successfully. |
||
157 | * @throws ManganException if the record is not new |
||
158 | * @throws ManganException on fail of insert or insert of empty document |
||
159 | * @throws ManganException on fail of insert, when safe flag is set to true |
||
160 | * @throws ManganException on timeout of db operation , when safe flag is set to true |
||
161 | * @since v1.0 |
||
162 | */ |
||
163 | 36 | public function insert(AnnotatedInterface $model = null) |
|
187 | |||
188 | /** |
||
189 | * Updates the row represented by this active document. |
||
190 | * All loaded attributes will be saved to the database. |
||
191 | * Note, validation is not performed in this method. You may call {@link validate} to perform the validation. |
||
192 | * |
||
193 | * @param array $attributes list of attributes that need to be saved. Defaults to null, |
||
194 | * meaning all attributes that are loaded from DB will be saved. |
||
195 | |||
196 | 6 | * @return boolean whether the update is successful |
|
197 | * @throws ManganException if the record is new |
||
198 | 6 | * @throws ManganException on fail of update |
|
199 | * @throws ManganException on timeout of db operation , when safe flag is set to true |
||
200 | 6 | * @since v1.0 |
|
201 | 6 | */ |
|
202 | 5 | public function update(array $attributes = null) |
|
217 | |||
218 | /** |
||
219 | * Updates one document with the specified criteria and attributes |
||
220 | * |
||
221 | * This is more *raw* update: |
||
222 | * |
||
223 | * * Does not raise any events or signals |
||
224 | * * Does not perform any validation |
||
225 | * |
||
226 | 66 | * @param array|CriteriaInterface $criteria query criteria. |
|
227 | * @param array $attributes list of attributes that need to be saved. Defaults to null, |
||
228 | 66 | * @param bool Whether tu force update/upsert document |
|
229 | 66 | * meaning all attributes that are loaded from DB will be saved. |
|
230 | * @since v1.0 |
||
231 | */ |
||
232 | 66 | public function updateOne($criteria = null, array $attributes = null, $modify = false) |
|
282 | |||
283 | /** |
||
284 | * Atomic, in-place update method. |
||
285 | 1 | * |
|
286 | * @since v1.3.6 |
||
287 | 1 | * @param Modifier $modifier updating rules to apply |
|
288 | * @param CriteriaInterface $criteria condition to limit updating rules |
||
289 | 1 | * @return boolean |
|
290 | 1 | */ |
|
291 | 1 | public function updateAll(Modifier $modifier, CriteriaInterface $criteria = null) |
|
307 | |||
308 | /** |
||
309 | * Replaces the current document. |
||
310 | * |
||
311 | * **NOTE: This will overwrite entire document.** |
||
312 | * Any filtered out properties will be removed as well. |
||
313 | * |
||
314 | * The record is inserted as a documnent into the database collection, if exists it will be replaced. |
||
315 | * |
||
316 | * Validation will be performed before saving the record. If the validation fails, |
||
317 | * the record will not be saved. You can call {@link getErrors()} to retrieve the |
||
318 | * validation errors. |
||
319 | * |
||
320 | * @param boolean $runValidation whether to perform validation before saving the record. |
||
321 | * If the validation fails, the record will not be saved to database. |
||
322 | * |
||
323 | * @return boolean whether the saving succeeds |
||
324 | * @since v1.0 |
||
325 | */ |
||
326 | public function replace($runValidation = true) |
||
351 | |||
352 | /** |
||
353 | * Saves the current document. |
||
354 | * |
||
355 | * The record is inserted as a document into the database collection or updated if exists. |
||
356 | * |
||
357 | * Filtered out properties will remain in database - it is partial safe. |
||
358 | * |
||
359 | * Validation will be performed before saving the record. If the validation fails, |
||
360 | * the record will not be saved. You can call {@link getErrors()} to retrieve the |
||
361 | * validation errors. |
||
362 | * |
||
363 | 65 | * @param boolean $runValidation whether to perform validation before saving the record. |
|
364 | * If the validation fails, the record will not be saved to database. |
||
365 | 65 | * |
|
366 | * @return boolean whether the saving succeeds |
||
367 | * @since v1.0 |
||
368 | */ |
||
369 | public function save($runValidation = true) |
||
373 | |||
374 | /** |
||
375 | * Updates or inserts the current document. This will try to update existing fields. |
||
376 | * Will keep already stored data if present in document. |
||
377 | * |
||
378 | 67 | * If document does not exist, a new one will be inserted. |
|
379 | * |
||
380 | 67 | * @param boolean $runValidation |
|
381 | * @return boolean |
||
382 | 67 | * @throws ManganException |
|
383 | 67 | */ |
|
384 | public function upsert($runValidation = true) |
||
415 | |||
416 | 2 | /** |
|
417 | * Reloads document from database. |
||
418 | 2 | * It return true if document is reloaded and false if it's no longer exists. |
|
419 | 2 | * |
|
420 | 2 | * @return boolean |
|
421 | */ |
||
422 | 2 | public function refresh() |
|
436 | 10 | ||
437 | /** |
||
438 | 10 | * Deletes the document from database. |
|
439 | * @return boolean whether the deletion is successful. |
||
440 | 9 | * @throws ManganException if the record is new |
|
441 | */ |
||
442 | 9 | public function delete() |
|
463 | |||
464 | /** |
||
465 | 11 | * Deletes one document with the specified primary keys. |
|
466 | * <b>Does not raise beforeDelete</b> |
||
467 | 11 | * See {@link find()} for detailed explanation about $condition and $params. |
|
468 | * @param array|CriteriaInterface $criteria query criteria. |
||
469 | 11 | * @since v1.0 |
|
470 | 11 | */ |
|
471 | public function deleteOne($criteria = null) |
||
480 | |||
481 | /** |
||
482 | 2 | * Deletes document with the specified primary key. |
|
483 | * See {@link find()} for detailed explanation about $condition and $params. |
||
484 | 2 | * @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). |
|
485 | * @param array|CriteriaInterface $criteria query criteria. |
||
486 | 1 | * @since v1.0 |
|
487 | 1 | */ |
|
488 | public function deleteByPk($pkValue, $criteria = null) |
||
502 | |||
503 | /** |
||
504 | 2 | * Deletes documents with the specified primary keys. |
|
505 | * See {@link find()} for detailed explanation about $condition and $params. |
||
506 | 2 | * @param mixed[] $pkValues Primary keys array |
|
507 | * @param array|CriteriaInterface $criteria query criteria. |
||
508 | 1 | * @since v1.0 |
|
509 | 1 | */ |
|
510 | 1 | public function deleteAllByPk($pkValues, $criteria = null) |
|
523 | |||
524 | /** |
||
525 | * Deletes documents with the specified primary keys. |
||
526 | * |
||
527 | * **Does not raise beforeDelete event and does not emit signals** |
||
528 | 7 | * |
|
529 | * See {@link find()} for detailed explanation about $condition and $params. |
||
530 | 7 | * |
|
531 | * @param array|CriteriaInterface $criteria query criteria. |
||
532 | 7 | * @since v1.0 |
|
533 | 7 | */ |
|
534 | public function deleteAll($criteria = null) |
||
543 | |||
544 | public function getCollection() |
||
548 | |||
549 | 97 | /** |
|
550 | * Make status uniform |
||
551 | 97 | * @param bool|array $result |
|
552 | * @param bool $insert Set to true for inserts |
||
553 | 97 | * @return bool Return true if secceed |
|
554 | */ |
||
555 | 36 | private function _result($result, $insert = false) |
|
567 | |||
568 | // <editor-fold defaultstate="collapsed" desc="Event and Signal handling"> |
||
569 | 99 | ||
570 | /** |
||
571 | 99 | * Take care of EventBeforeSave |
|
572 | 99 | * @see EventBeforeSave |
|
573 | * @return boolean |
||
574 | 96 | */ |
|
575 | private function _beforeSave($model, $event = null) |
||
588 | |||
589 | 96 | /** |
|
590 | 96 | * Take care of EventAfterSave |
|
591 | * @see EventAfterSave |
||
592 | 41 | */ |
|
593 | private function _afterSave($model, $event = null) |
||
603 | |||
604 | /** |
||
605 | * This method is invoked before deleting a record. |
||
606 | 12 | * The default implementation raises the {@link onBeforeDelete} event. |
|
607 | * You may override this method to do any preparation work for record deletion. |
||
608 | 12 | * Make sure you call the parent implementation so that the event is raised properly. |
|
609 | 12 | * @return boolean whether the record should be deleted. Defaults to true. |
|
610 | * @since v1.0 |
||
611 | 11 | */ |
|
612 | 11 | private function _beforeDelete() |
|
622 | |||
623 | /** |
||
624 | 9 | * This method is invoked after deleting a record. |
|
625 | * The default implementation raises the {@link onAfterDelete} event. |
||
626 | 9 | * You may override this method to do postprocessing after the record is deleted. |
|
627 | 9 | * Make sure you call the parent implementation so that the event is raised properly. |
|
628 | 9 | * @since v1.0 |
|
629 | 9 | */ |
|
630 | private function _afterDelete() |
||
636 | |||
637 | // </editor-fold> |
||
638 | } |
||
639 |