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 | 2 | 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){ |
|
| 212 | |||
| 213 | /** |
||
| 214 | * @inheritdoc |
||
| 215 | */ |
||
| 216 | 1 | public function authRequired() { |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Verifies URL and Data are setup, then sets them on the Request Object |
||
| 222 | * @throws InvalidURLException |
||
| 223 | * @throws RequiredDataException |
||
| 224 | */ |
||
| 225 | 1 | protected function configureRequest(){ |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Verifies URL and Data are setup, then sets them on the Request Object |
||
| 237 | * @throws InvalidURLException |
||
| 238 | * @throws RequiredDataException |
||
| 239 | */ |
||
| 240 | 1 | protected function configureResponse(){ |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Configures the authentication header on the Request object |
||
| 248 | */ |
||
| 249 | 1 | protected function configureAuth(){ |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Configures Data for the EntryPoint. Used mainly as an override function on implemented EntryPoints. |
||
| 257 | * @var $data |
||
| 258 | */ |
||
| 259 | 1 | protected function configureData($data){ |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Configure Defaults on a Data array based on the Required Data property |
||
| 268 | * @param array $data |
||
| 269 | * @return array |
||
| 270 | */ |
||
| 271 | 1 | protected function configureDefaultData(array $data){ |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Configures the URL, by updating any variable placeholders in the URL property on the EntryPoint |
||
| 282 | * - Replaces $module with $this->Module |
||
| 283 | * - Replaces all other variables starting with $, with options in the order they were given |
||
| 284 | */ |
||
| 285 | 2 | protected function configureURL(){ |
|
| 295 | 2 | ||
| 296 | 2 | /** |
|
| 297 | 2 | * Verify if URL is configured properly |
|
| 298 | 2 | * @return bool |
|
| 299 | 2 | * @throws InvalidURLException |
|
| 300 | 2 | */ |
|
| 301 | 2 | protected function verifyUrl(){ |
|
| 308 | |||
| 309 | 2 | /** |
|
| 310 | 2 | * Validate the Data property on the EntryPoint |
|
| 311 | 2 | * @return bool |
|
| 312 | 1 | * @throws RequiredDataException |
|
| 313 | */ |
||
| 314 | 1 | protected function verifyData(){ |
|
| 323 | 1 | ||
| 324 | 1 | /** |
|
| 325 | 1 | * Validate DataType on the EntryPoint object |
|
| 326 | 1 | * @return bool |
|
| 327 | 1 | * @throws RequiredDataException |
|
| 328 | 1 | */ |
|
| 329 | 1 | protected function verifyDataType(){ |
|
| 335 | |||
| 336 | /** |
||
| 337 | 2 | * Validate Required Data for the EntryPoint |
|
| 338 | 2 | * @return bool |
|
| 339 | 1 | * @throws RequiredDataException |
|
| 340 | */ |
||
| 341 | 1 | protected function verifyRequiredData(){ |
|
| 353 | 1 | ||
| 354 | 1 | /** |
|
| 355 | 2 | * Checks if EntryPoint URL contains requires Options |
|
| 356 | 2 | * @return bool |
|
| 357 | 1 | */ |
|
| 358 | protected function requiresOptions(){ |
||
| 361 | |||
| 362 | } |