Complex classes like ApiPersister 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 ApiPersister, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class ApiPersister implements EntityPersister |
||
15 | { |
||
16 | /** @var SearchArgumentsTransformer */ |
||
17 | private $transformer; |
||
18 | /** @var EntityMetadata */ |
||
19 | private $metadata; |
||
20 | /** @var ApiEntityManager */ |
||
21 | private $manager; |
||
22 | /** @var CrudsApiInterface */ |
||
23 | private $api; |
||
24 | /** @var array */ |
||
25 | private $pendingInserts = []; |
||
26 | |||
27 | /** |
||
28 | * ApiPersister constructor. |
||
29 | * |
||
30 | * @param ApiEntityManager $manager |
||
31 | * @param CrudsApiInterface $api |
||
32 | */ |
||
33 | 17 | public function __construct(ApiEntityManager $manager, CrudsApiInterface $api) |
|
40 | |||
41 | /** {@inheritdoc} */ |
||
42 | public function getClassMetadata() |
||
46 | |||
47 | /** {@inheritdoc} */ |
||
48 | 4 | public function getCrudsApi() |
|
52 | |||
53 | /** {@inheritdoc} */ |
||
54 | 1 | public function update($entity) |
|
65 | |||
66 | /** {@inheritdoc} */ |
||
67 | 1 | public function delete($entity) |
|
71 | |||
72 | /** {@inheritdoc} */ |
||
73 | 1 | public function count($criteria = []) |
|
77 | |||
78 | /** {@inheritdoc} */ |
||
79 | 2 | public function loadAll(array $criteria = [], array $orderBy = null, $limit = null, $offset = null) |
|
95 | |||
96 | /** {@inheritdoc} */ |
||
97 | public function loadOneToOneEntity(array $assoc, $sourceEntity, array $identifier = []) |
||
110 | |||
111 | /** {@inheritdoc} */ |
||
112 | 11 | public function loadById(array $identifiers, $entity = null) |
|
122 | |||
123 | /** {@inheritdoc} */ |
||
124 | public function refresh(array $id, $entity) |
||
128 | |||
129 | /** {@inheritdoc} */ |
||
130 | 2 | public function loadOneToManyCollection(array $assoc, $sourceEntity, AbstractLazyCollection $collection) |
|
159 | |||
160 | /** {@inheritdoc} */ |
||
161 | 9 | public function getOneToManyCollection(array $assoc, $sourceEntity, $limit = null, $offset = null) |
|
177 | |||
178 | /** {@inheritdoc} */ |
||
179 | 4 | public function getToOneEntity(array $mapping, $sourceEntity, array $identifiers) |
|
180 | { |
||
181 | 4 | $metadata = $this->manager->getClassMetadata(get_class($sourceEntity)); |
|
182 | |||
183 | 4 | if (!$mapping['isOwningSide']) { |
|
184 | $identifiers = $metadata->getIdentifierValues($sourceEntity); |
||
185 | 1 | } |
|
186 | |||
187 | 4 | return $this->manager->getReference($mapping['target'], $identifiers); |
|
188 | } |
||
189 | |||
190 | 4 | public function pushNewEntity($entity) |
|
194 | |||
195 | 4 | public function flushNewEntities() |
|
215 | |||
216 | /** |
||
217 | * Prepares the changeset of an entity for database insertion (UPDATE). |
||
218 | * |
||
219 | * The changeset is obtained from the currently running UnitOfWork. |
||
220 | * |
||
221 | * During this preparation the array that is passed as the second parameter is filled with |
||
222 | * <columnName> => <value> pairs, grouped by table name. |
||
223 | * |
||
224 | * Example: |
||
225 | * <code> |
||
226 | * array( |
||
227 | * 'foo_table' => array('column1' => 'value1', 'column2' => 'value2', ...), |
||
228 | * 'bar_table' => array('columnX' => 'valueX', 'columnY' => 'valueY', ...), |
||
229 | * ... |
||
230 | * ) |
||
231 | * </code> |
||
232 | * |
||
233 | * @param object $entity The entity for which to prepare the data. |
||
234 | * |
||
235 | * @return array The prepared data. |
||
236 | */ |
||
237 | 1 | protected function prepareUpdateData($entity) |
|
276 | |||
277 | 4 | private function convertEntityToData($entity) |
|
295 | } |
||
296 |