Skip to content

Commit

Permalink
Refactored ApiResponse to use method chaining, and added transformer …
Browse files Browse the repository at this point in the history
…as a chainable method.
  • Loading branch information
zakhenry committed Aug 3, 2015
1 parent e90d7f6 commit 27febf0
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 98 deletions.
33 changes: 25 additions & 8 deletions api/app/Http/Controllers/ApiController.php
Expand Up @@ -45,7 +45,9 @@ abstract class ApiController extends Controller
*/
public function getAll()
{
return $this->getResponse()->collection($this->getRepository()->all(), $this->transformer);
return $this->getResponse()
->transformer($this->transformer)
->collection($this->getRepository()->all());
}

public function getAllPaginated(PaginatedRequestDecoratorInterface $request)
Expand All @@ -54,7 +56,10 @@ public function getAllPaginated(PaginatedRequestDecoratorInterface $request)
$limit = $request->getLimit($this->paginatorDefaultLimit, $this->paginatorMaxLimit);
$offset = $request->isGetLast()?$count-$limit:$request->getOffset();
$collection = $this->getRepository()->all(['*'], $offset, $limit);
return $this->getResponse()->paginatedCollection($collection, $this->transformer, $offset, $count);

return $this->getResponse()
->transformer($this->transformer)
->paginatedCollection($collection, $offset, $count);
}

/**
Expand All @@ -66,14 +71,20 @@ public function getAllPaginated(PaginatedRequestDecoratorInterface $request)
public function getOne($id)
{
$this->validateId($id);
$model = null;

try {
$model = $this->getRepository()->find($id);
return $this->getResponse()->item($model, $this->transformer);

} catch (ModelNotFoundException $e) {
$this->notFound($this->getKeyName());
$this->notFound();
}

return $this->getResponse()->noContent();
return $this->getResponse()
->transformer($this->transformer)
->item($model)
;

}

/**
Expand All @@ -88,7 +99,9 @@ public function postOne(Request $request)
$model->fill($request->all());
$this->getRepository()->save($model);

return $this->getResponse()->createdItem($model, $this->transformer);
return $this->getResponse()
->transformer($this->transformer)
->createdItem($model);
}

/**
Expand All @@ -109,7 +122,9 @@ public function putOne($id, Request $request)
$model->fill($request->all());
$this->getRepository()->save($model);

return $this->getResponse()->createdItem($model, $this->transformer);
return $this->getResponse()
->transformer($this->transformer)
->createdItem($model);
}

/**
Expand Down Expand Up @@ -143,7 +158,9 @@ public function putMany(Request $request)

$models = $this->getRepository()->saveMany($putModels);

return $this->getResponse()->createdCollection($models, $this->transformer);
return $this->getResponse()
->transformer($this->transformer)
->createdCollection($models);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion api/app/Http/Controllers/ArticleController.php
Expand Up @@ -29,6 +29,8 @@ public function getPermalinks($id)
{
/** @var Article $article */
$article = $this->repository->find($id);
return $this->getResponse()->collection($article->permalinks, $this->transformer);
return $this->getResponse()
->transformer($this->transformer)
->collection($article->permalinks);
}
}
12 changes: 9 additions & 3 deletions api/app/Http/Controllers/AuthController.php
Expand Up @@ -67,7 +67,9 @@ public function login(Request $request)
throw new RuntimeException($e->getMessage(), 500, $e);
}

return $this->getResponse()->item($token, $this->transformer);
return $this->getResponse()
->transformer($this->transformer)
->item($token);
}

/**
Expand All @@ -86,7 +88,9 @@ public function refresh(Request $request)

$token = $this->jwtAuth->refresh($token);

return $this->getResponse()->item($token, $this->transformer);
return $this->getResponse()
->transformer($this->transformer)
->item($token);
}

/**
Expand All @@ -113,6 +117,8 @@ public function token(Request $request, UserRepository $userRepository)

$token = $this->jwtAuth->fromUser($user);

return $this->getResponse()->item($token, $this->transformer);
return $this->getResponse()
->transformer($this->transformer)
->item($token);
}
}
4 changes: 3 additions & 1 deletion api/app/Http/Controllers/CountriesController.php
Expand Up @@ -25,6 +25,8 @@ public function __construct(Countries $countries, EloquentModelTransformer $tran
*/
public function getAll()
{
return $this->getResponse()->collection($this->countries->all(), $this->transformer);
return $this->getResponse()
->transformer($this->transformer)
->collection($this->countries->all());
}
}
4 changes: 3 additions & 1 deletion api/app/Http/Controllers/TimezoneController.php
Expand Up @@ -24,6 +24,8 @@ public function __construct(Timezones $timezones, EloquentModelTransformer $tran
*/
public function getAll()
{
return $this->getResponse()->collection($this->timezones->all(), $this->transformer);
return $this->getResponse()
->transformer($this->transformer)
->collection($this->timezones->all());
}
}
4 changes: 3 additions & 1 deletion api/app/Http/Controllers/UserController.php
Expand Up @@ -121,7 +121,9 @@ public function putOne($id, Request $request)
// Finally create the credentials
$model->setCredential(new UserCredential($credential));

return $this->getResponse()->createdItem($model, $this->transformer);
return $this->getResponse()
->transformer($this->transformer)
->createdItem($model, $this->transformer);
}

/**
Expand Down
1 change: 0 additions & 1 deletion api/app/Http/Transformers/AuthTokenTransformer.php
Expand Up @@ -5,7 +5,6 @@
use App;
use Tymon\JWTAuth\Token;
use App\Exceptions\NotImplementedException;
use Spira\Responder\Contract\TransformerInterface;

class AuthTokenTransformer extends EloquentModelTransformer
{
Expand Down
153 changes: 71 additions & 82 deletions api/src/Responder/Response/ApiResponse.php
Expand Up @@ -9,27 +9,42 @@
namespace Spira\Responder\Response;

use Illuminate\Http\Response;
use Illuminate\Support\Collection;
use Spira\Responder\Contract\TransformerInterface;
use Symfony\Component\HttpKernel\Exception\HttpException;

class ApiResponse extends Response
{

/** @var TransformerInterface */
protected $transformer = null;

/**
* Set the transformer to use for building entities
* @param TransformerInterface $transformer
* @return $this
*/
public function transformer(TransformerInterface $transformer)
{
$this->transformer = $transformer;
return $this;
}

/**
* Respond with a created response and associate a location if provided.
*
* @param null|string $location
*
* @param null $location
* @return Response
*/
public function created($location = null)
{
$this->setContent(null);
$this->setStatusCode(self::HTTP_CREATED);

if (! is_null($location)) {
$this->headers->set('Location', $location);
$this->header('Location', $location);
}
return $this;

return $this
->setContent(null)
->setStatusCode(self::HTTP_CREATED)
;
}


Expand All @@ -41,126 +56,100 @@ public function created($location = null)
*/
public function noContent($code = self::HTTP_NO_CONTENT)
{
$this->setContent(null);
return $this->setStatusCode($code);
return $this
->setStatusCode($code)
->setContent(null)
;
}


/**
* Bind a collection to a transformer and start building a response.
*
* @param array|Collection $items
* @param TransformerInterface $transformer
* @param array $parameters
* Bind an item to a transformer and start building a response.
* @param $item
* @param int $statusCode
* @return Response
*/
public function collection($items, TransformerInterface $transformer, array $parameters = [])
public function item($item, $statusCode = self::HTTP_OK)
{
return $this->collectionWithStatusCode($items, $transformer);
}


/**
* Respond with a created response.
*
* @param array|Collection $items
* @param TransformerInterface $transformer
* @param array $parameters
* @return Response
*/
public function createdCollection($items, TransformerInterface $transformer, array $parameters = [])
{
foreach ($items as $item) {
$item->setVisible(['']);
if ($this->transformer){
$item = $this->transformer->transformItem($item);
}
return $this->collectionWithStatusCode($items, $transformer, self::HTTP_CREATED);
}

/**
* Bind an item to a transformer and start building a response.
*
* @param object|string $item
* @param TransformerInterface $transformer
* @param array $parameters
* @return Response
*/
public function item($item, TransformerInterface $transformer, array $parameters = [])
{
return $this->itemWithStatusCode($item, $transformer);
return $this
->header('Content-Type', 'application/json')
->setContent($this->encode($item))
->setStatusCode($statusCode)
;
}


/**
* Respond with a created response.
*
* @param object $item
* @param TransformerInterface $transformer
* @param array $parameters
* @param $item
* @return Response
*/
public function createdItem($item, TransformerInterface $transformer, array $parameters = [])
public function createdItem($item)
{
$item->setVisible(['']);
return $this->itemWithStatusCode($item, $transformer, self::HTTP_CREATED);
return $this->item($item, self::HTTP_CREATED);
}

/**
* @param $item
* @param TransformerInterface $transformer
* @param int $code
* @param $items
* @param int $statusCode
* @return Response
*/
protected function itemWithStatusCode($item, TransformerInterface $transformer, $code = self::HTTP_OK)
public function collection($items, $statusCode = Response::HTTP_OK)
{
$this->setStatusCode($code);
$this->headers->set('Content-Type', 'application/json');
$this->setContent($this->encode($transformer->transformItem($item)));
return $this;

if ($this->transformer){
$items = $this->transformer->transformCollection($items);
}

return $this
->header('Content-Type', 'application/json')
->setContent($this->encode($items))
->setStatusCode($statusCode)
;
}


/**
* Respond with a created response and hide all the items (except self)
* @param $items
* @param TransformerInterface $transformer
* @param int $code
* @return Response
*/
protected function collectionWithStatusCode($items, TransformerInterface $transformer, $code = self::HTTP_OK)
public function createdCollection($items)
{
$this->setStatusCode($code);
$this->headers->set('Content-Type', 'application/json');
$this->setContent($this->encode($transformer->transformCollection($items)));
foreach ($items as $item) {
$item->setVisible(['']);
}

return $this;
return $this->collection($items, self::HTTP_CREATED);
}


/**
* Build paginated response.
*
* @param Collection|array $items
* @param null|int $offset
* @param null|int $totalCount
* @param TransformerInterface $transformer
* @param array $parameters
* @param $items
* @param null $offset
* @param null $totalCount
* @return Response
* @throws HttpException
*/
public function paginatedCollection($items, TransformerInterface $transformer, $offset = null, $totalCount = null, array $parameters = [])
public function paginatedCollection($items, $offset = null, $totalCount = null)
{
$itemCount = count($items);
$this->validateRange($itemCount);

$this->headers->set('Accept-Ranges', 'entities');
$this->headers->set('Content-Type', 'application/json');
$this->setStatusCode(self::HTTP_PARTIAL_CONTENT);

$rangeHeader = $this->prepareRangeHeader($itemCount, $offset, $totalCount);
$this->headers->set('Content-Range', $rangeHeader);

$this->setContent($this->encode($transformer->transformCollection($items)));


return $this;
return $this
->header('Accept-Ranges', 'entities')
->header('Content-Type', 'application/json')
->header('Content-Range', $rangeHeader)
->collection($items, self::HTTP_PARTIAL_CONTENT)
;
}

/**
Expand Down

0 comments on commit 27febf0

Please sign in to comment.