From aba7d3185a594f9358df14440e356fc10f9e9d5d Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 1 May 2016 11:21:54 +0200 Subject: [PATCH] Added shortcode chars as an environment variable --- .env.dist | 1 + config/autoload/url-shortener.global.php | 9 ++++++--- .../CliRoutable/GenerateShortcodeMiddleware.php | 14 +++++++------- src/Service/UrlShortener.php | 4 ++-- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/.env.dist b/.env.dist index 2e34016a..a25e8c2c 100644 --- a/.env.dist +++ b/.env.dist @@ -2,6 +2,7 @@ APP_ENV= SHORTENED_URL_SCHEMA= SHORTENED_URL_HOSTNAME= +SHORTCODE_CHARS= # Database DB_USER= diff --git a/config/autoload/url-shortener.global.php b/config/autoload/url-shortener.global.php index 795ce021..718ed3f0 100644 --- a/config/autoload/url-shortener.global.php +++ b/config/autoload/url-shortener.global.php @@ -1,9 +1,12 @@ [ - 'schema' => getenv('SHORTENED_URL_SCHEMA') ?: 'http', - 'hostname' => getenv('SHORTENED_URL_HOSTNAME'), + 'url_shortener' => [ + 'domain' => [ + 'schema' => getenv('SHORTENED_URL_SCHEMA') ?: 'http', + 'hostname' => getenv('SHORTENED_URL_HOSTNAME'), + ], + 'shortcode_chars' => getenv('SHORTCODE_CHARS'), ], ]; diff --git a/src/Middleware/CliRoutable/GenerateShortcodeMiddleware.php b/src/Middleware/CliRoutable/GenerateShortcodeMiddleware.php index 9b3cd6c4..6573ed60 100644 --- a/src/Middleware/CliRoutable/GenerateShortcodeMiddleware.php +++ b/src/Middleware/CliRoutable/GenerateShortcodeMiddleware.php @@ -19,20 +19,20 @@ class GenerateShortcodeMiddleware implements MiddlewareInterface /** * @var array */ - private $config; + private $domainConfig; /** * GenerateShortcodeMiddleware constructor. * * @param UrlShortenerInterface|UrlShortener $urlShortener - * @param array $config + * @param array $domainConfig * - * @Inject({UrlShortener::class, "config.url-shortener"}) + * @Inject({UrlShortener::class, "config.url_shortener.domain"}) */ - public function __construct(UrlShortenerInterface $urlShortener, array $config) + public function __construct(UrlShortenerInterface $urlShortener, array $domainConfig) { $this->urlShortener = $urlShortener; - $this->config = $config; + $this->domainConfig = $domainConfig; } /** @@ -72,8 +72,8 @@ class GenerateShortcodeMiddleware implements MiddlewareInterface $shortcode = $this->urlShortener->urlToShortCode(new Uri($longUrl)); $shortUrl = (new Uri())->withPath($shortcode) - ->withScheme($this->config['schema']) - ->withHost($this->config['hostname']); + ->withScheme($this->domainConfig['schema']) + ->withHost($this->domainConfig['hostname']); $response->getBody()->write( sprintf('Processed URL "%s".%sGenerated short URL "%s"', $longUrl, PHP_EOL, $shortUrl) . PHP_EOL diff --git a/src/Service/UrlShortener.php b/src/Service/UrlShortener.php index 3fee0cf3..3c15a0f6 100644 --- a/src/Service/UrlShortener.php +++ b/src/Service/UrlShortener.php @@ -14,7 +14,7 @@ use Psr\Http\Message\UriInterface; class UrlShortener implements UrlShortenerInterface { - const DEFAULT_CHARS = 'rYHxLkXfsptbNZzKDG4hy85WFT7BRgMVdC9jvwQPnc6S32Jqm'; + const DEFAULT_CHARS = '123456789bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ'; /** * @var ClientInterface @@ -35,7 +35,7 @@ class UrlShortener implements UrlShortenerInterface * @param EntityManagerInterface $em * @param string $chars * - * @Inject({"httpClient", "em"}) + * @Inject({"httpClient", "em", "config.url_shortener.shortcode_chars"}) */ public function __construct( ClientInterface $httpClient,