Skip to content

Instantly share code, notes, and snippets.

@alefcastelo
Created November 7, 2016 17:55
Show Gist options
  • Save alefcastelo/a65b1825b04f533e760e84897fee9851 to your computer and use it in GitHub Desktop.
Save alefcastelo/a65b1825b04f533e760e84897fee9851 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 = $response->withStatus(404)
->withHeader('Message', 'Credential not exists');
else
$response->withStatus(200)
->withHeader('Message', 'Credential exists')
->getBody()->write(json_encode($credential->toArray()));
} else {
$credentials = (new RepositoryPaginatorProxy($this->repository))
->findPaginatorByToArray([], [], $limit, $offset);
$response->withStatus(200)
->withHeader('Message', 'List credential')
->getBody()->write(json_encode($credentials));
}
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:38:34 GMT
Server: Apache/2.4.10 (Debian)
X-Powered-By: PHP/5.6.26
Content-Length: 1988
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