Example #1
0
 /**
  * A method for getting the proper HTTP status code for a successful request
  *
  * @param  string $method "PUT", "POST", "DELETE" or "GET"
  * @param  Model|null $model The model that a PUT request was executed against
  * @return int
  */
 public static function successfulHttpStatusCode($method, $model = null)
 {
     // if we did a put request, we need to ensure that the model wasn't
     // changed in other ways than those specified by the request
     //     Ref: http://jsonapi.org/format/#crud-updating-responses-200
     if (($method === 'PUT' || $method === 'PATCH') && $model instanceof Model) {
         // check if the model has been changed
         if ($model->isChanged()) {
             // return our response as if there was a GET request
             $method = 'GET';
         }
     }
     switch ($method) {
         case 'POST':
             return BaseResponse::HTTP_CREATED;
         case 'PUT':
         case 'PATCH':
         case 'DELETE':
             return BaseResponse::HTTP_NO_CONTENT;
         case 'GET':
             return BaseResponse::HTTP_OK;
     }
     // Code shouldn't reach this point, but if it does we assume that the
     // client has made a bad request, e.g. PATCH
     return BaseResponse::HTTP_BAD_REQUEST;
 }