From 95ec7e0afae6cd3ca7c11312d44f074f214eee4a Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 7 Jul 2017 13:12:45 +0200 Subject: [PATCH] Registered action to list tags --- module/Rest/config/dependencies.config.php | 1 + module/Rest/config/routes.config.php | 34 +++++++++---- module/Rest/src/Action/AuthenticateAction.php | 1 + .../Rest/src/Action/CreateShortcodeAction.php | 1 + .../src/Action/EditShortcodeTagsAction.php | 1 + module/Rest/src/Action/GetVisitsAction.php | 1 + .../Rest/src/Action/ListShortcodesAction.php | 3 +- module/Rest/src/Action/ListTagsAction.php | 51 +++++++++++++++++++ module/Rest/src/Action/ResolveUrlAction.php | 3 +- .../CheckAuthenticationMiddleware.php | 3 +- .../CheckAuthenticationMiddlewareTest.php | 3 +- 11 files changed, 87 insertions(+), 15 deletions(-) create mode 100644 module/Rest/src/Action/ListTagsAction.php diff --git a/module/Rest/config/dependencies.config.php b/module/Rest/config/dependencies.config.php index d3bff420..d48d319b 100644 --- a/module/Rest/config/dependencies.config.php +++ b/module/Rest/config/dependencies.config.php @@ -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, diff --git a/module/Rest/config/routes.config.php b/module/Rest/config/routes.config.php index 8344e52a..6212e6a8 100644 --- a/module/Rest/config/routes.config.php +++ b/module/Rest/config/routes.config.php @@ -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'], + ], ], ]; diff --git a/module/Rest/src/Action/AuthenticateAction.php b/module/Rest/src/Action/AuthenticateAction.php index 73d6a7df..a4b15920 100644 --- a/module/Rest/src/Action/AuthenticateAction.php +++ b/module/Rest/src/Action/AuthenticateAction.php @@ -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) { diff --git a/module/Rest/src/Action/CreateShortcodeAction.php b/module/Rest/src/Action/CreateShortcodeAction.php index 5384aac9..3cea7469 100644 --- a/module/Rest/src/Action/CreateShortcodeAction.php +++ b/module/Rest/src/Action/CreateShortcodeAction.php @@ -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) { diff --git a/module/Rest/src/Action/EditShortcodeTagsAction.php b/module/Rest/src/Action/EditShortcodeTagsAction.php index 9eab0759..19f3a29d 100644 --- a/module/Rest/src/Action/EditShortcodeTagsAction.php +++ b/module/Rest/src/Action/EditShortcodeTagsAction.php @@ -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) { diff --git a/module/Rest/src/Action/GetVisitsAction.php b/module/Rest/src/Action/GetVisitsAction.php index 681c9338..0a03436c 100644 --- a/module/Rest/src/Action/GetVisitsAction.php +++ b/module/Rest/src/Action/GetVisitsAction.php @@ -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) { diff --git a/module/Rest/src/Action/ListShortcodesAction.php b/module/Rest/src/Action/ListShortcodesAction.php index 43803a7f..f4bf420b 100644 --- a/module/Rest/src/Action/ListShortcodesAction.php +++ b/module/Rest/src/Action/ListShortcodesAction.php @@ -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) { diff --git a/module/Rest/src/Action/ListTagsAction.php b/module/Rest/src/Action/ListTagsAction.php new file mode 100644 index 00000000..afee7df5 --- /dev/null +++ b/module/Rest/src/Action/ListTagsAction.php @@ -0,0 +1,51 @@ +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(), + ], + ]); + } +} diff --git a/module/Rest/src/Action/ResolveUrlAction.php b/module/Rest/src/Action/ResolveUrlAction.php index 55280922..f8167b16 100644 --- a/module/Rest/src/Action/ResolveUrlAction.php +++ b/module/Rest/src/Action/ResolveUrlAction.php @@ -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), diff --git a/module/Rest/src/Middleware/CheckAuthenticationMiddleware.php b/module/Rest/src/Middleware/CheckAuthenticationMiddleware.php index 0304a408..d7d922ad 100644 --- a/module/Rest/src/Middleware/CheckAuthenticationMiddleware.php +++ b/module/Rest/src/Middleware/CheckAuthenticationMiddleware.php @@ -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); diff --git a/module/Rest/test/Middleware/CheckAuthenticationMiddlewareTest.php b/module/Rest/test/Middleware/CheckAuthenticationMiddlewareTest.php index 65523f62..dcb905fe 100644 --- a/module/Rest/test/Middleware/CheckAuthenticationMiddlewareTest.php +++ b/module/Rest/test/Middleware/CheckAuthenticationMiddlewareTest.php @@ -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 */