Complex classes like Rest 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 Rest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class Rest extends AbstractController |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * @var string relation list |
||
| 33 | */ |
||
| 34 | protected $relation; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string relation Id |
||
| 38 | */ |
||
| 39 | protected $relationId; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array params of query |
||
| 43 | */ |
||
| 44 | protected $params = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array query data |
||
| 48 | */ |
||
| 49 | protected $data = array(); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Prepare request for processing |
||
| 53 | */ |
||
| 54 | 25 | public function __construct() |
|
| 55 | { |
||
| 56 | 25 | parent::__construct(); |
|
| 57 | |||
| 58 | // get path |
||
| 59 | // %module% / %controller% / %id% / %relation% / %id% |
||
| 60 | 25 | $path = Router::getCleanUri(); |
|
| 61 | |||
| 62 | 25 | $params = explode('/', rtrim($path, '/')); |
|
| 63 | |||
| 64 | // module |
||
| 65 | 25 | array_shift($params); |
|
| 66 | |||
| 67 | // controller |
||
| 68 | 25 | array_shift($params); |
|
| 69 | |||
| 70 | 25 | if (sizeof($params)) { |
|
| 71 | 13 | $this->primary = explode('-', array_shift($params)); |
|
| 72 | } |
||
| 73 | 25 | if (sizeof($params)) { |
|
| 74 | 2 | $this->relation = array_shift($params); |
|
| 75 | } |
||
| 76 | 25 | if (sizeof($params)) { |
|
| 77 | 1 | $this->relationId = array_shift($params); |
|
| 78 | } |
||
| 79 | 25 | } |
|
| 80 | |||
| 81 | /** |
||
| 82 | * {@inheritdoc} |
||
| 83 | * |
||
| 84 | * Everyone method can return: |
||
| 85 | * 401 Unauthorized - if authorization is required |
||
| 86 | * 403 Forbidden - if user don't have permissions |
||
| 87 | * 501 Not Implemented - if something not exists |
||
| 88 | * |
||
| 89 | * Methods can return: |
||
| 90 | * HEAD /module/rest/ -> 200 // return overview of collection |
||
| 91 | * HEAD /module/rest/id -> 200 // return overview of item |
||
| 92 | * -> 404 // not found |
||
| 93 | * GET /module/rest/ -> 200 // return collection or |
||
| 94 | * -> 206 // return part of collection |
||
| 95 | * GET /module/rest/id -> 200 // return one item or |
||
| 96 | * -> 404 // not found |
||
| 97 | * POST /module/rest/ -> 201 // item created or |
||
| 98 | * -> 400 // bad request, validation error |
||
| 99 | * POST /module/rest/id -> 501 // error, not used in REST |
||
| 100 | * PATCH /module/rest/ |
||
| 101 | * PUT /module/rest/ -> 200 // all items was updated or |
||
| 102 | * -> 207 // multi-status ? |
||
| 103 | * PATCH /module/rest/id |
||
| 104 | * PUT /module/rest/id -> 200 // item was updated or |
||
| 105 | * -> 304 // item not modified or |
||
| 106 | * -> 400 // bad request, validation error or |
||
| 107 | * -> 404 // not found |
||
| 108 | * DELETE /module/rest/ -> 204 // all items was deleted or |
||
| 109 | * -> 207 // multi-status ? |
||
| 110 | * DELETE /module/rest/id -> 204 // item was deleted |
||
| 111 | * -> 404 // not found |
||
| 112 | * |
||
| 113 | * @return mixed |
||
| 114 | * @throws NotImplementedException |
||
| 115 | * @throws NotFoundException |
||
| 116 | * @throws BadRequestException |
||
| 117 | */ |
||
| 118 | 25 | public function __invoke() |
|
| 119 | { |
||
| 120 | 25 | switch ($this->method) { |
|
| 121 | 25 | case Request::METHOD_HEAD: |
|
| 122 | 22 | case Request::METHOD_GET: |
|
| 123 | 8 | return $this->methodGet(); |
|
| 124 | // break |
||
| 125 | 17 | case Request::METHOD_POST: |
|
| 126 | 4 | return $this->methodPost(); |
|
| 127 | // break |
||
| 128 | 13 | case Request::METHOD_PATCH: |
|
| 129 | 13 | case Request::METHOD_PUT: |
|
| 130 | 6 | return $this->methodPut(); |
|
| 131 | // break |
||
| 132 | 7 | case Request::METHOD_DELETE: |
|
| 133 | 4 | return $this->methodDelete(); |
|
| 134 | // break |
||
| 135 | 3 | case Request::METHOD_OPTIONS: |
|
| 136 | 2 | return $this->methodOptions(); |
|
| 137 | // break |
||
| 138 | default: |
||
| 139 | 1 | throw new NotImplementedException(); |
|
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Method HEAD and GET |
||
| 145 | * |
||
| 146 | * @return mixed |
||
| 147 | */ |
||
| 148 | 8 | public function methodGet() |
|
| 149 | { |
||
| 150 | 8 | if (!empty($this->primary)) { |
|
| 151 | // @throws NotFoundException |
||
| 152 | 4 | $result = $this->readOne($this->primary); |
|
| 153 | 3 | return [$result]; |
|
| 154 | } else { |
||
| 155 | // setup default offset and limit - safe way |
||
| 156 | 4 | $offset = isset($this->params['offset'])?$this->params['offset']:0; |
|
| 157 | 4 | $limit = isset($this->params['limit'])?$this->params['limit']:10; |
|
| 158 | |||
| 159 | 4 | if ($range = Request::getHeader('Range')) { |
|
| 160 | 1 | list(, $offset, $last) = preg_split('/[-=]/', $range); |
|
| 161 | // for better compatibility |
||
| 162 | 1 | $limit = $last - $offset; |
|
| 163 | } |
||
| 164 | 4 | return $this->readSet($offset, $limit, $this->params); |
|
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Method POST |
||
| 170 | * |
||
| 171 | * @return array|false |
||
| 172 | * @throws BadRequestException |
||
| 173 | * @throws NotImplementedException |
||
| 174 | */ |
||
| 175 | 4 | public function methodPost() |
|
| 176 | { |
||
| 177 | 4 | if (!empty($this->primary)) { |
|
| 178 | // POST + ID is incorrect behaviour |
||
| 179 | 1 | throw new NotImplementedException(); |
|
| 180 | } |
||
| 181 | |||
| 182 | try { |
||
| 183 | 3 | $result = $this->createOne($this->data); |
|
| 184 | 1 | if (!$result) { |
|
| 185 | // system can't create record with this data |
||
| 186 | throw new BadRequestException(); |
||
| 187 | } |
||
| 188 | |||
| 189 | 1 | if (is_array($result)) { |
|
| 190 | 1 | $result = join('-', array_values($result)); |
|
| 191 | } |
||
| 192 | |||
| 193 | 2 | } catch (ValidatorException $e) { |
|
| 194 | 2 | Response::setStatusCode(400); |
|
| 195 | 2 | return ['errors' => $e->getErrors()]; |
|
| 196 | } |
||
| 197 | |||
| 198 | 1 | Response::setStatusCode(201); |
|
| 199 | 1 | Response::setHeader( |
|
| 200 | 1 | 'Location', |
|
| 201 | 1 | Router::getUrl(Request::getModule(), Request::getController()).'/'.$result |
|
| 202 | ); |
||
| 203 | 1 | return false; // disable view |
|
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Method PUT |
||
| 208 | * |
||
| 209 | * @return array|false |
||
| 210 | * @throws BadRequestException |
||
| 211 | */ |
||
| 212 | 6 | public function methodPut() |
|
| 213 | { |
||
| 214 | 6 | if (!sizeof($this->data)) { |
|
| 215 | // data not found |
||
| 216 | 1 | throw new BadRequestException(); |
|
| 217 | } |
||
| 218 | |||
| 219 | try { |
||
| 220 | 5 | if (!empty($this->primary)) { |
|
| 221 | // update one item |
||
| 222 | 4 | $result = $this->updateOne($this->primary, $this->data); |
|
| 223 | } else { |
||
| 224 | // update collection |
||
| 225 | 1 | $result = $this->updateSet($this->data); |
|
| 226 | } |
||
| 227 | // if $result === 0 it's means a update is not apply |
||
| 228 | // or records not found |
||
| 229 | 2 | if (0 === $result) { |
|
| 230 | 2 | Response::setStatusCode(304); |
|
| 231 | } |
||
| 232 | 3 | } catch (ValidatorException $e) { |
|
| 233 | 1 | Response::setStatusCode(400); |
|
| 234 | 1 | return ['errors' => $e->getErrors()]; |
|
| 235 | } |
||
| 236 | 2 | return false; // disable view |
|
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Method DELETE |
||
| 241 | * |
||
| 242 | * @return false |
||
| 243 | * @throws BadRequestException |
||
| 244 | */ |
||
| 245 | 4 | public function methodDelete() |
|
| 246 | { |
||
| 247 | 4 | if (!empty($this->primary)) { |
|
| 248 | // delete one |
||
| 249 | // @throws NotFoundException |
||
| 250 | 2 | $this->deleteOne($this->primary); |
|
| 251 | } else { |
||
| 252 | // delete collection |
||
| 253 | // @throws NotFoundException |
||
| 254 | 2 | if (!sizeof($this->data)) { |
|
| 255 | // data not exist |
||
| 256 | 1 | throw new BadRequestException(); |
|
| 257 | } |
||
| 258 | 1 | $this->deleteSet($this->data); |
|
| 259 | } |
||
| 260 | 1 | Response::setStatusCode(204); |
|
| 261 | 1 | return false; // disable view |
|
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Method OPTIONS |
||
| 266 | * |
||
| 267 | * @return false |
||
| 268 | */ |
||
| 269 | 2 | public function methodOptions() |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Get allowed methods by CRUD |
||
| 278 | * |
||
| 279 | * @param bool $primary |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | 2 | protected function getMethods($primary = false) |
|
| 317 | } |
||
| 318 |