From 1fbefbbd15bf365416fe522b53ccdd5493d7b07d Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 12 Jun 2016 17:51:30 +0200 Subject: [PATCH] Created shortcode creation rest endpoint --- composer.json | 4 +- config/autoload/routes.global.php | 9 ++ .../Rest/CreateShortcodeMiddleware.php | 98 +++++++++++++++++++ src/Util/RestUtils.php | 26 +++++ 4 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 src/Middleware/Rest/CreateShortcodeMiddleware.php create mode 100644 src/Util/RestUtils.php diff --git a/composer.json b/composer.json index 922e9067..15f92bee 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ } ], "require": { - "php": "^5.5 || ^7.0", + "php": "^5.6 || ^7.0", "zendframework/zend-expressive": "^1.0", "zendframework/zend-expressive-helpers": "^2.0", "zendframework/zend-expressive-fastroute": "^1.1", @@ -24,7 +24,7 @@ "symfony/console": "^3.0" }, "require-dev": { - "phpunit/phpunit": "^4.8", + "phpunit/phpunit": "^5.0", "squizlabs/php_codesniffer": "^2.3", "roave/security-advisories": "dev-master", "filp/whoops": "^2.0", diff --git a/config/autoload/routes.global.php b/config/autoload/routes.global.php index 40a3d20b..7f16c641 100644 --- a/config/autoload/routes.global.php +++ b/config/autoload/routes.global.php @@ -1,5 +1,6 @@ Routable\RedirectMiddleware::class, 'allowed_methods' => ['GET'], ], + + // Rest + [ + 'name' => 'rest-create-shortcode', + 'path' => '/rest/short-code', + 'middleware' => Rest\CreateShortcodeMiddleware::class, + 'allowed_methods' => ['POST'], + ], ], ]; diff --git a/src/Middleware/Rest/CreateShortcodeMiddleware.php b/src/Middleware/Rest/CreateShortcodeMiddleware.php new file mode 100644 index 00000000..21ab4379 --- /dev/null +++ b/src/Middleware/Rest/CreateShortcodeMiddleware.php @@ -0,0 +1,98 @@ +urlShortener = $urlShortener; + $this->domainConfig = $domainConfig; + } + + /** + * Process an incoming request and/or response. + * + * Accepts a server-side request and a response instance, and does + * something with them. + * + * If the response is not complete and/or further processing would not + * interfere with the work done in the middleware, or if the middleware + * wants to delegate to another process, it can use the `$out` callable + * if present. + * + * If the middleware does not return a value, execution of the current + * request is considered complete, and the response instance provided will + * be considered the response to return. + * + * Alternately, the middleware may return a response instance. + * + * Often, middleware will `return $out();`, with the assumption that a + * later middleware will return a response. + * + * @param Request $request + * @param Response $response + * @param null|callable $out + * @return null|Response + */ + public function __invoke(Request $request, Response $response, callable $out = null) + { + $postData = $request->getParsedBody(); + if (! isset($postData['longUrl'])) { + return new JsonResponse([ + 'error' => RestUtils::INVALID_ARGUMENT_ERROR, + 'message' => 'A URL was not provided', + ], 400); + } + $longUrl = $postData['longUrl']; + + try { + $shortcode = $this->urlShortener->urlToShortCode(new Uri($longUrl)); + $shortUrl = (new Uri())->withPath($shortcode) + ->withScheme($this->domainConfig['schema']) + ->withHost($this->domainConfig['hostname']); + + return new JsonResponse([ + 'longUrl' => $longUrl, + 'shortUrl' => $shortUrl->__toString(), + ]); + } catch (InvalidUrlException $e) { + return new JsonResponse([ + 'error' => RestUtils::getRestErrorCodeFromException($e), + 'message' => sprintf('Provided URL "%s" is invalid. Try with a different one.', $longUrl), + ], 400); + } catch (\Exception $e) { + return new JsonResponse([ + 'error' => RestUtils::UNKNOWN_ERROR, + 'message' => sprintf('Provided URL "%s" is invalid. Try with a different one.', $longUrl), + ], 500); + } + } +} diff --git a/src/Util/RestUtils.php b/src/Util/RestUtils.php new file mode 100644 index 00000000..d4c7179d --- /dev/null +++ b/src/Util/RestUtils.php @@ -0,0 +1,26 @@ +