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 |
||
41 | abstract class AbstractEntryPoint implements EPInterface { |
||
42 | |||
43 | /** |
||
44 | * Whether or not Authentication is Required |
||
45 | * @var bool |
||
46 | */ |
||
47 | protected $_AUTH_REQUIRED = true; |
||
48 | |||
49 | /** |
||
50 | * The URL for the EntryPoint |
||
51 | * - When configuring URL you define URL Parameters with $variables |
||
52 | * Examples: |
||
53 | * - Forecasts/$record_id |
||
54 | * - $module Variable is a keyword to place the Module property into the URL |
||
55 | * Examples: |
||
56 | * - $module/$record |
||
57 | * - Options property is used to replace variables in the order in which they are passed |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $_URL; |
||
62 | |||
63 | /** |
||
64 | * An array of Required Data properties that should be passed in the Request |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $_REQUIRED_DATA = array(); |
||
68 | |||
69 | /** |
||
70 | * The required type of Data to be given to the EntryPoint. If none, different types can be passed in. |
||
71 | * @var string |
||
72 | */ |
||
73 | protected $_DATA_TYPE; |
||
74 | |||
75 | /** |
||
76 | * The configured URL for the EntryPoint |
||
77 | * @var string |
||
78 | */ |
||
79 | protected $Url; |
||
80 | |||
81 | /** |
||
82 | * The initial URL passed into the EntryPoint |
||
83 | * @var |
||
84 | */ |
||
85 | protected $baseUrl; |
||
86 | |||
87 | /** |
||
88 | * The passed in Options for the EntryPoint |
||
89 | * - If $module variable is used in $_URL static property, then 1st option will be used as Module |
||
90 | * @var array |
||
91 | */ |
||
92 | protected $Options = array(); |
||
93 | |||
94 | /** |
||
95 | * The data being passed to the API EntryPoint |
||
96 | * @var mixed - array||stdClass |
||
97 | */ |
||
98 | protected $Data; |
||
99 | |||
100 | /** |
||
101 | * The Request Object, used by the EntryPoint to submit the data |
||
102 | * @var RequestInterface |
||
103 | */ |
||
104 | protected $Request; |
||
105 | |||
106 | /** |
||
107 | * The Response Object, returned by the Request Object |
||
108 | * @var ResponseInterface |
||
109 | */ |
||
110 | protected $Response; |
||
111 | |||
112 | /** |
||
113 | * Access Token for authentication |
||
114 | * @var string |
||
115 | */ |
||
116 | protected $accessToken; |
||
117 | |||
118 | |||
119 | 1 | public function __construct($baseUrl,array $options = array()){ |
|
127 | |||
128 | /** |
||
129 | * @inheritdoc |
||
130 | */ |
||
131 | 1 | public function setOptions(array $options){ |
|
136 | |||
137 | /** |
||
138 | * @inheritdoc |
||
139 | * @throws RequiredDataException - When passed in data contains issues |
||
140 | */ |
||
141 | 1 | public function setData($data){ |
|
145 | |||
146 | /** |
||
147 | * @inheritdoc |
||
148 | */ |
||
149 | 1 | public function setAuth($accessToken) { |
|
153 | |||
154 | /** |
||
155 | * @inheritdoc |
||
156 | */ |
||
157 | public function setUrl($url) { |
||
161 | |||
162 | /** |
||
163 | * @inheritdoc |
||
164 | */ |
||
165 | 1 | public function setRequest(RequestInterface $Request) { |
|
169 | |||
170 | /** |
||
171 | * @inheritdoc |
||
172 | */ |
||
173 | public function setResponse(ResponseInterface $Response) { |
||
177 | |||
178 | /** |
||
179 | * @inheritdoc |
||
180 | */ |
||
181 | 1 | public function getOptions(){ |
|
184 | |||
185 | /** |
||
186 | * @inheritdoc |
||
187 | */ |
||
188 | 1 | public function getData(){ |
|
191 | |||
192 | /** |
||
193 | * @inheritdoc |
||
194 | */ |
||
195 | 1 | public function getUrl(){ |
|
198 | |||
199 | /** |
||
200 | * @inheritdoc |
||
201 | */ |
||
202 | public function getResponse(){ |
||
205 | |||
206 | /** |
||
207 | * @inheritdoc |
||
208 | */ |
||
209 | 1 | public function getRequest(){ |
|
212 | |||
213 | /** |
||
214 | * @inheritdoc |
||
215 | * @param null $data - short form data for EntryPoint, which is configure by configureData method |
||
216 | * @return $this |
||
217 | * @throws InvalidRequestException |
||
218 | * @throws InvalidURLException |
||
219 | */ |
||
220 | 2 | public function execute($data = NULL){ |
|
221 | 2 | $data = ($data === NULL?$this->Data:$data); |
|
222 | 2 | $this->configureData($data); |
|
223 | 2 | if (is_object($this->Request)) { |
|
224 | 1 | $this->configureRequest(); |
|
225 | 1 | $this->Request->send(); |
|
226 | 1 | }else{ |
|
227 | 1 | throw new InvalidRequestException(get_called_class(),"Request property not configured"); |
|
228 | } |
||
229 | 1 | return $this; |
|
230 | } |
||
231 | |||
232 | /** |
||
233 | * @inheritdoc |
||
234 | */ |
||
235 | 1 | public function authRequired() { |
|
238 | |||
239 | /** |
||
240 | * Verifies URL and Data are setup, then sets them on the Request Object |
||
241 | * @throws InvalidURLException |
||
242 | * @throws RequiredDataException |
||
243 | */ |
||
244 | 1 | protected function configureRequest(){ |
|
253 | |||
254 | /** |
||
255 | * Configures the authentication header on the Request object |
||
256 | */ |
||
257 | 1 | protected function configureAuth(){ |
|
262 | |||
263 | /** |
||
264 | * Configures Data for the EntryPoint. Used mainly as an override function on implemented EntryPoints. |
||
265 | * @var $data |
||
266 | */ |
||
267 | 1 | protected function configureData($data){ |
|
273 | |||
274 | /** |
||
275 | * Configure Defaults on a Data array based on the Required Data property |
||
276 | * @param array $data |
||
277 | * @return array |
||
278 | */ |
||
279 | 1 | protected function configureDefaultData(array $data){ |
|
287 | |||
288 | /** |
||
289 | * Configures the URL, by updating any variable placeholders in the URL property on the EntryPoint |
||
290 | * - Replaces $module with $this->Module |
||
291 | * - Replaces all other variables starting with $, with options in the order they were given |
||
292 | */ |
||
293 | 1 | protected function configureURL(){ |
|
311 | |||
312 | /** |
||
313 | * Verify if URL is configured properly |
||
314 | * @return bool |
||
315 | * @throws InvalidURLException |
||
316 | */ |
||
317 | 1 | protected function verifyUrl(){ |
|
324 | |||
325 | /** |
||
326 | * Validate the Data property on the EntryPoint |
||
327 | * @return bool |
||
328 | * @throws RequiredDataException |
||
329 | */ |
||
330 | 1 | protected function verifyData(){ |
|
339 | |||
340 | /** |
||
341 | * Validate DataType on the EntryPoint object |
||
342 | * @return bool |
||
343 | * @throws RequiredDataException |
||
344 | */ |
||
345 | 1 | protected function verifyDataType(){ |
|
351 | 1 | ||
352 | 1 | /** |
|
353 | * Validate Required Data for the EntryPoint |
||
354 | * @return bool |
||
355 | * @throws RequiredDataException |
||
356 | */ |
||
357 | protected function verifyRequiredData(){ |
||
369 | |||
370 | /** |
||
371 | * Checks if EntryPoint URL contains requires Options |
||
372 | 2 | * @return bool |
|
373 | 2 | */ |
|
374 | 2 | protected function requiresOptions(){ |
|
377 | |||
378 | } |