From 6c733f95502ce10cf49658d8129876592a0e9d58 Mon Sep 17 00:00:00 2001 From: Ehsan Abbasi Date: Tue, 29 May 2018 16:20:10 +0430 Subject: [PATCH 1/4] fixing increase problem in increase hash count --- src/ShortUrl.php | 5 +++-- src/database/storages/RedisStorage.php | 8 ++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/ShortUrl.php b/src/ShortUrl.php index 2c19464..c61207f 100644 --- a/src/ShortUrl.php +++ b/src/ShortUrl.php @@ -25,10 +25,11 @@ public function __construct(StorageInterface $storage , UrlValidator $urlValidat /** * @param string $hashKey + * @return null|string */ - public function increaseHashCount (string $hashKey) :void + public function increaseHashCount (string $hashKey) : ?string { - $this->storage->incCount($hashKey); + return $this->storage->incCount($hashKey); } diff --git a/src/database/storages/RedisStorage.php b/src/database/storages/RedisStorage.php index 7121cd1..3f0a3b0 100644 --- a/src/database/storages/RedisStorage.php +++ b/src/database/storages/RedisStorage.php @@ -41,9 +41,13 @@ public function update() // TODO: Implement update() method. } - public function incCount(string $hash) :void + public function incCount(string $hash) : ?string { - $this->client->hincrby($hash, 'count', 1); + if (empty($this->checkHash($hash))) { + + return 'Hash Does NOT Exist'; + } + return $this->client->hincrby($hash, 'count', 1); } From e0b776ea0f4e2cd1fd0218ecc0ac5387880373bd Mon Sep 17 00:00:00 2001 From: Ehsan Abbasi Date: Tue, 29 May 2018 16:34:09 +0430 Subject: [PATCH 2/4] return null on increase hash --- src/database/storages/RedisStorage.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/database/storages/RedisStorage.php b/src/database/storages/RedisStorage.php index 3f0a3b0..ba2c8e0 100644 --- a/src/database/storages/RedisStorage.php +++ b/src/database/storages/RedisStorage.php @@ -47,7 +47,8 @@ public function incCount(string $hash) : ?string return 'Hash Does NOT Exist'; } - return $this->client->hincrby($hash, 'count', 1); + $this->client->hincrby($hash, 'count', 1); + return null; } From 19101570ebe59d5bcf7cf443cf576561d76d06f1 Mon Sep 17 00:00:00 2001 From: Ehsan Abbasi Date: Sat, 2 Jun 2018 13:33:22 +0430 Subject: [PATCH 3/4] Check the psr2 standards --- src/ShortUrl.php | 19 ++++--------------- src/UrlValidator.php | 19 ++++++++----------- src/Validator.php | 1 - src/database/StorageFactory.php | 3 ++- src/database/StorageInterface.php | 1 - src/database/storages/MysqlStorage.php | 12 ++++++------ src/database/storages/RedisStorage.php | 4 ---- tests/ShortUrlTest.php | 17 ++++++++--------- tests/UrlTest.php | 8 ++++---- 9 files changed, 32 insertions(+), 52 deletions(-) diff --git a/src/ShortUrl.php b/src/ShortUrl.php index c61207f..3b9bbec 100644 --- a/src/ShortUrl.php +++ b/src/ShortUrl.php @@ -17,7 +17,7 @@ class ShortUrl * @internal param $config * @internal param StorageInterface $storage */ - public function __construct(StorageInterface $storage , UrlValidator $urlValidator) + public function __construct(StorageInterface $storage, UrlValidator $urlValidator) { $this->storage = $storage; $this->urlValidator = $urlValidator; @@ -27,20 +27,18 @@ public function __construct(StorageInterface $storage , UrlValidator $urlValidat * @param string $hashKey * @return null|string */ - public function increaseHashCount (string $hashKey) : ?string + public function increaseHashCount(string $hashKey) : ?string { return $this->storage->incCount($hashKey); - } /** * @param string $hashKey * @return string */ - public function getHashStats (string $hashKey) : ?string + public function getHashStats(string $hashKey): ?string { return $this->storage->getCount($hashKey); - } /** @@ -63,7 +61,6 @@ public function hashIsAvailable(string $hashKey) :bool public function getUrl(string $hashKey) : ?string { return $this->storage->getUrlByHash($hashKey); - } @@ -73,33 +70,25 @@ public function getUrl(string $hashKey) : ?string */ public function insertShortCodeInDataBase(string $url) :?string { - $hashUrl = $this->createRepetitiveHash($url); - if (!empty($hashUrl)) { - - return $this->storage->store($this->createRepetitiveHash($url),$url); + return $this->storage->store($this->createRepetitiveHash($url), $url); } return null; - } - /** * @param string $url * @return bool|string */ private function createRepetitiveHash(string $url) :?string { - if ($this->urlValidator::validateUrl($url)) { return substr(sha1($url), 0, 10); } return null ; } - } - diff --git a/src/UrlValidator.php b/src/UrlValidator.php index 07a08d7..d94a1fc 100644 --- a/src/UrlValidator.php +++ b/src/UrlValidator.php @@ -9,9 +9,9 @@ class UrlValidator implements Validator * @param $url * @return mixed */ - private static function validateUrlFormat(string $url) { - return filter_var($url, FILTER_VALIDATE_URL, - FILTER_FLAG_PATH_REQUIRED); + private static function validateUrlFormat(string $url) + { + return filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED); /** * We USE FILTER_FLAG_PATH_REQUIRED @@ -21,19 +21,18 @@ private static function validateUrlFormat(string $url) { * FILTER_FLAG_HOST_REQUIRED * FILTER_FLAG_SCHEME_REQUIRED */ - } /** * @param $url * @return bool */ - private static function verifyUrlExists(string $url) :bool { - + private static function verifyUrlExists(string $url) :bool + { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_NOBODY, true); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_exec($ch); $response = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); @@ -43,14 +42,14 @@ private static function verifyUrlExists(string $url) :bool { } return false; - } /** * @param $url * @return bool */ - public static function validateUrl(string $url) :bool { + public static function validateUrl(string $url) :bool + { if (self::validateUrlFormat($url) == false) { return false; } @@ -60,7 +59,5 @@ public static function validateUrl(string $url) :bool { } return true; - } - } diff --git a/src/Validator.php b/src/Validator.php index 6d7c65c..ff10b69 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -5,5 +5,4 @@ interface Validator { public static function validateUrl(string $url); - } diff --git a/src/database/StorageFactory.php b/src/database/StorageFactory.php index f973304..23efa63 100644 --- a/src/database/StorageFactory.php +++ b/src/database/StorageFactory.php @@ -5,6 +5,7 @@ use Miniurl\Database\Storage\RedisStorage; use Predis; use PDO; + class StorageFactory extends AbstractStorageFactory { const REDIS = 'redis'; @@ -20,7 +21,7 @@ public static function getStorage(array $config) $database = new RedisStorage($config, new Predis\Client($config)); break; case self::MYSQL: - $database = new MysqlStorage($config,new PDO($config)); + $database = new MysqlStorage($config, new PDO($config)); break; default: $database = new RedisStorage($config, new Predis\Client($config)); diff --git a/src/database/StorageInterface.php b/src/database/StorageInterface.php index 3328e30..2190f31 100644 --- a/src/database/StorageInterface.php +++ b/src/database/StorageInterface.php @@ -9,5 +9,4 @@ public function update(); public function incCount(string $hash); public function getUrlByHash(string $hash); public function checkHash(string $hash); - } diff --git a/src/database/storages/MysqlStorage.php b/src/database/storages/MysqlStorage.php index 4461786..0711a02 100644 --- a/src/database/storages/MysqlStorage.php +++ b/src/database/storages/MysqlStorage.php @@ -9,19 +9,19 @@ class MysqlStorage implements StorageInterface private $config; private $client; - public function __construct($config,$client) + public function __construct($config, $client) { $this->config = $config; $this->client = $client; } - public function store($hash, $url) + public function store(string $hash, string $url) :string { // TODO: Implement store() method. } - public function getCount($hash) + public function getCount(string $hash) :?string { // TODO: Implement getCount() method. } @@ -31,17 +31,17 @@ public function update() // TODO: Implement update() method. } - public function incCount($hash) + public function incCount(string $hash) :?string { // TODO: Implement incCount() method. } - public function getUrlByHash($hash) + public function getUrlByHash(string $hash) :?string { // TODO: Implement getUrlByHash() method. } - public function checkHash($hash) + public function checkHash(string $hash) :array { // TODO: Implement checkHash() method. } diff --git a/src/database/storages/RedisStorage.php b/src/database/storages/RedisStorage.php index ba2c8e0..6693528 100644 --- a/src/database/storages/RedisStorage.php +++ b/src/database/storages/RedisStorage.php @@ -14,13 +14,11 @@ public function __construct(array $config, Predis\Client $client) { $this->baseUrl = $config['baseUrl']; $this->client = $client; - } public function store(string $hash, string $url) :string { if (empty($this->checkHash($hash))) { - $this->client->hmset($hash, [ 'url' => $url, 'shortedUrl'=> $this->baseUrl."/".$hash, @@ -44,14 +42,12 @@ public function update() public function incCount(string $hash) : ?string { if (empty($this->checkHash($hash))) { - return 'Hash Does NOT Exist'; } $this->client->hincrby($hash, 'count', 1); return null; } - public function getUrlByHash(string $hash) : ?string { return $this->client->hget($hash, 'url'); diff --git a/tests/ShortUrlTest.php b/tests/ShortUrlTest.php index c387db1..34d5fa7 100644 --- a/tests/ShortUrlTest.php +++ b/tests/ShortUrlTest.php @@ -31,7 +31,7 @@ public function setUp() :void ->setConstructorArgs(array($this->config, $myRedisMockClass)) ->getMock(); - $this->shortUrl = new ShortUrl($this->redis , new UrlValidator()); + $this->shortUrl = new ShortUrl($this->redis, new UrlValidator()); } public function tearDown() :void @@ -39,7 +39,8 @@ public function tearDown() :void unset($this->shortUrl); } - public function urlDataProvider() :array { + public function urlDataProvider() :array + { return [ ['https://www.sheypoor.com/%D8%A7%DB%8C%D8%B1%D8%A7%D9%86/%DA%A9%D8%B3%D8%A8-%DA%A9%D8%A7%D8%B1',$this->config['baseUrl']."/".'0503acb8e8'], ['https://www.sheypoor.com/%DA%A9%D9%BE%D8%B3%D9%88%D9%84-%D9%BE%DB%8C%DA%A9%D9%86%DB%8C%DA%A9-52836998.html',$this->config['baseUrl']."/".'7a3c06e576'], @@ -57,12 +58,12 @@ public function urlDataProvider() :array { public function testInsertShortCodeInDataBase($url, $hash) :void { - $this->assertEquals($hash, $this->shortUrl->insertShortCodeInDataBase($url)); } - public function getUrlDataProvider() :array { + public function getUrlDataProvider() :array + { return [ ['https://www.sheypoor.com/%D8%A7%DB%8C%D8%B1%D8%A7%D9%86/%DA%A9%D8%B3%D8%A8-%DA%A9%D8%A7%D8%B1','0503acb8e8'], ['https://www.sheypoor.com/%DA%A9%D9%BE%D8%B3%D9%88%D9%84-%D9%BE%DB%8C%DA%A9%D9%86%DB%8C%DA%A9-52836998.html','7a3c06e576'], @@ -83,7 +84,8 @@ public function testGetUrl($url, $hash) :void $this->assertEquals($url, $this->shortUrl->getUrl($hash)); } - public function hashDataProvider() :array { + public function hashDataProvider() :array + { return [ ['0503acb8e8',0], ['7a3c06e576',0], @@ -93,7 +95,6 @@ public function hashDataProvider() :array { ]; } - /** * @dataProvider hashDataProvider * @param $hash @@ -101,8 +102,6 @@ public function hashDataProvider() :array { */ public function testGetHashStats($hash, $exception) :void { - - $this->assertEquals($exception,$this->shortUrl->getHashStats($hash)); + $this->assertEquals($exception, $this->shortUrl->getHashStats($hash)); } - } diff --git a/tests/UrlTest.php b/tests/UrlTest.php index 993e448..5471fd6 100644 --- a/tests/UrlTest.php +++ b/tests/UrlTest.php @@ -18,7 +18,8 @@ public function tearDown() :void unset($this->url); } - public function urlDataProvider() :array { + public function urlDataProvider() :array + { return array( array('https://www.sheypoor.com/%D8%A7%DB%8C%D8%B1%D8%A7%D9%86/%DA%A9%D8%B3%D8%A8-%DA%A9%D8%A7%D8%B1',true), array('https://www.sheypoor.com/%DA%A9%D9%BE%D8%B3%D9%88%D9%84-%D9%BE%DB%8C%DA%A9%D9%86%DB%8C%DA%A9-52836998.html',true), @@ -32,9 +33,8 @@ public function urlDataProvider() :array { /** * @dataProvider urlDataProvider */ - public function testValidateUrl($url,$exception) :void + public function testValidateUrl($url, $exception) :void { - $this->assertEquals($exception,$this->url->validateUrl($url)); + $this->assertEquals($exception, $this->url->validateUrl($url)); } - } From 4e84d420e8658c1faec3b07454e40dd41235c4bb Mon Sep 17 00:00:00 2001 From: Ehsan Abbasi Date: Sat, 9 Jun 2018 11:41:51 +0430 Subject: [PATCH 4/4] change m6web libarary place in composer --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 84d4042..71d37f2 100644 --- a/composer.json +++ b/composer.json @@ -3,12 +3,12 @@ "description": "Simple Url Shortener", "require": { "PHP": "^7.1", - "predis/predis": "^1.1", - "m6web/redis-mock": "~2.0" + "predis/predis": "^1.1" }, "require-dev": { "phpunit/phpunit": "^7", - "behat/behat": "3.*" + "behat/behat": "3.*", + "m6web/redis-mock": "~2.0" }, "autoload": { "classmap": [