-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShortenerCommand.php
55 lines (42 loc) · 1.67 KB
/
ShortenerCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
declare(strict_types=1);
namespace LaSalle\UrlShortener\JudithVilela\UrlShortened\Infrastructure\Ui\Cli;
use LaSalle\UrlShortener\JudithVilela\UrlShortened\Application\UrlShortener\UrlShortener;
use LaSalle\UrlShortener\JudithVilela\UrlShortened\Application\UrlShortener\UrlShortenerRequest;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Config\Definition\Exception\Exception;
final class ShortenerCommand extends Command
{
protected static $defaultName = 'app:url-shortener';
/** @var UrlShortener */
private $urlShortener;
public function __construct(UrlShortener $urlShortener)
{
parent::__construct();
$this->urlShortener = $urlShortener;
}
protected function configure()
{
$this->setDescription('Given a url return a url shortened by Bitly');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$helper = $this->getHelper('question');
$question = new Question('Please, input url:');
$long_url = $helper->ask($input, $output, $question);
if (empty($long_url)) {
throw new Exception('URL empty');
}
try {
$urlResponse = $this->urlShortener->__invoke(new UrlShortenerRequest($long_url));
$urlShortened = $urlResponse->urlShort();
dump($urlShortened);
return 0;
} catch (\Throwable $exception) {
throw new Exception('Error shortening url '. $exception->getMessage());
}
}
}