-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b119f8
commit 0591dbf
Showing
80 changed files
with
2,634 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/bootstrap/compiled.php | ||
/vendor | ||
composer.phar | ||
composer.lock | ||
.env.local.php | ||
.env.php | ||
.DS_Store | ||
Thumbs.db | ||
/.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Contribution Guidelines | ||
|
||
Please submit all issues and pull requests to the [laravel/framework](http://github.com/laravel/framework) repository! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php namespace Way\Exceptions; | ||
|
||
class NonExistentHashException extends \Exception {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php namespace Way; | ||
|
||
class Link extends \Eloquent { | ||
|
||
/** | ||
* Fillable fields | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = ['url', 'hash']; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php namespace Way\Repositories; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
class BackendServiceProvider extends ServiceProvider { | ||
|
||
/** | ||
* Register bindings with IoC container | ||
*/ | ||
public function register() | ||
{ | ||
$this->app->bind( | ||
'Way\Repositories\LinkRepositoryInterface', | ||
'Way\Repositories\DbLinkRepository' | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php namespace Way\Repositories; | ||
|
||
use Way\Link; | ||
|
||
class DbLinkRepository implements LinkRepositoryInterface { | ||
|
||
/** | ||
* Create new link in db | ||
* | ||
* @param array $data | ||
* | ||
* @return mixed | ||
*/ | ||
public function create(array $data) | ||
{ | ||
return Link::create($data); | ||
} | ||
|
||
/** | ||
* Fetch link by hash | ||
* | ||
* @param $hash | ||
* | ||
* @return mixed | ||
*/ | ||
public function byHash($hash) | ||
{ | ||
return Link::whereHash($hash)->first(); | ||
} | ||
|
||
/** | ||
* Fetch link by url | ||
* | ||
* @param $url | ||
* | ||
* @return mixed | ||
*/ | ||
public function byUrl($url) | ||
{ | ||
return Link::whereUrl($url)->first(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php namespace Way\Repositories; | ||
|
||
interface LinkRepositoryInterface { | ||
|
||
/** | ||
* Create new link | ||
* | ||
* @param array $data | ||
*/ | ||
public function create(array $data); | ||
|
||
/** | ||
* Fetch link by hash | ||
* | ||
* @param $hash | ||
*/ | ||
public function byHash($hash); | ||
|
||
/** | ||
* Fetch link by url | ||
* | ||
* @param $url | ||
*/ | ||
public function byUrl($url); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php namespace Way\Shortener\Facades; | ||
|
||
use Illuminate\Support\Facades\Facade; | ||
|
||
class Little extends Facade { | ||
|
||
/** | ||
* Get name of binding in IoC container | ||
* | ||
* @return string | ||
*/ | ||
public static function getFacadeAccessor() | ||
{ | ||
return 'little'; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php namespace Way\Shortener; | ||
|
||
use Way\Repositories\LinkRepositoryInterface as LinkRepo; | ||
use Way\Utilities\UrlHasher; | ||
use Way\Exceptions\NonExistentHashException; | ||
|
||
class LittleService { | ||
|
||
/** | ||
* @var \Way\Repositories\LinkRepositoryInterface | ||
*/ | ||
protected $linkRepo; | ||
|
||
/** | ||
* @var \Way\Utilities\UrlHasher | ||
*/ | ||
protected $urlHasher; | ||
|
||
/** | ||
* @param LinkRepo $linkRepo | ||
* @param UrlHasher $urlHasher | ||
*/ | ||
public function __construct(LinkRepo $linkRepo, UrlHasher $urlHasher) | ||
{ | ||
$this->linkRepo = $linkRepo; | ||
$this->urlHasher = $urlHasher; | ||
} | ||
|
||
/** | ||
* Save url to db and hash | ||
* | ||
* @param $url | ||
* | ||
* @return string | ||
*/ | ||
public function make($url) | ||
{ | ||
$link = $this->linkRepo->byUrl($url); | ||
|
||
return $link ? $link->hash : $this->makeHash($url); | ||
} | ||
|
||
/** | ||
* Fetch a url by hash | ||
* | ||
* @param $hash | ||
* | ||
* @return mixed | ||
* @throws \Way\Exceptions\NonExistentHashException | ||
*/ | ||
public function getUrlByHash($hash) | ||
{ | ||
$link = $this->linkRepo->byHash($hash); | ||
|
||
if ( ! $link) throw new NonExistentHashException; | ||
|
||
return $link->url; | ||
} | ||
|
||
/** | ||
* Prepare and save new url + hash | ||
* | ||
* @param $url | ||
* @returns string | ||
*/ | ||
private function makeHash($url) | ||
{ | ||
$hash = $this->urlHasher->make($url); | ||
$data = compact('url', 'hash'); | ||
|
||
\Event::fire('link.creating', [$data]); | ||
|
||
$this->linkRepo->create($data); | ||
|
||
return $hash; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php namespace Way\Shortener; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
class LittleServiceProvider extends ServiceProvider { | ||
|
||
/** | ||
* Register in IoC container | ||
*/ | ||
public function register() | ||
{ | ||
$this->app->bind('little', 'Way\Shortener\LittleService'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php namespace Way\Utilities; | ||
|
||
class UrlHasher { | ||
|
||
/** | ||
* @var integer | ||
*/ | ||
protected $hashLength; | ||
|
||
/** | ||
* @param integer $hashLength | ||
*/ | ||
public function __construct($hashLength) | ||
{ | ||
$this->hashLength = $hashLength; | ||
} | ||
|
||
/** | ||
* Prepare URL hash | ||
* | ||
* @param $url | ||
* | ||
* @return string | ||
*/ | ||
public function make($url) | ||
{ | ||
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | ||
|
||
return substr(str_shuffle(str_repeat($pool, 5)), 0, $this->hashLength); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php namespace Way\Utilities; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
use Way\Utilities\UrlHasher; | ||
|
||
class UtilitiesServiceProvider extends ServiceProvider { | ||
|
||
/** | ||
* Register in IoC container | ||
*/ | ||
public function register() | ||
{ | ||
$this->app->bind('Way\Utilities\UrlHasher', function() | ||
{ | ||
$length = 5; | ||
|
||
return new UrlHasher($length); | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php namespace Way\Validators; | ||
|
||
class LinkValidator extends Validator { | ||
|
||
/** | ||
* Rules for a link | ||
* | ||
* @var array | ||
*/ | ||
protected static $rules = [ | ||
'url' => 'required|url|unique:links,url', | ||
'hash' => 'required|unique:links,hash' | ||
]; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php namespace Way\Validators; | ||
|
||
class ValidationException extends \Exception { | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $errors; | ||
|
||
/** | ||
* @param string $errors | ||
*/ | ||
public function __construct($errors) | ||
{ | ||
$this->errors = $errors; | ||
} | ||
|
||
/** | ||
* Fetch validation errors | ||
* | ||
* @return string | ||
*/ | ||
public function getErrors() | ||
{ | ||
return $this->errors; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php namespace Way\Validators; | ||
|
||
use Illuminate\Validation\Factory; | ||
|
||
abstract class Validator { | ||
|
||
/** | ||
* @var \Illuminate\Validation\Factory | ||
*/ | ||
protected $validator; | ||
|
||
/** | ||
* @param Factory $validator | ||
*/ | ||
public function __construct(Factory $validator) | ||
{ | ||
$this->validator = $validator; | ||
} | ||
|
||
/** | ||
* Trigger validation | ||
* | ||
* @param array $data | ||
* | ||
* @return bool | ||
* @throws ValidationException | ||
*/ | ||
public function fire(array $data) | ||
{ | ||
$validation = $this->validator->make($data, static::$rules); | ||
|
||
if ( $validation->fails()) throw new ValidationException($validation->messages()); | ||
|
||
return true; | ||
} | ||
} |
Empty file.
Oops, something went wrong.