Complex classes like Action 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 Action, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | abstract class Action implements EventListenerInterface, EventDispatcherInterface |
||
34 | { |
||
35 | use EventDispatcherTrait; |
||
36 | use InstanceConfigTrait; |
||
37 | use ValidatorAwareTrait; |
||
38 | |||
39 | /** |
||
40 | * Extensions to load and attach to listener |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | public $extensions = []; |
||
45 | |||
46 | /** |
||
47 | * An Auth instance. |
||
48 | * |
||
49 | * @var Auth |
||
50 | */ |
||
51 | public $Auth; |
||
52 | |||
53 | /** |
||
54 | * Default Action options. |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $_defaultConfig = []; |
||
59 | |||
60 | /** |
||
61 | * A Service reference. |
||
62 | * |
||
63 | * @var Service |
||
64 | */ |
||
65 | protected $_service; |
||
66 | |||
67 | /** |
||
68 | * Activated route. |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | protected $_route = null; |
||
73 | |||
74 | /** |
||
75 | * Extension registry. |
||
76 | * |
||
77 | * @var \CakeDC\Api\Service\Action\ExtensionRegistry |
||
78 | */ |
||
79 | protected $_extensions; |
||
80 | |||
81 | /** |
||
82 | * Action name. |
||
83 | * |
||
84 | * @var string |
||
85 | */ |
||
86 | protected $_name; |
||
87 | |||
88 | /** |
||
89 | * Action constructor. |
||
90 | * |
||
91 | * @param array $config Configuration options passed to the constructor |
||
92 | */ |
||
93 | 123 | public function __construct(array $config = []) |
|
94 | { |
||
95 | 123 | if (!empty($config['name'])) { |
|
96 | 81 | $this->setName($config['name']); |
|
97 | 81 | } |
|
98 | 123 | if (!empty($config['service'])) { |
|
99 | 123 | $this->setService($config['service']); |
|
100 | 123 | } |
|
101 | 123 | if (!empty($config['route'])) { |
|
102 | 64 | $this->setRoute($config['route']); |
|
103 | 64 | } |
|
104 | 123 | if (!empty($config['Extension'])) { |
|
105 | 49 | $this->extensions = (Hash::merge($this->extensions, $config['Extension'])); |
|
106 | 49 | } |
|
107 | 123 | $extensionRegistry = $eventManager = null; |
|
108 | 123 | if (!empty($config['eventManager'])) { |
|
109 | $eventManager = $config['eventManager']; |
||
110 | } |
||
111 | 123 | $this->_eventManager = $eventManager ?: new EventManager(); |
|
112 | 123 | $this->setConfig($config); |
|
113 | 123 | $this->initialize($config); |
|
114 | 123 | $this->_eventManager->on($this); |
|
115 | 123 | $this->setExtensions($extensionRegistry); |
|
116 | 123 | $this->_loadExtensions(); |
|
117 | 123 | } |
|
118 | |||
119 | /** |
||
120 | * Initialize an action instance |
||
121 | * |
||
122 | * @param array $config Configuration options passed to the constructor |
||
123 | * @return void |
||
124 | */ |
||
125 | 123 | public function initialize(array $config) |
|
126 | { |
||
127 | 123 | $this->Auth = $this->_initializeAuth(); |
|
128 | 123 | } |
|
129 | |||
130 | /** |
||
131 | * Gets an action name. |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | 79 | public function getName() |
|
136 | { |
||
137 | 79 | return $this->_name; |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * Sets an action name. |
||
142 | * |
||
143 | * @param string $name An action name. |
||
144 | * @return $this |
||
145 | */ |
||
146 | 81 | public function setName($name) |
|
147 | { |
||
148 | 81 | $this->_name = $name; |
|
149 | |||
150 | 81 | return $this; |
|
151 | } |
||
152 | |||
153 | /** |
||
154 | * Get and set service name. |
||
155 | * |
||
156 | * @param string $name Action name. |
||
157 | * @deprecated 3.4.0 Use setName()/getName() instead. |
||
158 | * @return string |
||
159 | */ |
||
160 | public function name($name = null) |
||
161 | { |
||
162 | deprecationWarning( |
||
163 | 'Action::name() is deprecated. ' . |
||
164 | 'Use Action::setName()/getName() instead.' |
||
165 | ); |
||
166 | |||
167 | if ($name !== null) { |
||
168 | return $this->setName($name); |
||
169 | } |
||
170 | |||
171 | return $this->getName(); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Returns activated route. |
||
176 | * |
||
177 | * @return array |
||
178 | */ |
||
179 | 13 | public function getRoute() |
|
180 | { |
||
181 | 13 | return $this->_route; |
|
182 | } |
||
183 | |||
184 | /** |
||
185 | * Sets activated route. |
||
186 | * |
||
187 | * @param array $route Route config. |
||
188 | * @return $this |
||
189 | */ |
||
190 | 64 | public function setRoute(array $route) |
|
191 | { |
||
192 | 64 | $this->_route = $route; |
|
193 | |||
194 | 64 | return $this; |
|
195 | } |
||
196 | |||
197 | /** |
||
198 | * Api method for activated route. |
||
199 | * |
||
200 | * @param array $route Activated route. |
||
201 | * @deprecated 3.4.0 Use setRoute()/getRoute() instead. |
||
202 | * @return array |
||
203 | */ |
||
204 | public function route($route = null) |
||
205 | { |
||
206 | deprecationWarning( |
||
207 | 'Action::route() is deprecated. ' . |
||
208 | 'Use Action::setRoute()/getRoute() instead.' |
||
209 | ); |
||
210 | |||
211 | if ($route !== null) { |
||
212 | return $this->setRoute($route); |
||
213 | } |
||
214 | |||
215 | return $this->getRoute(); |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @return Service |
||
220 | */ |
||
221 | 9 | public function getService() |
|
222 | { |
||
223 | 9 | return $this->_service; |
|
224 | } |
||
225 | |||
226 | /** |
||
227 | * Set a service |
||
228 | * |
||
229 | * @param Service $service service |
||
230 | */ |
||
231 | 123 | public function setService(Service $service) |
|
232 | { |
||
233 | 123 | $this->_service = $service; |
|
234 | 123 | } |
|
235 | |||
236 | /** |
||
237 | * Set or get service. |
||
238 | * |
||
239 | * @param Service $service An Service instance. |
||
240 | * @return Service |
||
241 | * @deprecated 3.4.0 Use setService()/getService() instead. |
||
242 | */ |
||
243 | public function service($service = null) |
||
244 | { |
||
245 | deprecationWarning( |
||
246 | 'Action::service() is deprecated. ' . |
||
247 | 'Use Action::setService()/getService() instead.' |
||
248 | ); |
||
249 | |||
250 | if ($service !== null) { |
||
251 | return $this->setService($service); |
||
252 | } |
||
253 | |||
254 | return $this->getService(); |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Apply validation process. |
||
259 | * |
||
260 | * @return bool |
||
261 | */ |
||
262 | 47 | public function validates() |
|
263 | { |
||
264 | 47 | return true; |
|
265 | } |
||
266 | |||
267 | /** |
||
268 | * Execute action. |
||
269 | * |
||
270 | * @return mixed |
||
271 | */ |
||
272 | abstract public function execute(); |
||
273 | |||
274 | /** |
||
275 | * Action execution life cycle. |
||
276 | * |
||
277 | * @return mixed |
||
278 | */ |
||
279 | 60 | public function process() |
|
280 | { |
||
281 | 60 | $event = $this->dispatchEvent('Action.beforeProcess', ['action' => $this]); |
|
282 | |||
283 | 60 | if ($event->isStopped()) { |
|
284 | return $event->result; |
||
285 | } |
||
321 | |||
322 | /** |
||
323 | * Execute action call to the method. |
||
324 | * |
||
325 | * This method pass action params as method params. |
||
326 | * |
||
327 | * @param string $methodName Method name. |
||
328 | * @return mixed |
||
329 | * @throws Exception |
||
330 | */ |
||
331 | 1 | protected function _executeAction($methodName = 'action') |
|
358 | |||
359 | /** |
||
360 | * @return array |
||
361 | */ |
||
362 | 123 | public function implementedEvents() |
|
381 | |||
382 | /** |
||
383 | * Returns action input params |
||
384 | * |
||
385 | * @return mixed |
||
386 | */ |
||
387 | 66 | public function getData() |
|
391 | |||
392 | /** |
||
393 | * Returns action input params |
||
394 | * |
||
395 | * @return mixed |
||
396 | * @deprecated 3.6.0 Use getData() instead. |
||
397 | */ |
||
398 | public function data() |
||
407 | |||
408 | /** |
||
409 | * @return \CakeDC\Api\Service\Action\ExtensionRegistry |
||
410 | */ |
||
411 | 49 | public function getExtensions() |
|
415 | |||
416 | /** |
||
417 | * Set a service |
||
418 | * |
||
419 | * @param \CakeDC\Api\Service\Action\ExtensionRegistry|null $extensions Extension registry. |
||
420 | */ |
||
421 | 123 | public function setExtensions($extensions = null) |
|
431 | |||
432 | /** |
||
433 | * Get the extension registry for this action. |
||
434 | * |
||
435 | * If called with the first parameter, it will be set as the action $this->_extensions property |
||
436 | * |
||
437 | * @param \CakeDC\Api\Service\Action\ExtensionRegistry|null $extensions Extension registry. |
||
438 | * |
||
439 | * @return \CakeDC\Api\Service\Action\ExtensionRegistry |
||
440 | * @deprecated 3.6.0 Use setExtensions()/getExtensions() instead. |
||
441 | */ |
||
442 | public function extensions($extensions = null) |
||
455 | |||
456 | /** |
||
457 | * Loads the defined extensions using the Extension factory. |
||
458 | * |
||
459 | * @return void |
||
460 | */ |
||
461 | protected function _loadExtensions() |
||
473 | 49 | ||
474 | 49 | /** |
|
475 | 49 | * Initialize auth. |
|
476 | 49 | * |
|
477 | * @return Auth |
||
478 | */ |
||
479 | protected function _initializeAuth() |
||
491 | |||
492 | /** |
||
493 | 123 | * Prepare Auth configuration. |
|
494 | * |
||
495 | * @return array |
||
496 | */ |
||
497 | protected function _authConfig() |
||
508 | } |
||
509 |