Completed
Pull Request — master (#199)
by Alejandro
03:59
created

DeleteShortCodeCommand::runDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\CLI\Command\Shortcode;
5
6
use Shlinkio\Shlink\Core\Exception;
7
use Shlinkio\Shlink\Core\Service\ShortUrl\DeleteShortUrlServiceInterface;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Style\SymfonyStyle;
14
use Zend\I18n\Translator\TranslatorInterface;
15
16
class DeleteShortCodeCommand extends Command
17
{
18
    public const NAME = 'short-code:delete';
19
    private const ALIASES = [];
20
21
    /**
22
     * @var DeleteShortUrlServiceInterface
23
     */
24
    private $deleteShortUrlService;
25
    /**
26
     * @var TranslatorInterface
27
     */
28
    private $translator;
29
30 4
    public function __construct(DeleteShortUrlServiceInterface $deleteShortUrlService, TranslatorInterface $translator)
31
    {
32 4
        $this->deleteShortUrlService = $deleteShortUrlService;
33 4
        $this->translator = $translator;
34 4
        parent::__construct();
35 4
    }
36
37 4
    protected function configure(): void
38
    {
39
        $this
40 4
            ->setName(self::NAME)
41 4
            ->setAliases(self::ALIASES)
42 4
            ->setDescription(
43 4
                $this->translator->translate('Deletes a short URL')
44
            )
45 4
            ->addArgument(
46 4
                'shortCode',
47 4
                InputArgument::REQUIRED,
48 4
                $this->translator->translate('The short code to be deleted')
49
            )
50 4
            ->addOption(
51 4
                'ignore-threshold',
52 4
                'i',
53 4
                InputOption::VALUE_NONE,
54 4
                $this->translator->translate(
55
                    'Ignores the safety visits threshold check, which could make short URLs with many visits to be '
56 4
                    . 'accidentally deleted'
57
                )
58
            );
59 4
    }
60
61 4
    protected function execute(InputInterface $input, OutputInterface $output): void
62
    {
63 4
        $io = new SymfonyStyle($input, $output);
64 4
        $shortCode = $input->getArgument('shortCode');
65 4
        $ignoreThreshold = $input->getOption('ignore-threshold');
66
67
        try {
68 4
            $this->runDelete($io, $shortCode, $ignoreThreshold);
69 3
        } catch (Exception\InvalidShortCodeException $e) {
70 1
            $io->error(
71 1
                \sprintf($this->translator->translate('Provided short code "%s" could not be found.'), $shortCode)
72
            );
73 2
        } catch (Exception\DeleteShortUrlException $e) {
74 2
            $this->retry($io, $shortCode, $e);
75
        }
76 4
    }
77
78 2
    private function retry(SymfonyStyle $io, string $shortCode, Exception\DeleteShortUrlException $e): void
79
    {
80 2
        $warningMsg = \sprintf($this->translator->translate(
81 2
            'It was not possible to delete the short URL with short code "%s" because it has more than %s visits.'
82 2
        ), $shortCode, $e->getVisitsThreshold());
83 2
        $io->writeln('<bg=yellow>' . $warningMsg . '</>');
84 2
        $forceDelete = $io->confirm($this->translator->translate('Do you want to delete it anyway?'), false);
85
86 2
        if ($forceDelete) {
87 1
            $this->runDelete($io, $shortCode, true);
88
        } else {
89 1
            $io->warning($this->translator->translate('Short URL was not deleted.'));
90
        }
91 2
    }
92
93 4
    private function runDelete(SymfonyStyle $io, string $shortCode, bool $ignoreThreshold): void
94
    {
95 4
        $this->deleteShortUrlService->deleteByShortCode($shortCode, $ignoreThreshold);
96 2
        $io->success(\sprintf(
97 2
            $this->translator->translate('Short URL with short code "%s" successfully deleted.'),
98 2
            $shortCode
99
        ));
100 2
    }
101
}
102