Registered action to list tags

This commit is contained in:
Alejandro Celaya 2017-07-07 13:12:45 +02:00
parent c37660f763
commit 95ec7e0afa
11 changed files with 87 additions and 15 deletions

View File

@ -19,6 +19,7 @@ return [
Action\GetVisitsAction::class => AnnotatedFactory::class,
Action\ListShortcodesAction::class => AnnotatedFactory::class,
Action\EditShortcodeTagsAction::class => AnnotatedFactory::class,
Action\ListTagsAction::class => AnnotatedFactory::class,
Middleware\BodyParserMiddleware::class => AnnotatedFactory::class,
Middleware\CrossDomainMiddleware::class => InvokableFactory::class,

View File

@ -5,41 +5,53 @@ return [
'routes' => [
[
'name' => 'rest-authenticate',
'name' => Action\AuthenticateAction::class,
'path' => '/rest/v{version:1}/authenticate',
'middleware' => Action\AuthenticateAction::class,
'allowed_methods' => ['POST'],
],
// Short codes
[
'name' => 'rest-create-shortcode',
'name' => Action\CreateShortcodeAction::class,
'path' => '/rest/v{version:1}/short-codes',
'middleware' => Action\CreateShortcodeAction::class,
'allowed_methods' => ['POST'],
],
[
'name' => 'rest-resolve-url',
'name' => Action\ResolveUrlAction::class,
'path' => '/rest/v{version:1}/short-codes/{shortCode}',
'middleware' => Action\ResolveUrlAction::class,
'allowed_methods' => ['GET'],
],
[
'name' => 'rest-list-shortened-url',
'name' => Action\ListShortcodesAction::class,
'path' => '/rest/v{version:1}/short-codes',
'middleware' => Action\ListShortcodesAction::class,
'allowed_methods' => ['GET'],
],
[
'name' => 'rest-get-visits',
'path' => '/rest/v{version:1}/short-codes/{shortCode}/visits',
'middleware' => Action\GetVisitsAction::class,
'allowed_methods' => ['GET'],
],
[
'name' => 'rest-edit-tags',
'name' => Action\EditShortcodeTagsAction::class,
'path' => '/rest/v{version:1}/short-codes/{shortCode}/tags',
'middleware' => Action\EditShortcodeTagsAction::class,
'allowed_methods' => ['PUT'],
],
// Visits
[
'name' => Action\GetVisitsAction::class,
'path' => '/rest/v{version:1}/short-codes/{shortCode}/visits',
'middleware' => Action\GetVisitsAction::class,
'allowed_methods' => ['GET'],
],
// Tags
[
'name' => Action\ListTagsAction::class,
'path' => '/rest/v{version:1}/tags',
'middleware' => Action\ListTagsAction::class,
'allowed_methods' => ['GET'],
],
],
];

View File

@ -54,6 +54,7 @@ class AuthenticateAction extends AbstractRestAction
* @param Request $request
* @param DelegateInterface $delegate
* @return null|Response
* @throws \InvalidArgumentException
*/
public function process(Request $request, DelegateInterface $delegate)
{

View File

@ -55,6 +55,7 @@ class CreateShortcodeAction extends AbstractRestAction
* @param Request $request
* @param DelegateInterface $delegate
* @return null|Response
* @throws \InvalidArgumentException
*/
public function process(Request $request, DelegateInterface $delegate)
{

View File

@ -46,6 +46,7 @@ class EditShortcodeTagsAction extends AbstractRestAction
* @param Request $request
* @param DelegateInterface $delegate
* @return null|Response
* @throws \InvalidArgumentException
*/
public function process(Request $request, DelegateInterface $delegate)
{

View File

@ -47,6 +47,7 @@ class GetVisitsAction extends AbstractRestAction
* @param Request $request
* @param DelegateInterface $delegate
* @return null|Response
* @throws \InvalidArgumentException
*/
public function process(Request $request, DelegateInterface $delegate)
{

View File

@ -48,6 +48,7 @@ class ListShortcodesAction extends AbstractRestAction
* @param Request $request
* @param DelegateInterface $delegate
* @return null|Response
* @throws \InvalidArgumentException
*/
public function process(Request $request, DelegateInterface $delegate)
{
@ -66,7 +67,7 @@ class ListShortcodesAction extends AbstractRestAction
/**
* @param array $query
* @return string
* @return array
*/
public function queryToListParams(array $query)
{

View File

@ -0,0 +1,51 @@
<?php
namespace Shlinkio\Shlink\Rest\Action;
use Acelaya\ZsmAnnotatedServices\Annotation as DI;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Core\Service\Tag\TagService;
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
use Zend\Diactoros\Response\JsonResponse;
class ListTagsAction extends AbstractRestAction
{
/**
* @var TagServiceInterface
*/
private $tagService;
/**
* ListTagsAction constructor.
* @param TagServiceInterface $tagService
* @param LoggerInterface|null $logger
*
* @DI\Inject({TagService::class, LoggerInterface::class})
*/
public function __construct(TagServiceInterface $tagService, LoggerInterface $logger = null)
{
parent::__construct($logger);
$this->tagService = $tagService;
}
/**
* Process an incoming server request and return a response, optionally delegating
* to the next middleware component to create the response.
*
* @param ServerRequestInterface $request
* @param DelegateInterface $delegate
*
* @return ResponseInterface
* @throws \InvalidArgumentException
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
return new JsonResponse([
'tags' => [
'data' => $this->tagService->listTags(),
],
]);
}
}

View File

@ -46,6 +46,7 @@ class ResolveUrlAction extends AbstractRestAction
* @param Request $request
* @param DelegateInterface $delegate
* @return null|Response
* @throws \InvalidArgumentException
*/
public function process(Request $request, DelegateInterface $delegate)
{
@ -53,7 +54,7 @@ class ResolveUrlAction extends AbstractRestAction
try {
$longUrl = $this->urlShortener->shortCodeToUrl($shortCode);
if (! isset($longUrl)) {
if ($longUrl === null) {
return new JsonResponse([
'error' => RestUtils::INVALID_ARGUMENT_ERROR,
'message' => sprintf($this->translator->translate('No URL found for short code "%s"'), $shortCode),

View File

@ -9,6 +9,7 @@ use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Shlinkio\Shlink\Rest\Action\AuthenticateAction;
use Shlinkio\Shlink\Rest\Authentication\JWTService;
use Shlinkio\Shlink\Rest\Authentication\JWTServiceInterface;
use Shlinkio\Shlink\Rest\Exception\AuthenticationException;
@ -69,7 +70,7 @@ class CheckAuthenticationMiddleware implements MiddlewareInterface, StatusCodeIn
$routeResult = $request->getAttribute(RouteResult::class);
if (! isset($routeResult)
|| $routeResult->isFailure()
|| $routeResult->getMatchedRouteName() === 'rest-authenticate'
|| $routeResult->getMatchedRouteName() === AuthenticateAction::class
|| $request->getMethod() === 'OPTIONS'
) {
return $delegate->process($request);

View File

@ -5,6 +5,7 @@ use Interop\Http\ServerMiddleware\DelegateInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\MethodProphecy;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Rest\Action\AuthenticateAction;
use Shlinkio\Shlink\Rest\Authentication\JWTService;
use Shlinkio\Shlink\Rest\Middleware\CheckAuthenticationMiddleware;
use ShlinkioTest\Shlink\Common\Util\TestUtils;
@ -56,7 +57,7 @@ class CheckAuthenticationMiddlewareTest extends TestCase
$request = ServerRequestFactory::fromGlobals()->withAttribute(
RouteResult::class,
RouteResult::fromRoute(new Route('foo', '', Route::HTTP_METHOD_ANY, 'rest-authenticate'), [])
RouteResult::fromRoute(new Route('foo', '', Route::HTTP_METHOD_ANY, AuthenticateAction::class))
);
$delegate = $this->prophesize(DelegateInterface::class);
/** @var MethodProphecy $process */