Complex classes like AbstractAction 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 AbstractAction, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | abstract class AbstractAction implements ActionInterface |
||
31 | { |
||
32 | /** |
||
33 | * The service class this action is registered to |
||
34 | * @var Service $service |
||
35 | */ |
||
36 | protected $service; |
||
37 | |||
38 | /** |
||
39 | * additional key fields that are included in partial queries to make the DQL valid |
||
40 | * These columns should be purged from the result set |
||
41 | * @var array $addedKeyFields |
||
42 | */ |
||
43 | protected $addedKeyFields; |
||
44 | |||
45 | /** |
||
46 | * Set the service object |
||
47 | * @param Service $service |
||
48 | */ |
||
49 | 25 | public function setService(Service $service) |
|
50 | { |
||
51 | 25 | $this->service = $service; |
|
52 | 25 | } |
|
53 | |||
54 | /** |
||
55 | * Get the service class this action is registered against |
||
56 | * @return Service |
||
57 | */ |
||
58 | protected function getService() |
||
62 | |||
63 | /** |
||
64 | * get the matched route object |
||
65 | * @return RouteMetaData $route |
||
66 | */ |
||
67 | 25 | protected function getMatchedRoute() |
|
71 | |||
72 | /** |
||
73 | * Get the entity manager from the service object (uses the default manager) |
||
74 | * @return \Doctrine\ORM\EntityManager $em |
||
75 | */ |
||
76 | 25 | protected function getEntityManager() |
|
80 | |||
81 | /** |
||
82 | * Get the entity manager registry |
||
83 | * @return \Drest\EntityManagerRegistry |
||
84 | */ |
||
85 | 25 | protected function getEntityManagerRegistry() |
|
89 | |||
90 | /** |
||
91 | * Get the response object |
||
92 | * @return Response $response |
||
93 | */ |
||
94 | 6 | protected function getResponse() |
|
98 | |||
99 | /** |
||
100 | * Get the request object |
||
101 | * @return Request $request |
||
102 | */ |
||
103 | 3 | protected function getRequest() |
|
107 | |||
108 | /** |
||
109 | * Get the predetermined representation |
||
110 | * @return AbstractRepresentation |
||
111 | */ |
||
112 | 3 | public function getRepresentation() |
|
116 | |||
117 | /** |
||
118 | * Handle an error - set the resulting error document to the response object |
||
119 | * @param \Exception $e |
||
120 | * @param integer $defaultResponseCode the default response code to use if no match on exception type occurs |
||
121 | * @param ResponseInterface $errorDocument |
||
122 | * @return ResultSet the error result set |
||
123 | */ |
||
124 | 5 | public function handleError(\Exception $e, $defaultResponseCode = 500, ResponseInterface $errorDocument = null) |
|
128 | |||
129 | /** |
||
130 | * Register the expose details from given metadata, typically used when setting up GET single/collection endpoints |
||
131 | * @param ORM\QueryBuilder $qb |
||
132 | * @param ORM\Mapping\ClassMetadata $classMetaData |
||
133 | */ |
||
134 | 17 | protected function registerExposeFromMetaData(ORM\QueryBuilder $qb, ORM\Mapping\ClassMetadata $classMetaData) |
|
142 | |||
143 | /** |
||
144 | * A recursive function to process the specified expose fields for a fetch request (GET) |
||
145 | * @param array $fields - expose fields to process |
||
146 | * @param ORM\QueryBuilder $qb |
||
147 | * @param ORM\Mapping\ClassMetadata $classMetaData |
||
148 | * @param $rootAlias - table alias to be used on SQL query |
||
149 | * @param array $addedKeyFields |
||
150 | * @return ORM\QueryBuilder |
||
151 | */ |
||
152 | 17 | protected function registerExpose( |
|
207 | |||
208 | /** |
||
209 | * Get filtered associations |
||
210 | * @param array $fields |
||
211 | * @param ORM\Mapping\ClassMetadata $classMetaData |
||
212 | * @param string $type 'fields' or 'association |
||
213 | * @return array |
||
214 | */ |
||
215 | 17 | protected function getFilteredAssociations(array $fields, ORM\Mapping\ClassMetadata $classMetaData, $type = 'fields') |
|
234 | |||
235 | /** |
||
236 | * Method used to write to the $data array. |
||
237 | * - wraps results in a single entry array keyed by entity name. |
||
238 | * Eg array(user1, user2) becomes array('users' => array(user1, user2)) - this is useful for a more descriptive output of collection resources |
||
239 | * - Removes any addition expose fields required for a partial DQL query |
||
240 | * @param array $data - the data fetched from the database |
||
241 | * @param string $keyName - the key name to use to wrap the data in. If null will attempt to pluralise the entity name on collection request, or singularize on single element request |
||
242 | * @return ResultSet $data |
||
243 | */ |
||
244 | 14 | public function createResultSet(array $data, $keyName = null) |
|
273 | |||
274 | |||
275 | /** |
||
276 | * Functional recursive method to remove any fields added to make the partial DQL work and remove the data |
||
277 | * @param array $addedKeyFields |
||
278 | * @param array $data - pass by reference |
||
279 | * @return array |
||
280 | */ |
||
281 | 2 | protected function removeAddedKeyFields($addedKeyFields, &$data) |
|
306 | |||
307 | |||
308 | /** |
||
309 | * Perform a default update action. Logic between PUT and PATCH (not POST) are identical |
||
310 | * If things change then we can start to break this function down |
||
311 | * @return ResultSet |
||
312 | */ |
||
313 | 2 | protected function performDefaultUpdateAction() |
|
353 | |||
354 | |||
355 | /** |
||
356 | * Run the handle call on an entity object |
||
357 | * @param object $object |
||
358 | */ |
||
359 | 3 | protected function runHandle($object) |
|
368 | |||
369 | /** |
||
370 | * Get a unique alias name from an entity class name and relation field |
||
371 | * @param string $className - The class of the related entity |
||
372 | * @param string $fieldName - The field the relation is on. Typical to root when using top level. |
||
373 | * @return string |
||
374 | */ |
||
375 | 24 | public static function getAlias($className, $fieldName = 'rt') |
|
391 | } |
||
392 |