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 | public function findById($id) | ||
| 118 | |||
| 119 | /** | ||
| 120 | * Cохранение сущности | ||
| 121 | * @param EntityInterface $Entity | ||
| 122 | */ | ||
| 123 | public function save(EntityInterface $Entity) | ||
| 161 | |||
| 162 | /** | ||
| 163 | * На успешное сохранение | ||
| 164 | * @param \SimpleORM\EntityInterface $Entity | ||
| 165 | */ | ||
| 166 | 	protected function onBeforeSave(EntityInterface $Entity){ | ||
| 181 | |||
| 182 | /** | ||
| 183 | * На успешное удаление | ||
| 184 | * @param \SimpleORM\EntityInterface $Entity | ||
| 185 | */ | ||
| 186 | 	protected function onBeforeDelete(EntityInterface $Entity) { | ||
| 200 | |||
| 201 | |||
| 202 | |||
| 203 | /** | ||
| 204 | * Подготавливаем конечный вариант Сущности | ||
| 205 | * | ||
| 206 | * @param \Core\Infrastructure\EntityInterface $Entity | ||
| 207 | * @param array $row | ||
| 208 | * @return \Core\Infrastructure\EntityInterface | ||
| 209 | * @throws BadMethodCallException | ||
| 210 | */ | ||
| 211 | 	protected function buildEntity(EntityInterface $Entity, array $row){ | ||
| 254 | |||
| 255 | |||
| 256 | /** | ||
| 257 | * из объекта формирует массив | ||
| 258 | * @param \Core\Infrastructure\EntityInterface $Entity | ||
| 259 | * @return \Core\Infrastructure\EntityInterface | ||
| 260 | * @throws BadMethodCallException | ||
| 261 | */ | ||
| 262 | 	protected function unbuildEntity(EntityInterface $Entity){ | ||
| 300 | |||
| 301 | /** | ||
| 302 | * Установка поля для маппинга | ||
| 303 | */ | ||
| 304 | 5 | 	protected function addMappingField($alias,$field = null){ | |
| 327 | |||
| 328 | |||
| 329 | |||
| 330 | /** | ||
| 331 | * Установка ключа | ||
| 332 | */ | ||
| 333 | 	protected function getPrimaryKey() { | ||
| 336 | |||
| 337 | /** | ||
| 338 | * Устанвка поля для мягкого удаляения | ||
| 339 | */ | ||
| 340 | 	protected function setSoftDeleteKey() { | ||
| 343 | |||
| 344 | |||
| 345 | |||
| 346 | 	public function getFieldAlias($field){ | ||
| 351 | |||
| 352 | |||
| 353 | /** | ||
| 354 | * | ||
| 355 | * @param ISpecificationCriteria $specification | ||
| 356 | * @return type | ||
| 357 | */ | ||
| 358 | 	public function findBySpecification(ISpecificationCriteria $specification){ | ||
| 375 | |||
| 376 | /** | ||
| 377 | * Удаление записи | ||
| 378 | * @param EntityInterface $Entity | ||
| 379 | * @return boolean | ||
| 380 | */ | ||
| 381 | public function delete(EntityInterface $Entity) | ||
| 401 | |||
| 402 | public function findAllBySpecification(ISpecificationCriteria $specification) | ||
| 424 | |||
| 425 | public function findAll() | ||
| 429 | |||
| 430 | /** | ||
| 431 | * Выборка удаленных моделей | ||
| 432 | * @param ISpecificationCriteria $specification | ||
| 433 | */ | ||
| 434 | 	private function setSoftDelete(ISpecificationCriteria $specification){ | ||
| 442 | |||
| 443 | /** | ||
| 444 | * Построение join-ов | ||
| 445 | * | ||
| 446 | * @todo добавить типы связей | ||
| 447 | * has_many - один к многим (пост и коммеентарии) | ||
| 448 | * belongs_to - многие к многим (пользователь имет множество оплат одного заказа) | ||
| 449 | * has_one - один к одному | ||
| 450 | */ | ||
| 451 | 	protected function setRelations(ISpecificationCriteria $Specification){ | ||
| 480 | |||
| 481 | /** | ||
| 482 | * Использование join-ов | ||
| 483 | */ | ||
| 484 | public function useJoins() | ||
| 490 | |||
| 491 | 	public function withDelete(){ | ||
| 496 | |||
| 497 | } | ||
| 498 | 
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.