From 18d9815e8890db9d6fc0104bb1db308f101b2308 Mon Sep 17 00:00:00 2001 From: Mikolaj Gogula Date: Tue, 17 Oct 2017 11:03:12 +0200 Subject: [PATCH 1/6] Added option for enable/disable URL Validation by response status code. --- .../UrlShortenerConfigCustomizerPlugin.php | 9 ++++++++- module/CLI/src/Model/CustomizableAppConfig.php | 2 ++ .../UrlShortenerConfigCustomizerPluginTest.php | 9 +++++++-- .../Common/test/Factory/CacheFactoryTest.php | 2 +- module/Core/src/Service/UrlShortener.php | 18 ++++++++++++++---- module/Core/test/Service/UrlShortenerTest.php | 18 +++++++++++++++++- 6 files changed, 49 insertions(+), 9 deletions(-) diff --git a/module/CLI/src/Install/Plugin/UrlShortenerConfigCustomizerPlugin.php b/module/CLI/src/Install/Plugin/UrlShortenerConfigCustomizerPlugin.php index ac9c32e9..886749e0 100644 --- a/module/CLI/src/Install/Plugin/UrlShortenerConfigCustomizerPlugin.php +++ b/module/CLI/src/Install/Plugin/UrlShortenerConfigCustomizerPlugin.php @@ -42,7 +42,14 @@ class UrlShortenerConfigCustomizerPlugin extends AbstractConfigCustomizerPlugin 'Character set for generated short codes (leave empty to autogenerate one)', null, true - ) ?: str_shuffle(UrlShortener::DEFAULT_CHARS) + ) ?: str_shuffle(UrlShortener::DEFAULT_CHARS), + 'VALIDATE_URL' => $this->questionHelper->ask( + $input, + $output, + new ConfirmationQuestion( + 'Do you want to validate long urls by 200 HTTP status code on response (Y/n):' + ) + ) ]); } } diff --git a/module/CLI/src/Model/CustomizableAppConfig.php b/module/CLI/src/Model/CustomizableAppConfig.php index 03836e0f..34f3674c 100644 --- a/module/CLI/src/Model/CustomizableAppConfig.php +++ b/module/CLI/src/Model/CustomizableAppConfig.php @@ -189,6 +189,7 @@ final class CustomizableAppConfig implements ArraySerializableInterface 'SCHEMA' => $urlShortener['domain']['schema'], 'HOSTNAME' => $urlShortener['domain']['hostname'], 'CHARS' => $urlShortener['shortcode_chars'], + 'VALIDATE_URL' => $urlShortener['validate_url'], ]); } } @@ -240,6 +241,7 @@ final class CustomizableAppConfig implements ArraySerializableInterface 'hostname' => $this->urlShortener['HOSTNAME'], ], 'shortcode_chars' => $this->urlShortener['CHARS'], + 'validate_url' => $this->urlShortener['VALIDATE_URL'] ], ]; diff --git a/module/CLI/test/Install/Plugin/UrlShortenerConfigCustomizerPluginTest.php b/module/CLI/test/Install/Plugin/UrlShortenerConfigCustomizerPluginTest.php index 85eaf681..7d535397 100644 --- a/module/CLI/test/Install/Plugin/UrlShortenerConfigCustomizerPluginTest.php +++ b/module/CLI/test/Install/Plugin/UrlShortenerConfigCustomizerPluginTest.php @@ -45,8 +45,9 @@ class UrlShortenerConfigCustomizerPluginTest extends TestCase 'SCHEMA' => 'something', 'HOSTNAME' => 'something', 'CHARS' => 'something', + 'VALIDATE_URL' => 'something', ], $config->getUrlShortener()); - $askSecret->shouldHaveBeenCalledTimes(3); + $askSecret->shouldHaveBeenCalledTimes(4); } /** @@ -64,6 +65,7 @@ class UrlShortenerConfigCustomizerPluginTest extends TestCase 'SCHEMA' => 'bar', 'HOSTNAME' => 'bar', 'CHARS' => 'bar', + 'VALIDATE_URL' => 'bar', ]); $this->plugin->process(new ArrayInput([]), new NullOutput(), $config); @@ -72,8 +74,9 @@ class UrlShortenerConfigCustomizerPluginTest extends TestCase 'SCHEMA' => 'foo', 'HOSTNAME' => 'foo', 'CHARS' => 'foo', + 'VALIDATE_URL' => false, ], $config->getUrlShortener()); - $ask->shouldHaveBeenCalledTimes(4); + $ask->shouldHaveBeenCalledTimes(5); } /** @@ -89,6 +92,7 @@ class UrlShortenerConfigCustomizerPluginTest extends TestCase 'SCHEMA' => 'foo', 'HOSTNAME' => 'foo', 'CHARS' => 'foo', + 'VALIDATE_URL' => 'foo', ]); $this->plugin->process(new ArrayInput([]), new NullOutput(), $config); @@ -97,6 +101,7 @@ class UrlShortenerConfigCustomizerPluginTest extends TestCase 'SCHEMA' => 'foo', 'HOSTNAME' => 'foo', 'CHARS' => 'foo', + 'VALIDATE_URL' => 'foo', ], $config->getUrlShortener()); $ask->shouldHaveBeenCalledTimes(1); } diff --git a/module/Common/test/Factory/CacheFactoryTest.php b/module/Common/test/Factory/CacheFactoryTest.php index 9a095ad6..58542426 100644 --- a/module/Common/test/Factory/CacheFactoryTest.php +++ b/module/Common/test/Factory/CacheFactoryTest.php @@ -73,7 +73,7 @@ class CacheFactoryTest extends TestCase */ public function filesystemCacheAdaptersReadDirOption() { - $dir = sys_get_temp_dir(); + $dir = realpath(sys_get_temp_dir()); /** @var FilesystemCache $instance */ $instance = $this->factory->__invoke($this->createSM(FilesystemCache::class, ['dir' => $dir]), ''); $this->assertInstanceOf(FilesystemCache::class, $instance); diff --git a/module/Core/src/Service/UrlShortener.php b/module/Core/src/Service/UrlShortener.php index 9d6927bf..d67c7c16 100644 --- a/module/Core/src/Service/UrlShortener.php +++ b/module/Core/src/Service/UrlShortener.php @@ -36,6 +36,10 @@ class UrlShortener implements UrlShortenerInterface * @var Cache */ private $cache; + /** + * @var bool + */ + private $isUrlExistsValidation; /** * UrlShortener constructor. @@ -43,19 +47,22 @@ class UrlShortener implements UrlShortenerInterface * @param EntityManagerInterface $em * @param Cache $cache * @param string $chars + * @param bool $isUrlExistsValidation * - * @Inject({"httpClient", "em", Cache::class, "config.url_shortener.shortcode_chars"}) + * @Inject({"httpClient", "em", Cache::class, "config.url_shortener.shortcode_chars", "config.url_shortener.validate_url"}) */ public function __construct( ClientInterface $httpClient, EntityManagerInterface $em, Cache $cache, - $chars = self::DEFAULT_CHARS + $chars = self::DEFAULT_CHARS, + $isUrlExistsValidation ) { $this->httpClient = $httpClient; $this->em = $em; $this->chars = empty($chars) ? self::DEFAULT_CHARS : $chars; $this->cache = $cache; + $this->isUrlExistsValidation = $isUrlExistsValidation; } /** @@ -77,8 +84,11 @@ class UrlShortener implements UrlShortenerInterface return $shortUrl->getShortCode(); } - // Check that the URL exists - $this->checkUrlExists($url); + // Check if the validation of url is enabled in the config + if (true === $this->isUrlExistsValidation) { + // Check that the URL exists + $this->checkUrlExists($url); + } // Transactionally insert the short url, then generate the short code and finally update the short code try { diff --git a/module/Core/test/Service/UrlShortenerTest.php b/module/Core/test/Service/UrlShortenerTest.php index 5d39deb4..e2980194 100644 --- a/module/Core/test/Service/UrlShortenerTest.php +++ b/module/Core/test/Service/UrlShortenerTest.php @@ -58,7 +58,21 @@ class UrlShortenerTest extends TestCase $this->cache = new ArrayCache(); - $this->urlShortener = new UrlShortener($this->httpClient->reveal(), $this->em->reveal(), $this->cache); + $this->setUrlShortener(false); + } + + /** + * @param bool $isUrlValidationExists + */ + public function setUrlShortener($isUrlValidationExists) + { + $this->urlShortener = new UrlShortener( + $this->httpClient->reveal(), + $this->em->reveal(), + $this->cache, + UrlShortener::DEFAULT_CHARS, + $isUrlValidationExists + ); } /** @@ -93,6 +107,8 @@ class UrlShortenerTest extends TestCase */ public function exceptionIsThrownWhenUrlDoesNotExist() { + $this->setUrlShortener(true); + $this->httpClient->request(Argument::cetera())->willThrow( new ClientException('', $this->prophesize(Request::class)->reveal()) ); From 16a2349d863c5b1dcb62afd8c7a405d12fe0d475 Mon Sep 17 00:00:00 2001 From: Mikolaj Gogula Date: Tue, 17 Oct 2017 11:28:05 +0200 Subject: [PATCH 2/6] Composer fixes. --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 637a66ab..6299cd08 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,8 @@ "theorchard/monolog-cascade": "^0.4", "endroid/qrcode": "^1.7", "mikehaertl/phpwkhtmltopdf": "^2.2", - "doctrine/migrations": "^1.4" + "doctrine/migrations": "^1.4", + "http-interop/http-middleware": "^0.4" }, "require-dev": { "phpunit/phpunit": "^5.7 || ^6.0", From 08d18b1dc15fe19d886318ebaf9dd067217b1aca Mon Sep 17 00:00:00 2001 From: Mikolaj Gogula Date: Tue, 17 Oct 2017 11:33:11 +0200 Subject: [PATCH 3/6] Codestyle fixes. --- module/Core/src/Service/UrlShortener.php | 14 ++++++++++---- module/Core/test/Service/UrlShortenerTest.php | 6 +----- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/module/Core/src/Service/UrlShortener.php b/module/Core/src/Service/UrlShortener.php index d67c7c16..1d652e07 100644 --- a/module/Core/src/Service/UrlShortener.php +++ b/module/Core/src/Service/UrlShortener.php @@ -46,17 +46,23 @@ class UrlShortener implements UrlShortenerInterface * @param ClientInterface $httpClient * @param EntityManagerInterface $em * @param Cache $cache - * @param string $chars * @param bool $isUrlExistsValidation + * @param string $chars * - * @Inject({"httpClient", "em", Cache::class, "config.url_shortener.shortcode_chars", "config.url_shortener.validate_url"}) + * @Inject({ + * "httpClient", + * "em", + * Cache::class, + * "config.url_shortener.validate_url", + * "config.url_shortener.shortcode_chars" + * }) */ public function __construct( ClientInterface $httpClient, EntityManagerInterface $em, Cache $cache, - $chars = self::DEFAULT_CHARS, - $isUrlExistsValidation + $isUrlExistsValidation, + $chars = self::DEFAULT_CHARS ) { $this->httpClient = $httpClient; $this->em = $em; diff --git a/module/Core/test/Service/UrlShortenerTest.php b/module/Core/test/Service/UrlShortenerTest.php index e2980194..0552ef5a 100644 --- a/module/Core/test/Service/UrlShortenerTest.php +++ b/module/Core/test/Service/UrlShortenerTest.php @@ -67,11 +67,7 @@ class UrlShortenerTest extends TestCase public function setUrlShortener($isUrlValidationExists) { $this->urlShortener = new UrlShortener( - $this->httpClient->reveal(), - $this->em->reveal(), - $this->cache, - UrlShortener::DEFAULT_CHARS, - $isUrlValidationExists + $this->httpClient->reveal(), $this->em->reveal(), $this->cache, $isUrlValidationExists, UrlShortener::DEFAULT_CHARS ); } From fef5390a629e26560b8ba7858c63ceda4727d470 Mon Sep 17 00:00:00 2001 From: Mikolaj Gogula Date: Tue, 17 Oct 2017 11:35:32 +0200 Subject: [PATCH 4/6] Codestyle fixes. --- module/Core/test/Service/UrlShortenerTest.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/module/Core/test/Service/UrlShortenerTest.php b/module/Core/test/Service/UrlShortenerTest.php index 0552ef5a..4e7c9547 100644 --- a/module/Core/test/Service/UrlShortenerTest.php +++ b/module/Core/test/Service/UrlShortenerTest.php @@ -67,7 +67,11 @@ class UrlShortenerTest extends TestCase public function setUrlShortener($isUrlValidationExists) { $this->urlShortener = new UrlShortener( - $this->httpClient->reveal(), $this->em->reveal(), $this->cache, $isUrlValidationExists, UrlShortener::DEFAULT_CHARS + $this->httpClient->reveal(), + $this->em->reveal(), + $this->cache, + $isUrlValidationExists, + UrlShortener::DEFAULT_CHARS ); } From 297c88c334f07f71955acfd74b336245c1fec8e6 Mon Sep 17 00:00:00 2001 From: Mikolaj Gogula Date: Tue, 17 Oct 2017 11:44:30 +0200 Subject: [PATCH 5/6] Change variable name for validation enabled. --- module/Core/src/Service/UrlShortener.php | 10 +++++----- module/Core/test/Service/UrlShortenerTest.php | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/module/Core/src/Service/UrlShortener.php b/module/Core/src/Service/UrlShortener.php index 1d652e07..b6008276 100644 --- a/module/Core/src/Service/UrlShortener.php +++ b/module/Core/src/Service/UrlShortener.php @@ -39,14 +39,14 @@ class UrlShortener implements UrlShortenerInterface /** * @var bool */ - private $isUrlExistsValidation; + private $urlValidationEnabled; /** * UrlShortener constructor. * @param ClientInterface $httpClient * @param EntityManagerInterface $em * @param Cache $cache - * @param bool $isUrlExistsValidation + * @param bool $urlValidationEnabled * @param string $chars * * @Inject({ @@ -61,14 +61,14 @@ class UrlShortener implements UrlShortenerInterface ClientInterface $httpClient, EntityManagerInterface $em, Cache $cache, - $isUrlExistsValidation, + $urlValidationEnabled, $chars = self::DEFAULT_CHARS ) { $this->httpClient = $httpClient; $this->em = $em; $this->chars = empty($chars) ? self::DEFAULT_CHARS : $chars; $this->cache = $cache; - $this->isUrlExistsValidation = $isUrlExistsValidation; + $this->urlValidationEnabled = $urlValidationEnabled; } /** @@ -91,7 +91,7 @@ class UrlShortener implements UrlShortenerInterface } // Check if the validation of url is enabled in the config - if (true === $this->isUrlExistsValidation) { + if (true === $this->urlValidationEnabled) { // Check that the URL exists $this->checkUrlExists($url); } diff --git a/module/Core/test/Service/UrlShortenerTest.php b/module/Core/test/Service/UrlShortenerTest.php index 4e7c9547..494a45f6 100644 --- a/module/Core/test/Service/UrlShortenerTest.php +++ b/module/Core/test/Service/UrlShortenerTest.php @@ -62,15 +62,15 @@ class UrlShortenerTest extends TestCase } /** - * @param bool $isUrlValidationExists + * @param bool $urlValidationEnabled */ - public function setUrlShortener($isUrlValidationExists) + public function setUrlShortener($urlValidationEnabled) { $this->urlShortener = new UrlShortener( $this->httpClient->reveal(), $this->em->reveal(), $this->cache, - $isUrlValidationExists, + $urlValidationEnabled, UrlShortener::DEFAULT_CHARS ); } From d04abd1f7590572308703512f111cde0032e8759 Mon Sep 17 00:00:00 2001 From: Mikolaj Gogula Date: Mon, 23 Oct 2017 11:28:04 +0200 Subject: [PATCH 6/6] Added validate_url config key for development. --- config/autoload/url-shortener.global.php | 1 + 1 file changed, 1 insertion(+) diff --git a/config/autoload/url-shortener.global.php b/config/autoload/url-shortener.global.php index f17d192d..82c8acd6 100644 --- a/config/autoload/url-shortener.global.php +++ b/config/autoload/url-shortener.global.php @@ -10,6 +10,7 @@ return [ 'hostname' => Common\env('SHORTENED_URL_HOSTNAME'), ], 'shortcode_chars' => Common\env('SHORTCODE_CHARS', UrlShortener::DEFAULT_CHARS), + 'validate_url' => true, ], ];