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 | * @throws ManganException |
||
| 98 | */ |
||
| 99 | 80 | public function __construct(AnnotatedInterface $model) |
|
| 100 | { |
||
| 101 | 80 | $this->model = $model; |
|
| 102 | 80 | $this->sm = new ScopeManager($model); |
|
| 103 | 80 | $this->options = new EntityOptions($model); |
|
| 104 | 80 | $this->collectionName = CollectionNamer::nameCollection($model); |
|
| 105 | 80 | $this->meta = ManganMeta::create($model); |
|
| 106 | 80 | $this->validator = new Validator($model); |
|
| 107 | 80 | $mangan = Mangan::fromModel($model); |
|
| 108 | 80 | if (!$this->collectionName) |
|
| 109 | 80 | { |
|
| 110 | throw new ManganException(sprintf('Invalid collection name for model: `%s`', $this->meta->type()->name)); |
||
| 111 | } |
||
| 112 | 80 | $this->_collection = new MongoCollection($mangan->getDbInstance(), $this->collectionName); |
|
| 113 | 80 | } |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Create model related entity manager. |
||
| 117 | * This will create customized entity manger if defined in model with EntityManager annotation. |
||
| 118 | * If no custom entity manager is defined this will return default EntityManager. |
||
| 119 | * @param AnnotatedInterface $model |
||
| 120 | * @return EntityManagerInterface |
||
| 121 | */ |
||
| 122 | 58 | public static function create($model) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Set attributes en masse. |
||
| 130 | * Attributes will be filtered according to SafeAnnotation. |
||
| 131 | * Only attributes marked as safe will be set, other will be ignored. |
||
| 132 | * |
||
| 133 | * @param mixed[] $atributes |
||
| 134 | */ |
||
| 135 | public function setAttributes($atributes) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Inserts a row into the table based on this active record attributes. |
||
| 142 | * If the table's primary key is auto-incremental and is null before insertion, |
||
| 143 | * it will be populated with the actual value after insertion. |
||
| 144 | * |
||
| 145 | * Note, validation is not performed in this method. You may call {@link validate} to perform the validation. |
||
| 146 | * After the record is inserted to DB successfully, its {@link isNewRecord} property will be set false, |
||
| 147 | * and its {@link scenario} property will be set to be 'update'. |
||
| 148 | * |
||
| 149 | * @param AnnotatedInterface $model if want to insert different model than set in constructor |
||
| 150 | * |
||
| 151 | * @return boolean whether the attributes are valid and the record is inserted successfully. |
||
| 152 | * @throws MongoException if the record is not new |
||
| 153 | * @throws MongoException on fail of insert or insert of empty document |
||
| 154 | * @throws MongoException on fail of insert, when safe flag is set to true |
||
| 155 | * @throws MongoException on timeout of db operation , when safe flag is set to true |
||
| 156 | * @since v1.0 |
||
| 157 | */ |
||
| 158 | 30 | public function insert(AnnotatedInterface $model = null) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Updates the row represented by this active document. |
||
| 179 | * All loaded attributes will be saved to the database. |
||
| 180 | * Note, validation is not performed in this method. You may call {@link validate} to perform the validation. |
||
| 181 | * |
||
| 182 | * @param array $attributes list of attributes that need to be saved. Defaults to null, |
||
| 183 | * meaning all attributes that are loaded from DB will be saved. |
||
| 184 | |||
| 185 | * @return boolean whether the update is successful |
||
| 186 | * @throws MongoException if the record is new |
||
| 187 | * @throws MongoException on fail of update |
||
| 188 | * @throws MongoException on timeout of db operation , when safe flag is set to true |
||
| 189 | * @since v1.0 |
||
| 190 | */ |
||
| 191 | 7 | public function update(array $attributes = null) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Atomic, in-place update method. |
||
| 232 | * |
||
| 233 | * @since v1.3.6 |
||
| 234 | * @param Modifier $modifier updating rules to apply |
||
| 235 | * @param CriteriaInterface $criteria condition to limit updating rules |
||
| 236 | * @return boolean |
||
| 237 | */ |
||
| 238 | 1 | public function updateAll(Modifier $modifier, CriteriaInterface $criteria = null) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Saves the current record. |
||
| 257 | * |
||
| 258 | * The record is inserted as a row into the database collection or updated if exists. |
||
| 259 | * |
||
| 260 | * Validation will be performed before saving the record. If the validation fails, |
||
| 261 | * the record will not be saved. You can call {@link getErrors()} to retrieve the |
||
| 262 | * validation errors. |
||
| 263 | * |
||
| 264 | * @param boolean $runValidation whether to perform validation before saving the record. |
||
| 265 | * If the validation fails, the record will not be saved to database. |
||
| 266 | * @param AnnotatedInterface $model if want to insert different model than set in constructor |
||
| 267 | * @return boolean whether the saving succeeds |
||
| 268 | * @since v1.0 |
||
| 269 | */ |
||
| 270 | 54 | public function save($runValidation = true, $model = null) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Reloads document from database. |
||
| 298 | * It return true if document is reloaded and false if it's no longer exists. |
||
| 299 | * |
||
| 300 | * @return boolean |
||
| 301 | */ |
||
| 302 | 2 | public function refresh() |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Deletes the document from database. |
||
| 319 | * @return boolean whether the deletion is successful. |
||
| 320 | * @throws MongoException if the record is new |
||
| 321 | */ |
||
| 322 | 8 | public function delete() |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Deletes one document with the specified primary keys. |
||
| 346 | * <b>Does not raise beforeDelete</b> |
||
| 347 | * See {@link find()} for detailed explanation about $condition and $params. |
||
| 348 | * @param array|CriteriaInterface $criteria query criteria. |
||
| 349 | * @since v1.0 |
||
| 350 | */ |
||
| 351 | 7 | public function deleteOne($criteria = null) |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Deletes document with the specified primary key. |
||
| 363 | * See {@link find()} for detailed explanation about $condition and $params. |
||
| 364 | * @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). |
||
| 365 | * @param array|CriteriaInterface $criteria query criteria. |
||
| 366 | * @since v1.0 |
||
| 367 | */ |
||
| 368 | 2 | public function deleteByPk($pkValue, $criteria = null) |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Deletes documents with the specified primary keys. |
||
| 385 | * See {@link find()} for detailed explanation about $condition and $params. |
||
| 386 | * @param mixed[] $pkValues Primary keys array |
||
| 387 | * @param array|CriteriaInterface $criteria query criteria. |
||
| 388 | * @since v1.0 |
||
| 389 | */ |
||
| 390 | 2 | public function deleteAllByPk($pkValues, $criteria = null) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Deletes documents with the specified primary keys. |
||
| 406 | * <b>Does not raise beforeDelete</b> |
||
| 407 | * See {@link find()} for detailed explanation about $condition and $params. |
||
| 408 | * @param array|CriteriaInterface $criteria query criteria. |
||
| 409 | * @since v1.0 |
||
| 410 | */ |
||
| 411 | 3 | public function deleteAll($criteria = null) |
|
| 420 | |||
| 421 | 66 | public function getCollection() |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Make status uniform |
||
| 428 | * @param bool|array $result |
||
| 429 | * @param bool $insert Set to true for inserts |
||
| 430 | * @return bool Return true if secceed |
||
| 431 | */ |
||
| 432 | 76 | private function _result($result, $insert = false) |
|
| 444 | |||
| 445 | // <editor-fold defaultstate="collapsed" desc="Event and Signal handling"> |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Take care of EventBeforeSave |
||
| 449 | * @see EventBeforeSave |
||
| 450 | * @return boolean |
||
| 451 | */ |
||
| 452 | 79 | private function _beforeSave($model, $event = null) |
|
| 465 | |||
| 466 | /** |
||
| 467 | * Take care of EventAfterSave |
||
| 468 | * @see EventAfterSave |
||
| 469 | */ |
||
| 470 | 76 | private function _afterSave($model, $event = null) |
|
| 480 | |||
| 481 | /** |
||
| 482 | * This method is invoked before deleting a record. |
||
| 483 | * The default implementation raises the {@link onBeforeDelete} event. |
||
| 484 | * You may override this method to do any preparation work for record deletion. |
||
| 485 | * Make sure you call the parent implementation so that the event is raised properly. |
||
| 486 | * @return boolean whether the record should be deleted. Defaults to true. |
||
| 487 | * @since v1.0 |
||
| 488 | */ |
||
| 489 | 10 | private function _beforeDelete() |
|
| 499 | |||
| 500 | /** |
||
| 501 | * This method is invoked after deleting a record. |
||
| 502 | * The default implementation raises the {@link onAfterDelete} event. |
||
| 503 | * You may override this method to do postprocessing after the record is deleted. |
||
| 504 | * Make sure you call the parent implementation so that the event is raised properly. |
||
| 505 | * @since v1.0 |
||
| 506 | */ |
||
| 507 | 7 | private function _afterDelete() |
|
| 512 | |||
| 513 | // </editor-fold> |
||
| 514 | } |
||
| 515 |