Added shortcode chars as an environment variable

This commit is contained in:
Alejandro Celaya 2016-05-01 11:21:54 +02:00
parent 3bb899bced
commit aba7d3185a
4 changed files with 16 additions and 12 deletions

View file

@ -2,6 +2,7 @@
APP_ENV=
SHORTENED_URL_SCHEMA=
SHORTENED_URL_HOSTNAME=
SHORTCODE_CHARS=
# Database
DB_USER=

View file

@ -1,9 +1,12 @@
<?php
return [
'url-shortener' => [
'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'),
],
];

View file

@ -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

View file

@ -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,