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 | use TraitDataMapperEvent; |
||
| 14 | |||
| 15 | |||
| 16 | /** |
||
| 17 | * адаптер для работы с бд |
||
| 18 | * @var type |
||
| 19 | */ |
||
| 20 | protected $adapter; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * таблица для сущности |
||
| 24 | * @var type |
||
| 25 | */ |
||
| 26 | protected $entityTable; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * первичный ключ |
||
| 30 | * @var type |
||
| 31 | */ |
||
| 32 | protected $key; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Использование join при выборке |
||
| 36 | * @var type |
||
| 37 | */ |
||
| 38 | protected $use_joins = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Использование мягкое удаление |
||
| 42 | * @var type |
||
| 43 | */ |
||
| 44 | protected $use_delete = false; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * поле для мягкого удаления |
||
| 48 | * @var type |
||
| 49 | */ |
||
| 50 | protected $soft_delete_key; |
||
| 51 | |||
| 52 | |||
| 53 | /** |
||
| 54 | * поля сущности |
||
| 55 | * @var type |
||
| 56 | */ |
||
| 57 | protected $mapping_fields = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * псевдонимы полей сущности |
||
| 61 | * @var type |
||
| 62 | */ |
||
| 63 | protected $mapping_fields_aliases = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * связи с другими мапперами |
||
| 67 | * @var type |
||
| 68 | */ |
||
| 69 | protected $relations = []; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Контейнер |
||
| 73 | * @var League\Container\Container |
||
| 74 | */ |
||
| 75 | protected $DI; |
||
| 76 | |||
| 77 | function __construct(\League\Container\Container $DI, QueryBuilderInterface $adapter, $db_name = null) { |
||
| 92 | |||
| 93 | abstract protected function setMappingFields(); |
||
| 94 | |||
| 95 | public function getAdapter() { |
||
| 98 | |||
| 99 | public function setAdapter(QueryBuilderInterface $adapter){ |
||
| 102 | |||
| 103 | |||
| 104 | protected function getEntityTable() { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Уставнока таблицы |
||
| 110 | */ |
||
| 111 | protected function setEntityTable($db_name) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Получение записи по ключу |
||
| 117 | * @param type $id |
||
| 118 | * @return type |
||
| 119 | */ |
||
| 120 | public function findById($id) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Сохранение сущности без спобытий |
||
| 129 | * @param \SimpleORM\EntityInterface $Entity |
||
| 130 | */ |
||
| 131 | public function saveWithoutEvents(EntityInterface $Entity) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Cохранение сущности |
||
| 167 | * @param EntityInterface $Entity |
||
| 168 | */ |
||
| 169 | public function save(EntityInterface $Entity) |
||
| 181 | |||
| 182 | |||
| 183 | /** |
||
| 184 | * получение мапперов в порядке их использования с учетом вложенности |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | protected function createListRelation(){ |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Выстроивает порядок использования мапперов в иерархии |
||
| 199 | * @param array $rel_map |
||
| 200 | * @param type $rel_list |
||
| 201 | */ |
||
| 202 | protected function createListRelationReq(array $rel_map,&$rel_list,$obj_parent_link = null) { |
||
| 217 | |||
| 218 | |||
| 219 | /** |
||
| 220 | * получить связи |
||
| 221 | */ |
||
| 222 | protected function getRelations(){ |
||
| 236 | |||
| 237 | |||
| 238 | |||
| 239 | |||
| 240 | |||
| 241 | |||
| 242 | |||
| 243 | /** |
||
| 244 | * Подготавливаем конечный вариант Сущности |
||
| 245 | * |
||
| 246 | * @param \Core\Infrastructure\EntityInterface $Entity |
||
| 247 | * @param array $row |
||
| 248 | * @return \Core\Infrastructure\EntityInterface |
||
| 249 | * @throws BadMethodCallException |
||
| 250 | */ |
||
| 251 | protected function buildEntity(EntityInterface $Entity, array $row){ |
||
| 294 | |||
| 295 | |||
| 296 | /** |
||
| 297 | * из объекта формирует массив |
||
| 298 | * @param \Core\Infrastructure\EntityInterface $Entity |
||
| 299 | * @return \Core\Infrastructure\EntityInterface |
||
| 300 | * @throws BadMethodCallException |
||
| 301 | */ |
||
| 302 | protected function unbuildEntity(EntityInterface $Entity){ |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Установка поля для маппинга |
||
| 343 | */ |
||
| 344 | 5 | protected function addMappingField($alias,$field = null){ |
|
| 367 | |||
| 368 | |||
| 369 | |||
| 370 | /** |
||
| 371 | * Установка ключа |
||
| 372 | */ |
||
| 373 | protected function getPrimaryKey() { |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Устанвка поля для мягкого удаляения |
||
| 379 | */ |
||
| 380 | protected function setSoftDeleteKey() { |
||
| 383 | |||
| 384 | |||
| 385 | |||
| 386 | public function getFieldAlias($field){ |
||
| 391 | |||
| 392 | |||
| 393 | /** |
||
| 394 | * |
||
| 395 | * @param ISpecificationCriteria $specification |
||
| 396 | * @return type |
||
| 397 | */ |
||
| 398 | public function findBySpecification(ISpecificationCriteria $specification){ |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Удаление записи |
||
| 418 | * @param EntityInterface $Entity |
||
| 419 | * @return boolean |
||
| 420 | */ |
||
| 421 | public function delete(EntityInterface $Entity) |
||
| 441 | |||
| 442 | public function findAllBySpecification(ISpecificationCriteria $specification) |
||
| 464 | |||
| 465 | public function findAll() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Выборка удаленных моделей |
||
| 472 | * @param ISpecificationCriteria $specification |
||
| 473 | */ |
||
| 474 | private function setSoftDelete(ISpecificationCriteria $specification){ |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Построение join-ов |
||
| 485 | * |
||
| 486 | * @todo добавить типы связей |
||
| 487 | * has_many - один к многим (пост и коммеентарии) |
||
| 488 | * belongs_to - многие к многим (пользователь имет множество оплат одного заказа) |
||
| 489 | * has_one - один к одному |
||
| 490 | */ |
||
| 491 | protected function setRelations(ISpecificationCriteria $Specification){ |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Использование join-ов |
||
| 523 | */ |
||
| 524 | public function useJoins() |
||
| 530 | |||
| 531 | public function withDelete(){ |
||
| 536 | |||
| 537 | } |
||
| 538 |
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.