Complex classes like AbstractEntryPoint 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 AbstractEntryPoint, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | abstract class AbstractEntryPoint implements EPInterface { |
||
22 | |||
23 | /** |
||
24 | * Whether or not Authentication is Required |
||
25 | * @var bool |
||
26 | */ |
||
27 | protected $_AUTH_REQUIRED = true; |
||
28 | |||
29 | /** |
||
30 | * The URL for the EntryPoint |
||
31 | * - When configuring URL you define URL Parameters with $variables |
||
32 | * Examples: |
||
33 | * - Forecasts/$record_id |
||
34 | * - $module Variable is a keyword to place the Module property into the URL |
||
35 | * Examples: |
||
36 | * - $module/$record |
||
37 | * - Options property is used to replace variables in the order in which they are passed |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $_URL; |
||
42 | |||
43 | /** |
||
44 | * An array of Required Data properties that should be passed in the Request |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $_REQUIRED_DATA = array(); |
||
48 | |||
49 | /** |
||
50 | * The required type of Data to be given to the EntryPoint. If none, different types can be passed in. |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $_DATA_TYPE; |
||
54 | |||
55 | /** |
||
56 | * The configured URL for the EntryPoint |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $Url; |
||
60 | |||
61 | /** |
||
62 | * The initial URL passed into the EntryPoint |
||
63 | * @var |
||
64 | */ |
||
65 | protected $baseUrl; |
||
66 | |||
67 | /** |
||
68 | * The passed in Options for the EntryPoint |
||
69 | * - If $module variable is used in $_URL static property, then 1st option will be used as Module |
||
70 | * @var array |
||
71 | */ |
||
72 | protected $Options = array(); |
||
73 | |||
74 | /** |
||
75 | * The data being passed to the API EntryPoint |
||
76 | * @var mixed - array||stdClass |
||
77 | */ |
||
78 | protected $Data; |
||
79 | |||
80 | /** |
||
81 | * The Request Object, used by the EntryPoint to submit the data |
||
82 | * @var RequestInterface |
||
83 | */ |
||
84 | protected $Request; |
||
85 | |||
86 | /** |
||
87 | * The Response Object, returned by the Request Object |
||
88 | * @var ResponseInterface |
||
89 | */ |
||
90 | protected $Response; |
||
91 | |||
92 | /** |
||
93 | * Access Token for authentication |
||
94 | * @var string |
||
95 | */ |
||
96 | protected $accessToken; |
||
97 | |||
98 | |||
99 | 1 | public function __construct($baseUrl,array $options = array()){ |
|
107 | |||
108 | /** |
||
109 | * @inheritdoc |
||
110 | */ |
||
111 | 1 | public function setOptions(array $options){ |
|
116 | |||
117 | /** |
||
118 | * @inheritdoc |
||
119 | * @throws RequiredDataException - When passed in data contains issues |
||
120 | */ |
||
121 | 1 | public function setData($data){ |
|
125 | |||
126 | /** |
||
127 | * @inheritdoc |
||
128 | */ |
||
129 | 1 | public function setAuth($accessToken) { |
|
133 | |||
134 | /** |
||
135 | * @inheritdoc |
||
136 | */ |
||
137 | 1 | public function setUrl($url) { |
|
141 | |||
142 | /** |
||
143 | * @inheritdoc |
||
144 | */ |
||
145 | 1 | public function setRequest(RequestInterface $Request) { |
|
149 | |||
150 | /** |
||
151 | * @inheritdoc |
||
152 | */ |
||
153 | 1 | public function setResponse(ResponseInterface $Response) { |
|
157 | |||
158 | /** |
||
159 | * @inheritdoc |
||
160 | */ |
||
161 | 1 | public function getOptions(){ |
|
164 | |||
165 | /** |
||
166 | * @inheritdoc |
||
167 | */ |
||
168 | 1 | public function getData(){ |
|
171 | |||
172 | /** |
||
173 | * @inheritdoc |
||
174 | */ |
||
175 | 1 | public function getUrl(){ |
|
178 | |||
179 | /** |
||
180 | * @inheritdoc |
||
181 | */ |
||
182 | 1 | public function getResponse(){ |
|
185 | |||
186 | /** |
||
187 | * @inheritdoc |
||
188 | */ |
||
189 | 1 | public function getRequest(){ |
|
192 | |||
193 | /** |
||
194 | * @inheritdoc |
||
195 | * @param null $data - short form data for EntryPoint, which is configure by configureData method |
||
196 | * @return $this |
||
197 | * @throws InvalidRequestException |
||
198 | * @throws InvalidURLException |
||
199 | */ |
||
200 | 2 | public function execute($data = NULL){ |
|
211 | |||
212 | /** |
||
213 | * @inheritdoc |
||
214 | */ |
||
215 | 1 | public function authRequired() { |
|
218 | |||
219 | /** |
||
220 | * Verifies URL and Data are setup, then sets them on the Request Object |
||
221 | * @throws InvalidURLException |
||
222 | * @throws RequiredDataException |
||
223 | */ |
||
224 | 1 | protected function configureRequest(){ |
|
233 | |||
234 | /** |
||
235 | * Configures the authentication header on the Request object |
||
236 | */ |
||
237 | 1 | protected function configureAuth(){ |
|
242 | |||
243 | /** |
||
244 | * Configures Data for the EntryPoint. Used mainly as an override function on implemented EntryPoints. |
||
245 | * @var $data |
||
246 | */ |
||
247 | 1 | protected function configureData($data){ |
|
253 | |||
254 | /** |
||
255 | * Configure Defaults on a Data array based on the Required Data property |
||
256 | * @param array $data |
||
257 | * @return array |
||
258 | */ |
||
259 | 1 | protected function configureDefaultData(array $data){ |
|
267 | |||
268 | /** |
||
269 | * Configures the URL, by updating any variable placeholders in the URL property on the EntryPoint |
||
270 | * - Replaces $module with $this->Module |
||
271 | * - Replaces all other variables starting with $, with options in the order they were given |
||
272 | */ |
||
273 | 1 | protected function configureURL(){ |
|
291 | |||
292 | /** |
||
293 | * Verify if URL is configured properly |
||
294 | * @return bool |
||
295 | * @throws InvalidURLException |
||
296 | */ |
||
297 | 2 | protected function verifyUrl(){ |
|
304 | |||
305 | /** |
||
306 | * Validate the Data property on the EntryPoint |
||
307 | * @return bool |
||
308 | * @throws RequiredDataException |
||
309 | */ |
||
310 | 1 | protected function verifyData(){ |
|
319 | |||
320 | /** |
||
321 | * Validate DataType on the EntryPoint object |
||
322 | * @return bool |
||
323 | * @throws RequiredDataException |
||
324 | */ |
||
325 | 2 | protected function verifyDataType(){ |
|
331 | |||
332 | /** |
||
333 | * Validate Required Data for the EntryPoint |
||
334 | * @return bool |
||
335 | * @throws RequiredDataException |
||
336 | */ |
||
337 | 2 | protected function verifyRequiredData(){ |
|
349 | |||
350 | /** |
||
351 | * Checks if EntryPoint URL contains requires Options |
||
352 | * @return bool |
||
353 | */ |
||
354 | 1 | protected function requiresOptions(){ |
|
357 | |||
358 | } |