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 | public function __construct($baseUrl,array $options = array()){ |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @inheritdoc |
||
| 131 | */ |
||
| 132 | public function setOptions(array $options){ |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @inheritdoc |
||
| 140 | * @throws RequiredDataException - When passed in data contains issues |
||
| 141 | */ |
||
| 142 | public function setData($data){ |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @inheritdoc |
||
| 149 | */ |
||
| 150 | public function setAuth($accessToken) { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @inheritdoc |
||
| 157 | */ |
||
| 158 | public function setUrl($url) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @inheritdoc |
||
| 165 | */ |
||
| 166 | public function setRequest(RequestInterface $Request) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @inheritdoc |
||
| 173 | */ |
||
| 174 | public function setResponse(ResponseInterface $Response) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @inheritdoc |
||
| 181 | */ |
||
| 182 | public function getOptions(){ |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @inheritdoc |
||
| 188 | */ |
||
| 189 | public function getData(){ |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @inheritdoc |
||
| 195 | */ |
||
| 196 | public function getUrl(){ |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @inheritdoc |
||
| 202 | */ |
||
| 203 | public function getResponse(){ |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @inheritdoc |
||
| 209 | */ |
||
| 210 | public function getRequest(){ |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @inheritdoc |
||
| 216 | * @param null $data - short form data for EntryPoint, which is configure by configureData method |
||
| 217 | * @return $this |
||
| 218 | * @throws InvalidRequestException |
||
| 219 | * @throws InvalidURLException |
||
| 220 | */ |
||
| 221 | public function execute($data = NULL){ |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @inheritdoc |
||
| 235 | */ |
||
| 236 | public function authRequired() { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Verifies URL and Data are setup, then sets them on the Request Object |
||
| 242 | * @throws InvalidURLException |
||
| 243 | * @throws RequiredDataException |
||
| 244 | */ |
||
| 245 | protected function configureRequest(){ |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Configures the authentication header on the Request object |
||
| 257 | */ |
||
| 258 | protected function configureAuth(){ |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Configures Data for the EntryPoint. Used mainly as an override function on implemented EntryPoints. |
||
| 266 | * @var $data |
||
| 267 | */ |
||
| 268 | protected function configureData($data){ |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Configure Defaults on a Data array based on the Required Data property |
||
| 277 | * @param array $data |
||
| 278 | * @return array |
||
| 279 | */ |
||
| 280 | protected function configureDefaultData(array $data){ |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Configures the URL, by updating any variable placeholders in the URL property on the EntryPoint |
||
| 291 | * - Replaces $module with $this->Module |
||
| 292 | * - Replaces all other variables starting with $, with options in the order they were given |
||
| 293 | */ |
||
| 294 | protected function configureURL(){ |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Verify if URL is configured properly |
||
| 315 | * @return bool |
||
| 316 | * @throws InvalidURLException |
||
| 317 | */ |
||
| 318 | protected function verifyUrl(){ |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Verify URL variables have been removed, and that valid number of options were passed. |
||
| 328 | * @return bool |
||
| 329 | * @throws RequiredOptionsException |
||
| 330 | */ |
||
| 331 | protected function verifyOptions(){ |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Validate the Data property on the EntryPoint |
||
| 343 | * @return bool |
||
| 344 | * @throws RequiredDataException |
||
| 345 | */ |
||
| 346 | protected function verifyData(){ |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Validate DataType on the EntryPoint object |
||
| 358 | * @return bool |
||
| 359 | * @throws RequiredDataException |
||
| 360 | */ |
||
| 361 | protected function verifyDataType(){ |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Validate Required Data for the EntryPoint |
||
| 370 | * @return bool |
||
| 371 | * @throws RequiredDataException |
||
| 372 | */ |
||
| 373 | protected function verifyRequiredData(){ |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Checks if EntryPoint URL contains requires Options |
||
| 388 | * @return bool |
||
| 389 | */ |
||
| 390 | protected function requiresOptions(){ |
||
| 393 | |||
| 394 | } |