| Total Complexity | 92 |
| Total Lines | 592 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Client 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.
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 Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Client extends ClientAbstract implements HandleContracts |
||
| 19 | {
|
||
| 20 | /** |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | protected $capsule = []; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var null|string |
||
| 27 | */ |
||
| 28 | protected $clientName; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | protected $except = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var null|string |
||
| 37 | */ |
||
| 38 | protected $method; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var ReflectionProcess |
||
| 42 | */ |
||
| 43 | protected $reflection; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var null|ClientHttpManager |
||
| 47 | */ |
||
| 48 | protected $requestHttp; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var null|array |
||
| 52 | */ |
||
| 53 | protected $clientData; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | protected $requestData = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $generatorList = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Request constructor. |
||
| 67 | * |
||
| 68 | * @param null|array $clientData |
||
| 69 | * |
||
| 70 | * @throws ReflectionExceptionAlias |
||
| 71 | */ |
||
| 72 | public function __construct($clientData=null) |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * auto validate |
||
| 92 | * |
||
| 93 | * @param $validate |
||
| 94 | */ |
||
| 95 | private function autoValidate($validate) |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * capsule inputs |
||
| 128 | * |
||
| 129 | * @return void|mixed |
||
| 130 | */ |
||
| 131 | private function capsule() |
||
| 132 | {
|
||
| 133 | //a process can be added to the capsule array using the method. |
||
| 134 | if(method_exists($this,'capsuleMethod')){
|
||
| 135 | $this->capsule = array_merge($this->capsule,$this->capsuleMethod()); |
||
| 136 | } |
||
| 137 | |||
| 138 | // expected method is executed. |
||
| 139 | // this method is a must for http method values to be found in this property. |
||
| 140 | if($this->checkProperties('capsule')){
|
||
| 141 | |||
| 142 | if(property_exists($this,'auto_capsule') && is_array($this->auto_capsule)){
|
||
|
|
|||
| 143 | $this->capsule = array_merge($this->capsule,$this->auto_capsule); |
||
| 144 | } |
||
| 145 | |||
| 146 | if($this->checkProperties('groups')){
|
||
| 147 | $this->capsule = array_merge($this->capsule,$this->groups); |
||
| 148 | |||
| 149 | foreach ($this->capsule as $item) {
|
||
| 150 | $groupProcess = $this->groupsProcess($item,true); |
||
| 151 | if(is_array($groupProcess)){
|
||
| 152 | $this->inputs = array_merge($this->inputs,$groupProcess); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | foreach($this->inputs as $input=>$value){
|
||
| 158 | |||
| 159 | if($this->checkProperties('capsule') && !in_array($input,$this->capsule)){
|
||
| 160 | exception('clientCapsule',['key'=>$input])
|
||
| 161 | ->overflow('The '.$input.' value cannot be sent.');
|
||
| 162 | } |
||
| 163 | } |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * check http method |
||
| 169 | * |
||
| 170 | * @return void|mixed |
||
| 171 | */ |
||
| 172 | private function checkHttpMethod() |
||
| 173 | {
|
||
| 174 | //get http method |
||
| 175 | $method = $this->requestHttp->getMethod(); |
||
| 176 | |||
| 177 | // Determines which HTTP method |
||
| 178 | // the request object will be exposed to. |
||
| 179 | if($this->checkProperties('http')){
|
||
| 180 | |||
| 181 | // if the current http method does not exist |
||
| 182 | // in the http object, the exception will be thrown. |
||
| 183 | if(!in_array($method,$this->http)){
|
||
| 184 | |||
| 185 | //exception batMethodCall |
||
| 186 | exception()->badMethodCall( |
||
| 187 | 'Invalid http method process for '.basename($this).'.That is accepted http methods ['.implode(",",$this->http).'] ');
|
||
| 188 | } |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * check properties |
||
| 194 | * |
||
| 195 | * @param $properties |
||
| 196 | * @return bool |
||
| 197 | */ |
||
| 198 | private function checkProperties($properties) |
||
| 199 | {
|
||
| 200 | // from the properties of the object properties to |
||
| 201 | // the existing variables, control the array and at least one element. |
||
| 202 | return (property_exists($this,$properties) |
||
| 203 | && is_array($this->{$properties}) && count($this->{$properties})) ? true : false;
|
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * register container for request |
||
| 208 | * |
||
| 209 | * @return mixed|void |
||
| 210 | */ |
||
| 211 | private function containerRegister() |
||
| 212 | {
|
||
| 213 | // we are saving the expected values for the request in container. |
||
| 214 | // this record can be returned in exception information. |
||
| 215 | app()->register('requestExpected',$this->expected);
|
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * get request except |
||
| 220 | * |
||
| 221 | * @param $except |
||
| 222 | * @return $this |
||
| 223 | */ |
||
| 224 | public function except($except) |
||
| 225 | {
|
||
| 226 | // the except parameter is a callable value. |
||
| 227 | if(is_callable($except)){
|
||
| 228 | $call = call_user_func_array($except,[$this]); |
||
| 229 | $except = $call; |
||
| 230 | } |
||
| 231 | |||
| 232 | // except with the except exceptions property |
||
| 233 | // and then assigning them to the inputs property. |
||
| 234 | $this->except = array_merge($this->except,$except); |
||
| 235 | $this->inputs = array_diff_key($this->inputs,array_flip($this->except)); |
||
| 236 | |||
| 237 | return $this; |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * expected inputs |
||
| 242 | * |
||
| 243 | * @return void|mixed |
||
| 244 | */ |
||
| 245 | private function expectedInputs() |
||
| 246 | {
|
||
| 247 | // expected method is executed. |
||
| 248 | // this method is a must for http method values to be found in this property. |
||
| 249 | if($this->checkProperties('expected')){
|
||
| 250 | |||
| 251 | // if the expected values are not found in the inputs array, |
||
| 252 | // the exception will be thrown. |
||
| 253 | foreach ($this->expected as $expected){
|
||
| 254 | |||
| 255 | $expectedValues = []; |
||
| 256 | |||
| 257 | // mandatory expected data for each key can be separated by | operator. |
||
| 258 | // this is evaluated as "or". |
||
| 259 | foreach($expectedData = explode("|",$expected) as $inputs){
|
||
| 260 | |||
| 261 | // we should do key control for group format. |
||
| 262 | // this process will allow us to perform key control for 2D array correctly. |
||
| 263 | $this->groupsProcess($inputs); |
||
| 264 | |||
| 265 | if(!isset($this->inputs[$inputs])){
|
||
| 266 | $expectedValues[$inputs] = $inputs; |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | // if the expectedData and expectedValues |
||
| 271 | // array are numerically equal to the expected key, the exception is thrown. |
||
| 272 | if(count($expectedData)===count($expectedValues)){
|
||
| 273 | exception('clientExpected',['key'=>$expected])
|
||
| 274 | ->unexpectedValue('You absolutely have to send the value '.implode(" or ",$expectedValues).' for request object');
|
||
| 275 | } |
||
| 276 | } |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * generator manager |
||
| 282 | * |
||
| 283 | * @throws ReflectionExceptionAlias |
||
| 284 | */ |
||
| 285 | private function generatorManager() |
||
| 286 | {
|
||
| 287 | // check the presence of the generator object |
||
| 288 | // and operate the generator over this object. |
||
| 289 | if($this->checkProperties('auto_generators')){
|
||
| 290 | $generators = $this->getAutoGenerators(); |
||
| 291 | } |
||
| 292 | |||
| 293 | // check the presence of the generator object |
||
| 294 | // and operate the generator over this object. |
||
| 295 | if($this->checkProperties('generators')){
|
||
| 296 | $generators = array_merge(isset($generators) ? $generators: [],$this->getGenerators()); |
||
| 297 | } |
||
| 298 | |||
| 299 | if(isset($generators)){
|
||
| 300 | $this->generatorMethod($generators); |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * generator method |
||
| 306 | * |
||
| 307 | * @param $generators |
||
| 308 | * |
||
| 309 | * @throws ReflectionExceptionAlias |
||
| 310 | */ |
||
| 311 | private function generatorMethod($generators) |
||
| 353 | } |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * get client name for request |
||
| 359 | * |
||
| 360 | * @return string |
||
| 361 | */ |
||
| 362 | public function getClientName() |
||
| 363 | {
|
||
| 364 | return $this->clientName; |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * we should do key control for group format. |
||
| 369 | * this process will allow us to perform key control for 2D array correctly. |
||
| 370 | * |
||
| 371 | * @param null $key |
||
| 372 | * @param null $callback |
||
| 373 | * @return mixed|void |
||
| 374 | */ |
||
| 375 | public function groupsProcess($key=null,$callback=null) |
||
| 376 | {
|
||
| 377 | if(property_exists($this,'groups') && is_array($this->groups)){
|
||
| 378 | |||
| 379 | $clientObjects = $this->getClientObjects(); |
||
| 380 | |||
| 381 | foreach ($this->groups as $group){
|
||
| 382 | |||
| 383 | if(true === $callback){
|
||
| 384 | if(isset($clientObjects['origin'][$key])){
|
||
| 385 | return $clientObjects['origin'][$key]; |
||
| 386 | } |
||
| 387 | } |
||
| 388 | |||
| 389 | if(isset($clientObjects['origin'][$group][$key])){
|
||
| 390 | |||
| 391 | $this->{$key} = $clientObjects['origin'][$group][$key];
|
||
| 392 | $this->inputs[$key] = $this->{$key};
|
||
| 393 | |||
| 394 | if(is_callable($callback)){
|
||
| 395 | call_user_func_array($callback,[$key]); |
||
| 396 | } |
||
| 397 | } |
||
| 398 | } |
||
| 399 | } |
||
| 400 | |||
| 401 | return []; |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * request handle |
||
| 406 | * |
||
| 407 | * @return mixed|void |
||
| 408 | * |
||
| 409 | * @throws ReflectionExceptionAlias |
||
| 410 | */ |
||
| 411 | public function handle() |
||
| 412 | {
|
||
| 413 | //set container for request |
||
| 414 | $this->containerRegister(); |
||
| 415 | |||
| 416 | //we record the values |
||
| 417 | //that coming with the post. |
||
| 418 | $this->initClient(); |
||
| 419 | |||
| 420 | // this method determines |
||
| 421 | // how the request object will be requested, |
||
| 422 | $this->checkHttpMethod(); |
||
| 423 | |||
| 424 | // get capsule as mandatory values |
||
| 425 | $this->capsule(); |
||
| 426 | |||
| 427 | // if a fake method is defined and it is not in |
||
| 428 | // the context of any key method when access is granted, |
||
| 429 | // it can be filled with fake method. |
||
| 430 | $this->generatorManager(); |
||
| 431 | |||
| 432 | // contrary to capsule method, |
||
| 433 | // expected values must be in the key being sent. |
||
| 434 | $this->expectedInputs(); |
||
| 435 | |||
| 436 | // it passes all keys that are sent through |
||
| 437 | // a validation method on the user side. |
||
| 438 | $this->validation(); |
||
| 439 | |||
| 440 | // we update the input values after |
||
| 441 | // we receive and check the saved objects. |
||
| 442 | $this->setClientObjects(); |
||
| 443 | |||
| 444 | // the values specified in request except property |
||
| 445 | // are subtracted from all input values. |
||
| 446 | $this->requestExcept(); |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * get init client |
||
| 451 | * |
||
| 452 | * @return void |
||
| 453 | */ |
||
| 454 | private function initClient() |
||
| 455 | {
|
||
| 456 | // we use the http method to write |
||
| 457 | // the values to the inputs and origin properties. |
||
| 458 | foreach($this->clientData as $key=>$value){
|
||
| 459 | |||
| 460 | //inputs and origin properties |
||
| 461 | $this->inputs[$key] = $value; |
||
| 462 | $this->origin[$key] = $value; |
||
| 463 | } |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * the values specified in request except property |
||
| 468 | * are subtracted from all input values. |
||
| 469 | * |
||
| 470 | * @return mixed|void |
||
| 471 | */ |
||
| 472 | private function requestExcept() |
||
| 473 | {
|
||
| 474 | if(property_exists($this,'requestExcept') && is_array($this->requestExcept)){
|
||
| 475 | foreach ($this->requestExcept as $item){
|
||
| 476 | if(isset($this->inputs[$item])){
|
||
| 477 | unset($this->inputs[$item]); |
||
| 478 | } |
||
| 479 | } |
||
| 480 | } |
||
| 481 | } |
||
| 482 | |||
| 483 | /** |
||
| 484 | * set client name for client resolver |
||
| 485 | * |
||
| 486 | * @param null|string $clientName |
||
| 487 | * @return void|mixed |
||
| 488 | */ |
||
| 489 | public function setClientName($clientName=null) |
||
| 500 | } |
||
| 501 | } |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * set client objects |
||
| 506 | * |
||
| 507 | * @throws ReflectionExceptionAlias |
||
| 508 | */ |
||
| 509 | private function setClientObjects() |
||
| 510 | {
|
||
| 511 | $clientObjects = $this->getClientObjects(); |
||
| 512 | |||
| 513 | // we update the input values after |
||
| 514 | // we receive and check the saved objects. |
||
| 515 | foreach ($clientObjects as $key=>$value){
|
||
| 516 | |||
| 517 | // we should do key control for group format. |
||
| 518 | // this process will allow us to perform key control for 2D array correctly. |
||
| 519 | $this->groupsProcess($key,function($key){
|
||
| 520 | $this->registerRequestInputs($key); |
||
| 521 | unset($this->inputs[$key]); |
||
| 522 | }); |
||
| 523 | |||
| 524 | if(!in_array($key,$this->generatorList) && isset($clientObjects['origin'][$key])){
|
||
| 525 | |||
| 526 | $this->{$key} = $clientObjects['origin'][$key];
|
||
| 527 | $this->inputs[$key] = $this->{$key};
|
||
| 528 | |||
| 529 | // the request update to be performed using |
||
| 530 | // the method name to be used with the http method. |
||
| 531 | $this->registerRequestInputs($key); |
||
| 532 | } |
||
| 533 | } |
||
| 534 | |||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * register request inputs |
||
| 539 | * |
||
| 540 | * @param $key |
||
| 541 | * |
||
| 542 | * @throws ReflectionExceptionAlias |
||
| 543 | */ |
||
| 544 | private function registerRequestInputs($key) |
||
| 557 | } |
||
| 558 | |||
| 559 | /** |
||
| 560 | * set request inputs |
||
| 561 | * |
||
| 562 | * @param $method |
||
| 563 | * @param $key |
||
| 564 | * |
||
| 565 | * @throws ReflectionExceptionAlias |
||
| 566 | */ |
||
| 567 | private function setRequestInputs($method,$key) |
||
| 591 | } |
||
| 592 | |||
| 593 | } |
||
| 594 | } |
||
| 595 | } |
||
| 596 | |||
| 597 | /** |
||
| 598 | * validation for request |
||
| 599 | * |
||
| 600 | * @return void |
||
| 601 | */ |
||
| 602 | private function validation() |
||
| 612 | } |