| Conditions | 15 |
| Paths | 34 |
| Total Lines | 89 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 44 | public function authorise() |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * Ignore if debug mode is initiated in Responsible API options |
||
| 48 | */ |
||
| 49 | if ($this->getRequestType() == 'debug') { |
||
| 50 | $this->grantAccess = true; |
||
| 51 | return true; |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Check if a custom scope is set |
||
| 56 | */ |
||
| 57 | if( isset($this->header->getMethod()->data['scope']) && |
||
| 58 | ($this->header->getMethod()->data['scope'] == 'anonymous') |
||
| 59 | ) { |
||
| 60 | $this->grantAccess = true; |
||
| 61 | return true; |
||
| 62 | } |
||
| 63 | |||
| 64 | if (isset($this->getOptions()['systemUser']) && !empty($this->getOptions()['systemUser'])) { |
||
| 65 | $this->header |
||
| 66 | ->setHeader('Authorization', array( |
||
| 67 | 'Bearer', $this->getOptions()['systemUser']['token'], |
||
| 68 | ), "", ""); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Scan for a header Authorization Bearer Json Web Token |
||
| 73 | * -- If not set header will return an unauthorised message |
||
| 74 | */ |
||
| 75 | $token = $this->header->authorizationHeaders(); |
||
| 76 | |||
| 77 | if (isset($token['client_access_request']) && !empty($token['client_access_request'])) { |
||
| 78 | $this->user = (object) $token['client_access_request']; |
||
| 79 | $this->grantAccess = true; |
||
| 80 | |||
| 81 | } else { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * [$jwt Decode the JWT] |
||
| 85 | * @var auth\jwt |
||
| 86 | */ |
||
| 87 | $jwt = new auth\jwt; |
||
| 88 | $decoded = $jwt |
||
| 89 | ->setOptions($this->getOptions()) |
||
| 90 | ->token($token) |
||
| 91 | ->key('payloadOnly') |
||
| 92 | ->decode() |
||
| 93 | ; |
||
| 94 | |||
| 95 | if( isset($decoded['sub']) && !empty($decoded['sub']) ) { |
||
| 96 | |||
| 97 | $this->user = (object) (new user\user) |
||
| 98 | ->setOptions($this->getOptions()) |
||
| 99 | ->load($decoded['sub'], ['refreshToken' => true]) |
||
| 100 | ; |
||
| 101 | |||
| 102 | if ( !empty($this->user) ) { |
||
| 103 | $jwt = new auth\jwt; |
||
| 104 | $decoded = $jwt |
||
| 105 | ->setOptions($this->getOptions()) |
||
| 106 | ->token($token) |
||
| 107 | ->key($this->user->secret) |
||
| 108 | ->decode() |
||
| 109 | ; |
||
| 110 | } |
||
| 111 | }else{ |
||
| 112 | |||
| 113 | $this->header->unauthorised(); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * [$user Check user account] |
||
| 119 | * @var [object] |
||
| 120 | */ |
||
| 121 | if ( (isset($decoded['sub']) && !empty($decoded['sub'])) && !$this->user ) { |
||
| 122 | $this->user = (object) (new user\user) |
||
| 123 | ->setOptions($this->getOptions()) |
||
| 124 | ->load($decoded['sub'], ['refreshToken' => true]) |
||
| 125 | ; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Account not found / doesn't exist |
||
| 130 | */ |
||
| 131 | if (empty($this->user)) { |
||
| 132 | $this->header->unauthorised(); |
||
| 133 | } |
||
| 200 |