-
-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2164 from acelaya-forks/feature/update-url-cli
Add command to update short URLs
- Loading branch information
Showing
9 changed files
with
312 additions
and
84 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
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
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
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
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
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,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shlinkio\Shlink\CLI\Command\ShortUrl; | ||
|
||
use Shlinkio\Shlink\CLI\Input\ShortUrlDataInput; | ||
use Shlinkio\Shlink\CLI\Input\ShortUrlIdentifierInput; | ||
use Shlinkio\Shlink\CLI\Util\ExitCode; | ||
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException; | ||
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifierInterface; | ||
use Shlinkio\Shlink\Core\ShortUrl\ShortUrlServiceInterface; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
use function sprintf; | ||
|
||
class EditShortUrlCommand extends Command | ||
{ | ||
public const NAME = 'short-url:edit'; | ||
|
||
private readonly ShortUrlDataInput $shortUrlDataInput; | ||
private readonly ShortUrlIdentifierInput $shortUrlIdentifierInput; | ||
|
||
public function __construct( | ||
private readonly ShortUrlServiceInterface $shortUrlService, | ||
private readonly ShortUrlStringifierInterface $stringifier, | ||
) { | ||
parent::__construct(); | ||
|
||
$this->shortUrlDataInput = new ShortUrlDataInput($this, longUrlAsOption: true); | ||
$this->shortUrlIdentifierInput = new ShortUrlIdentifierInput( | ||
$this, | ||
shortCodeDesc: 'The short code to edit', | ||
domainDesc: 'The domain to which the short URL is attached.', | ||
); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->setName(self::NAME) | ||
->setDescription('Edit an existing short URL'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
$identifier = $this->shortUrlIdentifierInput->toShortUrlIdentifier($input); | ||
|
||
try { | ||
$shortUrl = $this->shortUrlService->updateShortUrl( | ||
$identifier, | ||
$this->shortUrlDataInput->toShortUrlEdition($input), | ||
); | ||
|
||
$io->success(sprintf('Short URL "%s" properly edited', $this->stringifier->stringify($shortUrl))); | ||
return ExitCode::EXIT_SUCCESS; | ||
} catch (ShortUrlNotFoundException $e) { | ||
$io->error(sprintf('Short URL not found for "%s"', $identifier->__toString())); | ||
|
||
if ($io->isVerbose()) { | ||
$this->getApplication()?->renderThrowable($e, $io); | ||
} | ||
|
||
return ExitCode::EXIT_FAILURE; | ||
} | ||
} | ||
} |
Oops, something went wrong.