-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
30 changed files
with
1,184 additions
and
22 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,11 @@ | ||
# IDE | ||
.idea | ||
|
||
# Binary | ||
bin | ||
|
||
# Dependency | ||
vendor | ||
|
||
# Lock | ||
composer.lock |
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,25 @@ | ||
sudo: false | ||
language: php | ||
php: | ||
- 5.6 | ||
- 7.0 | ||
- hhvm | ||
matrix: | ||
allow_failures: | ||
- php: nightly | ||
cache: | ||
directories: | ||
- $HOME/.composer/cache | ||
- vendor | ||
branches: | ||
only: | ||
- master | ||
notifications: | ||
email: false | ||
before_script: | ||
- travis_retry composer self-update | ||
- travis_retry composer install --no-interaction --prefer-source --dev | ||
script: | ||
- php --version | ||
- composer --version | ||
- composer check |
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,42 @@ | ||
<?php | ||
|
||
namespace Leopardd\Bundle\UrlShortenerBundle\Controller; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Leopardd\Bundle\UrlShortenerBundle\Factory\ShortUrlFactory; | ||
use Leopardd\Bundle\UrlShortenerBundle\Service\EncodeService; | ||
use Leopardd\Bundle\UrlShortenerBundle\Exception\InvalidUrlException; | ||
|
||
class EncodeController extends Controller | ||
{ | ||
/** | ||
* @param Request $request | ||
* @throws InvalidUrlException | ||
* @return JsonResponse | ||
*/ | ||
public function indexAction(Request $request) | ||
{ | ||
/** @var ShortUrlFactory $shortUrlFactory */ | ||
$shortUrlFactory = $this->get('leopardd_url_shortener.factory.short_url'); | ||
|
||
/** @var EncodeService $encodeService */ | ||
$encodeService = $this->get('leopardd_url_shortener.service.encode'); | ||
|
||
$url = $request->request->get('url'); | ||
|
||
// validate and sanitize | ||
if (filter_var($url, FILTER_VALIDATE_URL) === false) throw new InvalidUrlException(); | ||
$url = rtrim($url, '/'); | ||
|
||
$shortUrl = $shortUrlFactory->create($url); | ||
$shortUrl = $encodeService->process($shortUrl); | ||
|
||
return new JsonResponse([ | ||
'url' => $shortUrl->getUrl(), | ||
'code' => $shortUrl->getCode() | ||
]); | ||
} | ||
} |
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 Leopardd\Bundle\UrlShortenerBundle\Controller; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Leopardd\Bundle\UrlShortenerBundle\Service\RedirectService; | ||
use Leopardd\Bundle\UrlShortenerBundle\Exception\InvalidCodeException; | ||
|
||
class RedirectController extends Controller | ||
{ | ||
/** | ||
* @param string $code | ||
* @throws InvalidCodeException | ||
* @return RedirectResponse | ||
*/ | ||
public function indexAction($code) | ||
{ | ||
/** @var RedirectService $redirectService */ | ||
$redirectService = $this->get('leopardd_url_shortener.service.redirect'); | ||
|
||
$response = $redirectService->getRedirectResponse($code); | ||
if ($response === null) throw new InvalidCodeException(); | ||
|
||
return $response; | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
|
||
namespace Leopardd\Bundle\UrlShortenerBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
|
||
/** | ||
* This is the class that validates and merges configuration from your app/config files. | ||
* | ||
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/configuration.html} | ||
*/ | ||
class Configuration implements ConfigurationInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getConfigTreeBuilder() | ||
{ | ||
$treeBuilder = new TreeBuilder(); | ||
$rootNode = $treeBuilder->root('leopardd_url_shortener'); | ||
|
||
// Here you should define the parameters that are allowed to | ||
// configure your bundle. See the documentation linked above for | ||
// more information on that topic. | ||
$rootNode | ||
->children() | ||
->arrayNode('hashids') | ||
->children() | ||
->scalarNode('salt')->end() | ||
->integerNode('min_length')->end() | ||
->scalarNode('alphabet')->end() | ||
->end() | ||
->end() | ||
->end(); | ||
|
||
return $treeBuilder; | ||
} | ||
} |
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 Leopardd\Bundle\UrlShortenerBundle\DependencyInjection; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
use Symfony\Component\DependencyInjection\Loader; | ||
|
||
/** | ||
* This is the class that loads and manages your bundle configuration. | ||
* | ||
* @link http://symfony.com/doc/current/cookbook/bundles/extension.html | ||
*/ | ||
class LeoparddUrlShortenerExtension extends Extension | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function load(array $configs, ContainerBuilder $container) | ||
{ | ||
$configuration = new Configuration(); | ||
$config = $this->processConfiguration($configuration, $configs); | ||
|
||
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); | ||
$loader->load('services.yml'); | ||
|
||
// Hashids Config | ||
if (isset($config['hashids'])) { | ||
if (isset($config['hashids']['salt'])) { | ||
$container->setParameter('leopardd_url_shortener.hashids.salt', $config['hashids']['salt']); | ||
} | ||
|
||
if (isset($config['hashids']['min_length'])) { | ||
$container->setParameter('leopardd_url_shortener.hashids.min_length', $config['hashids']['min_length']); | ||
} | ||
|
||
if (isset($config['hashids']['alphabet'])) { | ||
$container->setParameter('leopardd_url_shortener.hashids.alphabet', $config['hashids']['alphabet']); | ||
} | ||
} | ||
} | ||
} |
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,125 @@ | ||
<?php | ||
|
||
namespace Leopardd\Bundle\UrlShortenerBundle\Entity; | ||
|
||
use \DateTime as DateTime; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use JMS\Serializer\Annotation as JMS; | ||
|
||
/** | ||
* Class ShortUrl | ||
* @package Leopardd\Bundle\UrlShortenerBundle\Entity | ||
* @ORM\Entity(repositoryClass="Leopardd\Bundle\UrlShortenerBundle\Repository\ShortUrlRepository") | ||
* @ORM\Table(name="short_url", indexes={@ORM\Index(name="idx", columns={"id", "url"})}) | ||
*/ | ||
class ShortUrl implements ShortUrlInterface | ||
{ | ||
/** | ||
* @var int | ||
* @ORM\Id | ||
* @ORM\Column(name="id", type="integer") | ||
* @ORM\GeneratedValue(strategy="AUTO") | ||
* @JMS\Expose() | ||
*/ | ||
protected $id; | ||
|
||
/** | ||
* @var string | ||
* @Assert\NotBlank(message="The code should not be blank") | ||
* @ORM\Column(name="code", type="string", unique=true, nullable=true, options={"collation":"utf8_bin"}) | ||
* @JMS\Expose() | ||
*/ | ||
protected $code; | ||
|
||
/** | ||
* @var string | ||
* @Assert\NotBlank(message="The url should not be blank") | ||
* @Assert\Length( | ||
* max = 255, | ||
* maxMessage = "The url cannot be longer than {{ limit }} characters" | ||
* ) | ||
* @ORM\Column(name="url", type="string", length=255) | ||
* @JMS\Expose() | ||
*/ | ||
protected $url; | ||
|
||
/** | ||
* @var DateTime | ||
* @ORM\Column(name="created", type="datetime") | ||
* @JMS\Type("DateTime<'Y-m-d H:i:s', 'UTC'>") | ||
* @JMS\Expose() | ||
*/ | ||
private $created; | ||
|
||
/** | ||
* ShortUrl constructor. | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->created = new DateTime(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getCode() | ||
{ | ||
return $this->code; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setCode($code) | ||
{ | ||
$this->code = $code; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getUrl() | ||
{ | ||
return $this->url; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setUrl($url) | ||
{ | ||
$this->url = $url; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return DateTime | ||
*/ | ||
public function getCreated() | ||
{ | ||
return $this->created; | ||
} | ||
|
||
/** | ||
* @param DateTime $created | ||
* @return ShortUrl | ||
*/ | ||
public function setCreated($created) | ||
{ | ||
$this->created = $created; | ||
|
||
return $this; | ||
} | ||
} |
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,37 @@ | ||
<?php | ||
|
||
namespace Leopardd\Bundle\UrlShortenerBundle\Entity; | ||
|
||
/** | ||
* Interface ShortUrlInterface | ||
* @package Leopardd\Bundle\UrlShortenerBundle\Entity | ||
*/ | ||
interface ShortUrlInterface | ||
{ | ||
/** | ||
* @return int | ||
*/ | ||
public function getId(); | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getCode(); | ||
|
||
/** | ||
* @param string $code | ||
* @return ShortUrlInterface | ||
*/ | ||
public function setCode($code); | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getUrl(); | ||
|
||
/** | ||
* @param string $url | ||
* @return ShortUrlInterface | ||
*/ | ||
public function setUrl($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,33 @@ | ||
<?php | ||
|
||
namespace Leopardd\Bundle\UrlShortenerBundle\Event; | ||
|
||
use Symfony\Component\EventDispatcher\Event; | ||
use Leopardd\Bundle\UrlShortenerBundle\Entity\ShortUrlInterface; | ||
|
||
/** | ||
* Class ShortUrlCreatedEvent | ||
* @package Leopardd\Bundle\UrlShortenerBundle\Event | ||
*/ | ||
class ShortUrlCreatedEvent extends Event | ||
{ | ||
/** @var ShortUrlInterface */ | ||
private $shortUrl; | ||
|
||
/** | ||
* ShortUrlCreatedEvent constructor. | ||
* @param ShortUrlInterface $shortUrl | ||
*/ | ||
public function __construct(ShortUrlInterface $shortUrl) | ||
{ | ||
$this->shortUrl = $shortUrl; | ||
} | ||
|
||
/** | ||
* @return ShortUrlInterface | ||
*/ | ||
public function getShortUrl() | ||
{ | ||
return $this->shortUrl; | ||
} | ||
} |
Oops, something went wrong.