| Conditions | 20 |
| Paths | 62 |
| Total Lines | 110 |
| Code Lines | 65 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 77 | public function create(array $data) |
||
| 78 | { |
||
| 79 | $validator = Validator::make($data, [ |
||
| 80 | 'memo' => 'string|max:500', |
||
| 81 | 'allowed_ips' => 'sometimes|string', |
||
| 82 | 'permissions' => 'sometimes|required|array', |
||
| 83 | 'admin_permissions' => 'sometimes|required|array', |
||
| 84 | ]); |
||
| 85 | |||
| 86 | $validator->after(function ($validator) use ($data) { |
||
| 87 | if (array_key_exists('allowed_ips', $data) && ! empty($data['allowed_ips'])) { |
||
| 88 | foreach (explode("\n", $data['allowed_ips']) as $ip) { |
||
| 89 | $ip = trim($ip); |
||
| 90 | try { |
||
| 91 | Network::parse($ip); |
||
| 92 | array_push($this->allowed, $ip); |
||
| 93 | } catch (\Exception $ex) { |
||
| 94 | $validator->errors()->add('allowed_ips', 'Could not parse IP <' . $ip . '> because it is in an invalid format.'); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | } |
||
| 98 | }); |
||
| 99 | |||
| 100 | // Run validator, throw catchable and displayable exception if it fails. |
||
| 101 | // Exception includes a JSON result of failed validation rules. |
||
| 102 | if ($validator->fails()) { |
||
| 103 | throw new DisplayValidationException(json_encode($validator->errors())); |
||
| 104 | } |
||
| 105 | |||
| 106 | DB::beginTransaction(); |
||
| 107 | try { |
||
| 108 | $secretKey = str_random(16) . '.' . str_random(7) . '.' . str_random(7); |
||
| 109 | $key = Key::create([ |
||
| 110 | 'user_id' => $this->user->id, |
||
| 111 | 'public' => str_random(16), |
||
| 112 | 'secret' => Crypt::encrypt($secretKey), |
||
| 113 | 'allowed_ips' => empty($this->allowed) ? null : json_encode($this->allowed), |
||
| 114 | 'memo' => $data['memo'], |
||
| 115 | 'expires_at' => null, |
||
| 116 | ]); |
||
| 117 | |||
| 118 | $totalPermissions = 0; |
||
| 119 | $pNodes = Permission::permissions(); |
||
| 120 | |||
| 121 | if (isset($data['permissions'])) { |
||
| 122 | foreach ($data['permissions'] as $permission) { |
||
| 123 | $parts = explode('-', $permission); |
||
| 124 | |||
| 125 | if (count($parts) !== 2) { |
||
| 126 | continue; |
||
| 127 | } |
||
| 128 | |||
| 129 | list($block, $search) = $parts; |
||
| 130 | |||
| 131 | if (! array_key_exists($block, $pNodes['_user'])) { |
||
| 132 | continue; |
||
| 133 | } |
||
| 134 | |||
| 135 | if (! in_array($search, $pNodes['_user'][$block])) { |
||
| 136 | continue; |
||
| 137 | } |
||
| 138 | |||
| 139 | $totalPermissions++; |
||
| 140 | Permission::create([ |
||
| 141 | 'key_id' => $key->id, |
||
| 142 | 'permission' => 'user.' . $permission, |
||
| 143 | ]); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | if ($this->user->isRootAdmin() && isset($data['admin_permissions'])) { |
||
| 148 | unset($pNodes['_user']); |
||
| 149 | |||
| 150 | foreach ($data['admin_permissions'] as $permission) { |
||
| 151 | $parts = explode('-', $permission); |
||
| 152 | |||
| 153 | if (count($parts) !== 2) { |
||
| 154 | continue; |
||
| 155 | } |
||
| 156 | |||
| 157 | list($block, $search) = $parts; |
||
| 158 | |||
| 159 | if (! array_key_exists($block, $pNodes)) { |
||
| 160 | continue; |
||
| 161 | } |
||
| 162 | |||
| 163 | if (! in_array($search, $pNodes[$block])) { |
||
| 164 | continue; |
||
| 165 | } |
||
| 166 | |||
| 167 | $totalPermissions++; |
||
| 168 | Permission::create([ |
||
| 169 | 'key_id' => $key->id, |
||
| 170 | 'permission' => $permission, |
||
| 171 | ]); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | if ($totalPermissions < 1) { |
||
| 176 | throw new DisplayException('No valid permissions were passed.'); |
||
| 177 | } |
||
| 178 | |||
| 179 | DB::commit(); |
||
| 180 | |||
| 181 | return $secretKey; |
||
| 182 | } catch (\Exception $ex) { |
||
| 183 | DB::rollBack(); |
||
| 184 | throw $ex; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 208 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.