Merge pull request #17 from acelaya/feature/10

feature/10
This commit is contained in:
Alejandro Celaya 2016-07-06 20:33:04 +02:00 committed by GitHub
commit e3ad9eb03a
11 changed files with 334 additions and 16 deletions

13
CHANGELOG.md Normal file
View file

@ -0,0 +1,13 @@
## CHANGELOG
### 0.2.0
**Enhancements:**
* [9: Use symfony/console to dispatch console requests, instead of trying to integrate the process with expressive](https://github.com/acelaya/url-shortener/issues/9)
* [8: Create a REST API](https://github.com/acelaya/url-shortener/issues/8)
* [10: Add more CLI functionality](https://github.com/acelaya/url-shortener/issues/10)
**Tasks**
* [5: Create CHANGELOG file](https://github.com/acelaya/url-shortener/issues/5)

View file

@ -1,14 +1,10 @@
#!/usr/bin/env php
<?php
use Acelaya\UrlShortener\CliCommands\GenerateShortcodeCommand;
use Interop\Container\ContainerInterface;
use Symfony\Component\Console\Application as CliApp;
/** @var ContainerInterface $container */
$container = include __DIR__ . '/../config/container.php';
$app = new CliApp();
$app->addCommands([
$container->get(GenerateShortcodeCommand::class),
]);
$app = $container->get(CliApp::class);
$app->run();

View file

@ -0,0 +1,15 @@
<?php
use Acelaya\UrlShortener\CLI\Command;
return [
'cli' => [
'commands' => [
Command\GenerateShortcodeCommand::class,
Command\ResolveUrlCommand::class,
Command\ListShortcodesCommand::class,
Command\GetVisitsCommand::class,
]
],
];

View file

@ -1,5 +1,5 @@
<?php
use Acelaya\UrlShortener\CliCommands;
use Acelaya\UrlShortener\CLI;
use Acelaya\UrlShortener\Factory\CacheFactory;
use Acelaya\UrlShortener\Factory\EntityManagerFactory;
use Acelaya\UrlShortener\Middleware;
@ -7,7 +7,8 @@ use Acelaya\UrlShortener\Service;
use Acelaya\ZsmAnnotatedServices\Factory\V3\AnnotatedFactory;
use Doctrine\Common\Cache\Cache;
use Doctrine\ORM\EntityManager;
use Zend\Expressive\Application;
use Symfony\Component\Console;
use Zend\Expressive;
use Zend\Expressive\Container;
use Zend\Expressive\Helper;
use Zend\Expressive\Router;
@ -19,7 +20,8 @@ return [
'services' => [
'factories' => [
Application::class => Container\ApplicationFactory::class,
Expressive\Application::class => Container\ApplicationFactory::class,
Console\Application::class => CLI\Factory\ApplicationFactory::class,
// Url helpers
Helper\UrlHelper::class => Helper\UrlHelperFactory::class,
@ -42,7 +44,10 @@ return [
Cache::class => CacheFactory::class,
// Cli commands
CliCommands\GenerateShortcodeCommand::class => AnnotatedFactory::class,
CLI\Command\GenerateShortcodeCommand::class => AnnotatedFactory::class,
CLI\Command\ResolveUrlCommand::class => AnnotatedFactory::class,
CLI\Command\ListShortcodesCommand::class => AnnotatedFactory::class,
CLI\Command\GetVisitsCommand::class => AnnotatedFactory::class,
// Middleware
Middleware\Routable\RedirectMiddleware::class => AnnotatedFactory::class,

View file

@ -1,5 +1,5 @@
<?php
namespace Acelaya\UrlShortener\CliCommands;
namespace Acelaya\UrlShortener\CLI\Command;
use Acelaya\UrlShortener\Exception\InvalidUrlException;
use Acelaya\UrlShortener\Service\UrlShortener;
@ -41,7 +41,7 @@ class GenerateShortcodeCommand extends Command
public function configure()
{
$this->setName('generate-shortcode')
$this->setName('shortcode:generate')
->setDescription('Generates a shortcode for provided URL and returns the short URL')
->addArgument('longUrl', InputArgument::REQUIRED, 'The long URL to parse');
}
@ -88,8 +88,6 @@ class GenerateShortcodeCommand extends Command
$output->writeln(
sprintf('<error>Provided URL "%s" is invalid. Try with a different one.</error>', $longUrl)
);
} catch (\Exception $e) {
$output->writeln('<error>' . $e . '</error>');
}
}
}

View file

@ -0,0 +1,77 @@
<?php
namespace Acelaya\UrlShortener\CLI\Command;
use Acelaya\UrlShortener\Service\VisitsTracker;
use Acelaya\UrlShortener\Service\VisitsTrackerInterface;
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
class GetVisitsCommand extends Command
{
/**
* @var VisitsTrackerInterface
*/
private $visitsTracker;
/**
* GetVisitsCommand constructor.
* @param VisitsTrackerInterface|VisitsTracker $visitsTracker
*
* @Inject({VisitsTracker::class})
*/
public function __construct(VisitsTrackerInterface $visitsTracker)
{
parent::__construct(null);
$this->visitsTracker = $visitsTracker;
}
public function configure()
{
$this->setName('shortcode:visits')
->setDescription('Returns the detailed visits information for provided short code')
->addArgument('shortCode', InputArgument::REQUIRED, 'The short code which visits we want to get');
}
public function interact(InputInterface $input, OutputInterface $output)
{
$shortCode = $input->getArgument('shortCode');
if (! empty($shortCode)) {
return;
}
/** @var QuestionHelper $helper */
$helper = $this->getHelper('question');
$question = new Question(
'<question>A short code was not provided. Which short code do you want to use?:</question> '
);
$shortCode = $helper->ask($input, $output, $question);
if (! empty($shortCode)) {
$input->setArgument('shortCode', $shortCode);
}
}
public function execute(InputInterface $input, OutputInterface $output)
{
$shortCode = $input->getArgument('shortCode');
$visits = $this->visitsTracker->info($shortCode);
$table = new Table($output);
$table->setHeaders([
'Referer',
'Date',
'Temote Address',
'User agent',
]);
foreach ($visits as $row) {
$table->addRow(array_values($row->jsonSerialize()));
}
$table->render();
}
}

View file

@ -0,0 +1,84 @@
<?php
namespace Acelaya\UrlShortener\CLI\Command;
use Acelaya\UrlShortener\Paginator\Adapter\PaginableRepositoryAdapter;
use Acelaya\UrlShortener\Paginator\Util\PaginatorUtilsTrait;
use Acelaya\UrlShortener\Service\ShortUrlService;
use Acelaya\UrlShortener\Service\ShortUrlServiceInterface;
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
class ListShortcodesCommand extends Command
{
use PaginatorUtilsTrait;
/**
* @var ShortUrlServiceInterface
*/
private $shortUrlService;
/**
* ListShortcodesCommand constructor.
* @param ShortUrlServiceInterface|ShortUrlService $shortUrlService
*
* @Inject({ShortUrlService::class})
*/
public function __construct(ShortUrlServiceInterface $shortUrlService)
{
parent::__construct(null);
$this->shortUrlService = $shortUrlService;
}
public function configure()
{
$this->setName('shortcode:list')
->setDescription('List all short URLs')
->addOption(
'page',
'p',
InputOption::VALUE_OPTIONAL,
sprintf('The first page to list (%s items per page)', PaginableRepositoryAdapter::ITEMS_PER_PAGE),
1
);
}
public function execute(InputInterface $input, OutputInterface $output)
{
$page = intval($input->getOption('page'));
/** @var QuestionHelper $helper */
$helper = $this->getHelper('question');
do {
$result = $this->shortUrlService->listShortUrls($page);
$page++;
$table = new Table($output);
$table->setHeaders([
'Short code',
'Original URL',
'Date created',
'Visits count',
]);
foreach ($result as $row) {
$table->addRow(array_values($row->jsonSerialize()));
}
$table->render();
if ($this->isLastPage($result)) {
$continue = false;
$output->writeln('<info>You have reached last page</info>');
} else {
$continue = $helper->ask($input, $output, new ConfirmationQuestion(
sprintf('<question>Continue with page <bg=cyan;options=bold>%s</>? (y/N)</question> ', $page),
false
));
}
} while ($continue);
}
}

View file

@ -0,0 +1,78 @@
<?php
namespace Acelaya\UrlShortener\CLI\Command;
use Acelaya\UrlShortener\Exception\InvalidShortCodeException;
use Acelaya\UrlShortener\Service\UrlShortener;
use Acelaya\UrlShortener\Service\UrlShortenerInterface;
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
class ResolveUrlCommand extends Command
{
/**
* @var UrlShortenerInterface
*/
private $urlShortener;
/**
* ResolveUrlCommand constructor.
* @param UrlShortenerInterface|UrlShortener $urlShortener
*
* @Inject({UrlShortener::class})
*/
public function __construct(UrlShortenerInterface $urlShortener)
{
parent::__construct(null);
$this->urlShortener = $urlShortener;
}
public function configure()
{
$this->setName('shortcode:parse')
->setDescription('Returns the long URL behind a short code')
->addArgument('shortCode', InputArgument::REQUIRED, 'The short code to parse');
}
public function interact(InputInterface $input, OutputInterface $output)
{
$shortCode = $input->getArgument('shortCode');
if (! empty($shortCode)) {
return;
}
/** @var QuestionHelper $helper */
$helper = $this->getHelper('question');
$question = new Question(
'<question>A short code was not provided. Which short code do you want to parse?:</question> '
);
$shortCode = $helper->ask($input, $output, $question);
if (! empty($shortCode)) {
$input->setArgument('shortCode', $shortCode);
}
}
public function execute(InputInterface $input, OutputInterface $output)
{
$shortCode = $input->getArgument('shortCode');
try {
$longUrl = $this->urlShortener->shortCodeToUrl($shortCode);
if (! isset($longUrl)) {
$output->writeln(sprintf('<error>No URL found for short code "%s"</error>', $shortCode));
return;
}
$output->writeln(sprintf('Long URL <info>%s</info>', $longUrl));
} catch (InvalidShortCodeException $e) {
$output->writeln(
sprintf('<error>Provided short code "%s" has an invalid format.</error>', $shortCode)
);
}
}
}

View file

@ -0,0 +1,41 @@
<?php
namespace Acelaya\UrlShortener\CLI\Factory;
use Interop\Container\ContainerInterface;
use Interop\Container\Exception\ContainerException;
use Symfony\Component\Console\Application as CliApp;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\Factory\FactoryInterface;
class ApplicationFactory implements FactoryInterface
{
/**
* Create an object
*
* @param ContainerInterface $container
* @param string $requestedName
* @param null|array $options
* @return object
* @throws ServiceNotFoundException if unable to resolve the service.
* @throws ServiceNotCreatedException if an exception is raised when
* creating a service.
* @throws ContainerException if any other error occurs
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$config = $container->get('config')['cli'];
$app = new CliApp();
$commands = isset($config['commands']) ? $config['commands'] : [];
foreach ($commands as $command) {
if (! $container->has($command)) {
continue;
}
$app->add($container->get($command));
}
return $app;
}
}

View file

@ -1,7 +1,7 @@
<?php
namespace Acelaya\UrlShortener\Middleware\Rest;
use Acelaya\UrlShortener\Paginator\Util\PaginatorSerializerTrait;
use Acelaya\UrlShortener\Paginator\Util\PaginatorUtilsTrait;
use Acelaya\UrlShortener\Service\ShortUrlService;
use Acelaya\UrlShortener\Service\ShortUrlServiceInterface;
use Acelaya\UrlShortener\Util\RestUtils;
@ -13,7 +13,7 @@ use Zend\Stdlib\ArrayUtils;
class ListShortcodesMiddleware extends AbstractRestMiddleware
{
use PaginatorSerializerTrait;
use PaginatorUtilsTrait;
/**
* @var ShortUrlServiceInterface

View file

@ -4,7 +4,7 @@ namespace Acelaya\UrlShortener\Paginator\Util;
use Zend\Paginator\Paginator;
use Zend\Stdlib\ArrayUtils;
trait PaginatorSerializerTrait
trait PaginatorUtilsTrait
{
protected function serializePaginator(Paginator $paginator)
{
@ -16,4 +16,15 @@ trait PaginatorSerializerTrait
],
];
}
/**
* Checks if provided paginator is in last page
*
* @param Paginator $paginator
* @return bool
*/
protected function isLastPage(Paginator $paginator)
{
return $paginator->getCurrentPageNumber() >= $paginator->count();
}
}