1 | <?php |
||||
2 | |||||
3 | /* |
||||
4 | * This file is part of the SexyField package. |
||||
5 | * |
||||
6 | * (c) Dion Snoeijen <[email protected]> |
||||
7 | * |
||||
8 | * For the full copyright and license information, please view the LICENSE |
||||
9 | * file that was distributed with this source code. |
||||
10 | */ |
||||
11 | |||||
12 | declare (strict_types = 1); |
||||
13 | |||||
14 | namespace Tardigrades\Command; |
||||
15 | |||||
16 | use Symfony\Component\Console\Helper\QuestionHelper; |
||||
17 | use Symfony\Component\Console\Input\InputInterface; |
||||
18 | use Symfony\Component\Console\Output\OutputInterface; |
||||
19 | use Symfony\Component\Console\Question\ConfirmationQuestion; |
||||
20 | use Symfony\Component\Console\Question\Question; |
||||
21 | use Tardigrades\Entity\ApplicationInterface; |
||||
22 | use Tardigrades\SectionField\Service\ApplicationManagerInterface; |
||||
23 | use Tardigrades\SectionField\Service\ApplicationNotFoundException; |
||||
24 | use Tardigrades\SectionField\ValueObject\Id; |
||||
25 | |||||
26 | class DeleteApplicationCommand extends ApplicationCommand |
||||
27 | { |
||||
28 | /** @var ApplicationManagerInterface */ |
||||
29 | private $applicationManager; |
||||
30 | |||||
31 | /** @var QuestionHelper */ |
||||
32 | private $questionHelper; |
||||
33 | |||||
34 | public function __construct( |
||||
35 | ApplicationManagerInterface $applicationManager |
||||
36 | ) { |
||||
37 | $this->applicationManager = $applicationManager; |
||||
38 | |||||
39 | parent::__construct('sf:delete-application'); |
||||
40 | } |
||||
41 | |||||
42 | protected function configure() |
||||
43 | { |
||||
44 | $this |
||||
45 | ->setDescription('Delete application') |
||||
46 | ->setHelp('Shows a list of installed applications, choose the application you would like to delete.'); |
||||
47 | } |
||||
48 | |||||
49 | protected function execute(InputInterface $input, OutputInterface $output) |
||||
50 | { |
||||
51 | $this->questionHelper = $this->getHelper('question'); |
||||
52 | |||||
53 | try { |
||||
54 | $this->showInstalledApplications($input, $output); |
||||
55 | } catch (ApplicationNotFoundException $exception) { |
||||
56 | $output->writeln("Application not found."); |
||||
57 | } |
||||
58 | } |
||||
59 | |||||
60 | private function showInstalledApplications(InputInterface $input, OutputInterface $output): void |
||||
61 | { |
||||
62 | $appliactions = $this->applicationManager->readAll(); |
||||
63 | |||||
64 | $this->renderTable($output, $appliactions, 'All installed Applications'); |
||||
65 | $this->deleteWhatRecord($input, $output); |
||||
66 | } |
||||
67 | |||||
68 | private function getApplicationRecord(InputInterface $input, OutputInterface $output): ?ApplicationInterface |
||||
69 | { |
||||
70 | $question = new Question('<question>What record do you want to delete?</question> (#id): '); |
||||
71 | $question->setValidator(function ($id) { |
||||
72 | try { |
||||
73 | return $this->applicationManager->read(Id::fromInt((int) $id)); |
||||
74 | } catch (ApplicationNotFoundException $exception) { |
||||
75 | return null; |
||||
76 | } |
||||
77 | }); |
||||
78 | |||||
79 | $applicationRecord = $this->questionHelper->ask($input, $output, $question); |
||||
80 | if (!$applicationRecord) { |
||||
81 | throw new ApplicationNotFoundException(); |
||||
82 | } |
||||
83 | return $applicationRecord; |
||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||
84 | } |
||||
85 | |||||
86 | private function deleteWhatRecord(InputInterface $input, OutputInterface $output): void |
||||
87 | { |
||||
88 | $application = $this->getApplicationRecord($input, $output); |
||||
89 | |||||
90 | $output->writeln('<info>Record with id #' . $application->getId() . ' will be deleted</info>'); |
||||
91 | |||||
92 | $sure = new ConfirmationQuestion('<comment>Are you sure?</comment> (y/n) ', false); |
||||
93 | |||||
94 | if (!$this->questionHelper->ask($input, $output, $sure)) { |
||||
95 | $output->writeln('<comment>Cancelled, nothing deleted.</comment>'); |
||||
96 | return; |
||||
97 | } |
||||
98 | $this->applicationManager->delete($application); |
||||
0 ignored issues
–
show
It seems like
$application can also be of type null ; however, parameter $entity of Tardigrades\SectionField...agerInterface::delete() does only seem to accept Tardigrades\Entity\ApplicationInterface , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
99 | |||||
100 | $output->writeln('<info>Removed!</info>'); |
||||
101 | } |
||||
102 | } |
||||
103 |