Skip to content

Commit

Permalink
testapp
Browse files Browse the repository at this point in the history
  • Loading branch information
roelVerdonschot committed Mar 15, 2016
1 parent 4b119f8 commit 0591dbf
Show file tree
Hide file tree
Showing 80 changed files with 2,634 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
9 changes: 9 additions & 0 deletions .gitignore
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
3 changes: 3 additions & 0 deletions CONTRIBUTING.md
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!
3 changes: 3 additions & 0 deletions app/Way/Exceptions/NonExistentHashException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php namespace Way\Exceptions;

class NonExistentHashException extends \Exception {};
12 changes: 12 additions & 0 deletions app/Way/Link.php
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'];

}
18 changes: 18 additions & 0 deletions app/Way/Repositories/BackendServiceProvider.php
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'
);
}

}
43 changes: 43 additions & 0 deletions app/Way/Repositories/DbLinkRepository.php
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();
}

}
26 changes: 26 additions & 0 deletions app/Way/Repositories/LinkRepositoryInterface.php
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);

}
17 changes: 17 additions & 0 deletions app/Way/Shortener/Facades/Little.php
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';
}

}
77 changes: 77 additions & 0 deletions app/Way/Shortener/LittleService.php
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;
}
}
15 changes: 15 additions & 0 deletions app/Way/Shortener/LittleServiceProvider.php
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');
}

}
32 changes: 32 additions & 0 deletions app/Way/Utilities/UrlHasher.php
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);
}

}
21 changes: 21 additions & 0 deletions app/Way/Utilities/UtilitiesServiceProvider.php
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);
});
}

}
15 changes: 15 additions & 0 deletions app/Way/Validators/LinkValidator.php
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'
];

}
27 changes: 27 additions & 0 deletions app/Way/Validators/ValidationException.php
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;
}
}
36 changes: 36 additions & 0 deletions app/Way/Validators/Validator.php
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 added app/commands/.gitkeep
Empty file.
Loading

0 comments on commit 0591dbf

Please sign in to comment.