Skip to content

Instantly share code, notes, and snippets.

@alefcastelo
Created November 7, 2016 17:50
Show Gist options
  • Save alefcastelo/465dc505e85b2d7e3c5dc1c06aa89247 to your computer and use it in GitHub Desktop.
Save alefcastelo/465dc505e85b2d7e3c5dc1c06aa89247 to your computer and use it in GitHub Desktop.
<?php
namespace App\Credential;
use Utreino\Core\Domain\Repository\RepositoryInterface;
use Utreino\Core\Infrastructure\Proxy\RepositoryPaginatorProxy;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\JsonResponse;
class ListCredential
{
private $repository;
public function __construct(RepositoryInterface $repository)
{
$this->repository = $repository;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
$params = $request->getQueryParams();
$limit = isset($params['limit']) && is_numeric($params['limit']) ? $params['limit'] : 10;
$offset = isset($params['offset']) && is_numeric($params['offset']) ? $params['offset'] : 0;
$uuid = $request->getAttribute('uuid');
if ( ! is_null($uuid)) {
$credential = $this->repository->findByUuid($uuid);
if( is_null($credential) )
$response = new JsonResponse(null, 404, ['Message' => 'Credential not exists']);
else
$response = new JsonResponse($credential->toArray(), 200, ['Message' => 'Credential exists']);
} else {
$credentials = (new RepositoryPaginatorProxy($this->repository))
->findPaginatorByToArray([], [], $limit, $offset);
$response = new JsonResponse($credentials, 200, ['Message' => 'List credential']);
}
if ($next)
$next($request, $response, $next);
return $response;
}
}
@alefcastelo
Copy link
Author

Rota: api/v1/credentials
Response:
HTTP/1.1 200 OK
Date: Mon, 07 Nov 2016 17:35:18 GMT
Server: Apache/2.4.10 (Debian)
X-Powered-By: PHP/5.6.26
Message: List credential
Content-Length: 1982
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment