| Conditions | 17 |
| Paths | 50 |
| Total Lines | 97 |
| Code Lines | 49 |
| 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 |
||
| 46 | public function authorise() |
||
| 47 | { |
||
| 48 | /** |
||
| 49 | * Ignore if debug mode is initiated in Responsible API options |
||
| 50 | */ |
||
| 51 | if ($this->getRequestType() == 'debug') { |
||
| 52 | $this->grantAccess = true; |
||
| 53 | return true; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Check if a custom scope is set |
||
| 58 | */ |
||
| 59 | if ( |
||
| 60 | isset($this->header->getMethod()->data['scope']) && |
||
| 61 | ($this->header->getMethod()->data['scope'] == 'anonymous') |
||
| 62 | ) { |
||
| 63 | $this->grantAccess = true; |
||
| 64 | return true; |
||
| 65 | } |
||
| 66 | |||
| 67 | if (isset($this->getOptions()['systemUser']) && !empty($this->getOptions()['systemUser'])) { |
||
| 68 | $this->header |
||
| 69 | ->setHeader('Authorization', array( |
||
| 70 | 'Bearer', $this->getOptions()['systemUser']['token'], |
||
| 71 | ), "", ""); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Scan for a header Authorization Bearer Json Web Token |
||
| 76 | * -- If not set header will return an unauthorised message |
||
| 77 | */ |
||
| 78 | $token = $this->header->authorizationHeaders(); |
||
| 79 | |||
| 80 | if (isset($token['client_access_request']) && !empty($token['client_access_request'])) { |
||
| 81 | $this->user = (object) $token['client_access_request']; |
||
| 82 | $this->grantAccess = true; |
||
| 83 | } else { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * [$jwt Decode the JWT] |
||
| 87 | * @var auth\jwt |
||
| 88 | */ |
||
| 89 | $jwt = new auth\jwt(); |
||
| 90 | $decoded = $jwt |
||
| 91 | ->setOptions($this->getOptions()) |
||
| 92 | ->token($token) |
||
| 93 | ->key('payloadOnly') |
||
| 94 | ->decode() |
||
| 95 | ; |
||
| 96 | |||
| 97 | if (isset($decoded['sub']) && !empty($decoded['sub'])) { |
||
| 98 | $this->user = (object) (new user\user()) |
||
| 99 | ->setOptions($this->getOptions()) |
||
| 100 | ->load($decoded['sub'], ['refreshToken' => true]) |
||
| 101 | ; |
||
| 102 | |||
| 103 | $secretKey = $this->user->secret; |
||
| 104 | if ( |
||
| 105 | isset($this->getOptions()['jwt']['signWith']) && |
||
| 106 | !empty($this->getOptions()['jwt']['signWith']) |
||
| 107 | ) { |
||
| 108 | $secretKey = $this->getOptions()['jwt']['signWith']; |
||
| 109 | } |
||
| 110 | |||
| 111 | if (!empty($this->user)) { |
||
| 112 | $jwt = new auth\jwt(); |
||
| 113 | $decoded = $jwt |
||
| 114 | ->setOptions($this->getOptions()) |
||
| 115 | ->token($token) |
||
| 116 | ->key($secretKey) |
||
| 117 | ->decode() |
||
| 118 | ; |
||
| 119 | } |
||
| 120 | } else { |
||
| 121 | $this->header->unauthorised(); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * [$user Check user account] |
||
| 127 | * @var [object] |
||
| 128 | */ |
||
| 129 | if ((isset($decoded['sub']) && !empty($decoded['sub'])) && !$this->user) { |
||
| 130 | $this->user = (object) (new user\user()) |
||
| 131 | ->setOptions($this->getOptions()) |
||
| 132 | ->load($decoded['sub'], ['refreshToken' => true]) |
||
| 133 | ; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Account not found / doesn't exist |
||
| 138 | */ |
||
| 139 | if (empty($this->user)) { |
||
| 140 | $this->header->unauthorised(); |
||
| 141 | } |
||
| 142 | return false; |
||
| 143 | } |
||
| 210 |