Skip to content

Commit

Permalink
Merge pull request #4 from ehsanabbasi/hotfix/fixingincrease
Browse files Browse the repository at this point in the history
fixing increase problem in increase hash count
  • Loading branch information
sr-hosseyni authored Jun 9, 2018
2 parents 1426607 + 4e84d42 commit 98fe021
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 56 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
22 changes: 6 additions & 16 deletions src/ShortUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,28 @@ 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;
}

/**
* @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);
}

/**
* @param string $hashKey
* @return string
*/
public function getHashStats (string $hashKey) : ?string
public function getHashStats(string $hashKey): ?string
{
return $this->storage->getCount($hashKey);

}

/**
Expand All @@ -62,7 +61,6 @@ public function hashIsAvailable(string $hashKey) :bool
public function getUrl(string $hashKey) : ?string
{
return $this->storage->getUrlByHash($hashKey);

}


Expand All @@ -72,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 ;
}

}

19 changes: 8 additions & 11 deletions src/UrlValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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;
}
Expand All @@ -60,7 +59,5 @@ public static function validateUrl(string $url) :bool {
}

return true;

}

}
1 change: 0 additions & 1 deletion src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@
interface Validator
{
public static function validateUrl(string $url);

}
3 changes: 2 additions & 1 deletion src/database/StorageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Miniurl\Database\Storage\RedisStorage;
use Predis;
use PDO;

class StorageFactory extends AbstractStorageFactory
{
const REDIS = 'redis';
Expand All @@ -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));
Expand Down
1 change: 0 additions & 1 deletion src/database/StorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ public function update();
public function incCount(string $hash);
public function getUrlByHash(string $hash);
public function checkHash(string $hash);

}
12 changes: 6 additions & 6 deletions src/database/storages/MysqlStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
Expand All @@ -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.
}
Expand Down
9 changes: 5 additions & 4 deletions src/database/storages/RedisStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -41,12 +39,15 @@ public function update()
// TODO: Implement update() method.
}

public function incCount(string $hash) :void
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');
Expand Down
17 changes: 8 additions & 9 deletions tests/ShortUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ 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
{
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'],
Expand All @@ -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'],
Expand All @@ -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],
Expand All @@ -93,16 +95,13 @@ public function hashDataProvider() :array {
];
}


/**
* @dataProvider hashDataProvider
* @param $hash
* @param $exception
*/
public function testGetHashStats($hash, $exception) :void
{

$this->assertEquals($exception,$this->shortUrl->getHashStats($hash));
$this->assertEquals($exception, $this->shortUrl->getHashStats($hash));
}

}
8 changes: 4 additions & 4 deletions tests/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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));
}

}

0 comments on commit 98fe021

Please sign in to comment.