Complex classes like AbstractDataMapper 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 AbstractDataMapper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | abstract class AbstractDataMapper implements RepositoryInterface, MapperInterface |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * адаптер для работы с бд |
||
| 14 | * @var type |
||
| 15 | */ |
||
| 16 | protected $adapter; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * таблица для сущности |
||
| 20 | * @var type |
||
| 21 | */ |
||
| 22 | protected $entityTable; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * первичный ключ |
||
| 26 | * @var type |
||
| 27 | */ |
||
| 28 | protected $key; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Использование join при выборке |
||
| 32 | * @var type |
||
| 33 | */ |
||
| 34 | protected $use_joins = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Использование мягкое удаление |
||
| 38 | * @var type |
||
| 39 | */ |
||
| 40 | protected $use_delete = false; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * поле для мягкого удаления |
||
| 44 | * @var type |
||
| 45 | */ |
||
| 46 | protected $soft_delete_key; |
||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * поля сущности |
||
| 51 | * @var type |
||
| 52 | */ |
||
| 53 | protected $mapping_fields = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * псевдонимы полей сущности |
||
| 57 | * @var type |
||
| 58 | */ |
||
| 59 | protected $mapping_fields_aliases = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * связи с другими мапперами |
||
| 63 | * @var type |
||
| 64 | */ |
||
| 65 | protected $relations = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Контейнер |
||
| 69 | * @var League\Container\Container |
||
| 70 | */ |
||
| 71 | protected $DI; |
||
| 72 | |||
| 73 | function __construct(\League\Container\Container $DI, QueryBuilderInterface $adapter, $db_name = null) { |
||
| 88 | |||
| 89 | abstract protected function setMappingFields(); |
||
| 90 | |||
| 91 | public function getAdapter() { |
||
| 94 | |||
| 95 | public function setAdapter(QueryBuilderInterface $adapter){ |
||
| 98 | |||
| 99 | |||
| 100 | protected function getEntityTable() { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Уставнока таблицы |
||
| 106 | */ |
||
| 107 | protected function setEntityTable($db_name) { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Получение записи по ключу |
||
| 113 | * @param type $id |
||
| 114 | * @return type |
||
| 115 | */ |
||
| 116 | public function findById($id) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Сохранение сущности без спобытий |
||
| 125 | * @param \SimpleORM\EntityInterface $Entity |
||
| 126 | */ |
||
| 127 | public function saveWithoutEvents(EntityInterface $Entity) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Cохранение сущности |
||
| 161 | * @param EntityInterface $Entity |
||
| 162 | */ |
||
| 163 | public function save(EntityInterface $Entity) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Событие перед сохранением |
||
| 178 | * @param \SimpleORM\EntityInterface $Entity |
||
| 179 | */ |
||
| 180 | public function onAfterSave(EntityInterface $Entity){ |
||
| 198 | |||
| 199 | /** |
||
| 200 | * После успешного сохранения |
||
| 201 | * @param \SimpleORM\EntityInterface $Entity |
||
| 202 | */ |
||
| 203 | protected function onBeforeSave(EntityInterface $Entity){ |
||
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * получение мапперов в порядке их использования с учетом вложенности |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | protected function createListRelation(){ |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Выстроивает порядок использования мапперов в иерархии |
||
| 238 | * @param array $rel_map |
||
| 239 | * @param type $rel_list |
||
| 240 | */ |
||
| 241 | protected function createListRelationReq(array $rel_map,&$rel_list,$obj_parent_link = null) { |
||
| 256 | |||
| 257 | |||
| 258 | /** |
||
| 259 | * На успешное удаление |
||
| 260 | * @param \SimpleORM\EntityInterface $Entity |
||
| 261 | */ |
||
| 262 | protected function onBeforeDelete(EntityInterface $Entity) { |
||
| 276 | |||
| 277 | |||
| 278 | |||
| 279 | /** |
||
| 280 | * получить связи |
||
| 281 | */ |
||
| 282 | protected function getRelations(){ |
||
| 296 | |||
| 297 | |||
| 298 | |||
| 299 | |||
| 300 | |||
| 301 | |||
| 302 | |||
| 303 | /** |
||
| 304 | * Подготавливаем конечный вариант Сущности |
||
| 305 | * |
||
| 306 | * @param \Core\Infrastructure\EntityInterface $Entity |
||
| 307 | * @param array $row |
||
| 308 | * @return \Core\Infrastructure\EntityInterface |
||
| 309 | * @throws BadMethodCallException |
||
| 310 | */ |
||
| 311 | protected function buildEntity(EntityInterface $Entity, array $row){ |
||
| 354 | |||
| 355 | |||
| 356 | /** |
||
| 357 | * из объекта формирует массив |
||
| 358 | * @param \Core\Infrastructure\EntityInterface $Entity |
||
| 359 | * @return \Core\Infrastructure\EntityInterface |
||
| 360 | * @throws BadMethodCallException |
||
| 361 | */ |
||
| 362 | protected function unbuildEntity(EntityInterface $Entity){ |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Установка поля для маппинга |
||
| 403 | */ |
||
| 404 | 5 | protected function addMappingField($alias,$field = null){ |
|
| 427 | |||
| 428 | |||
| 429 | |||
| 430 | /** |
||
| 431 | * Установка ключа |
||
| 432 | */ |
||
| 433 | protected function getPrimaryKey() { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Устанвка поля для мягкого удаляения |
||
| 439 | */ |
||
| 440 | protected function setSoftDeleteKey() { |
||
| 443 | |||
| 444 | |||
| 445 | |||
| 446 | public function getFieldAlias($field){ |
||
| 451 | |||
| 452 | |||
| 453 | /** |
||
| 454 | * |
||
| 455 | * @param ISpecificationCriteria $specification |
||
| 456 | * @return type |
||
| 457 | */ |
||
| 458 | public function findBySpecification(ISpecificationCriteria $specification){ |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Удаление записи |
||
| 478 | * @param EntityInterface $Entity |
||
| 479 | * @return boolean |
||
| 480 | */ |
||
| 481 | public function delete(EntityInterface $Entity) |
||
| 501 | |||
| 502 | public function findAllBySpecification(ISpecificationCriteria $specification) |
||
| 524 | |||
| 525 | public function findAll() |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Выборка удаленных моделей |
||
| 532 | * @param ISpecificationCriteria $specification |
||
| 533 | */ |
||
| 534 | private function setSoftDelete(ISpecificationCriteria $specification){ |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Построение join-ов |
||
| 545 | * |
||
| 546 | * @todo добавить типы связей |
||
| 547 | * has_many - один к многим (пост и коммеентарии) |
||
| 548 | * belongs_to - многие к многим (пользователь имет множество оплат одного заказа) |
||
| 549 | * has_one - один к одному |
||
| 550 | */ |
||
| 551 | protected function setRelations(ISpecificationCriteria $Specification){ |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Использование join-ов |
||
| 583 | */ |
||
| 584 | public function useJoins() |
||
| 590 | |||
| 591 | public function withDelete(){ |
||
| 596 | |||
| 597 | } |
||
| 598 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.