Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Entity / DeleteCommand.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\Console\Command\Entity\DeleteCommand.
5  */
6
7 namespace Drupal\Console\Command\Entity;
8
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 Drupal\Console\Core\Command\Command;
14 use Drupal\Core\Entity\EntityTypeRepository;
15 use Drupal\Core\Entity\EntityTypeManagerInterface;
16
17 class DeleteCommand extends Command
18 {
19     /**
20      * @var EntityTypeRepository
21      */
22     protected $entityTypeRepository;
23
24     /**
25      * @var EntityTypeManagerInterface
26      */
27     protected $entityTypeManager;
28
29     /**
30      * DeleteCommand constructor.
31      *
32      * @param EntityTypeRepository       $entityTypeRepository
33      * @param EntityTypeManagerInterface $entityTypeManager
34      */
35     public function __construct(
36         EntityTypeRepository $entityTypeRepository,
37         EntityTypeManagerInterface $entityTypeManager
38     ) {
39         $this->entityTypeRepository = $entityTypeRepository;
40         $this->entityTypeManager = $entityTypeManager;
41         parent::__construct();
42     }
43     /**
44      * {@inheritdoc}
45      */
46     protected function configure()
47     {
48         $this
49             ->setName('entity:delete')
50             ->setDescription($this->trans('commands.entity.delete.description'))
51             ->addOption(
52                 'all',
53                 null,
54                 InputOption::VALUE_NONE,
55                 $this->trans('commands.entity.delete.options.all')
56             )
57             ->addArgument(
58                 'entity-definition-id',
59                 InputArgument::REQUIRED,
60                 $this->trans('commands.entity.delete.arguments.entity-definition-id')
61             )
62             ->addArgument(
63                 'entity-id',
64                 InputArgument::REQUIRED,
65                 $this->trans('commands.entity.delete.arguments.entity-id')
66             )->setAliases(['ed']);
67     }
68
69     /**
70      * {@inheritdoc}
71      */
72     protected function interact(InputInterface $input, OutputInterface $output)
73     {
74         $entityDefinitionID = $input->getArgument('entity-definition-id');
75         $entityID = $input->getArgument('entity-id');
76         $all = $input->getOption('all');
77
78         if (!$entityDefinitionID) {
79             $entityTypes = $this->entityTypeRepository->getEntityTypeLabels(true);
80
81             $entityType = $this->getIo()->choice(
82                 $this->trans('commands.entity.delete.questions.entity-type'),
83                 array_keys($entityTypes)
84             );
85
86             $entityDefinitionID = $this->getIo()->choice(
87                 $this->trans('commands.entity.delete.questions.entity-definition-id'),
88                 array_keys($entityTypes[$entityType])
89             );
90
91             $input->setArgument('entity-definition-id', $entityDefinitionID);
92         }
93
94         if ($all) {
95             $input->setArgument('entity-id', '-');
96         } elseif (!$entityID) {
97             $entityID = $this->getIo()->ask(
98                 $this->trans('commands.entity.delete.questions.entity-id')
99             );
100             $input->setArgument('entity-id', $entityID);
101         }
102     }
103
104     /**
105      * {@inheritdoc}
106      */
107     protected function execute(InputInterface $input, OutputInterface $output)
108     {
109         $entityDefinitionID = $input->getArgument('entity-definition-id');
110
111         try {
112             $storage = $this->entityTypeManager->getStorage($entityDefinitionID);
113
114             if ($input->getOption('all')) {
115                 $entities = $storage->loadMultiple();
116                 if ($this->getIo()->confirm(
117                     sprintf(
118                         $this->trans('commands.entity.delete.messages.confirm-delete-all'),
119                         $entityDefinitionID,
120                         count($entities)
121                     )
122                 )
123                 ) {
124                     $storage->delete($entities);
125                     $this->getIo()->success(
126                         sprintf(
127                             $this->trans('commands.entity.delete.messages.deleted-all'),
128                             $entityDefinitionID,
129                             count($entities)
130                         )
131                     );
132                 }
133             } else {
134                 $entityID = $input->getArgument('entity-id');
135                 $storage->load($entityID)->delete();
136                 $this->getIo()->success(
137                     sprintf(
138                         $this->trans('commands.entity.delete.messages.deleted'),
139                         $entityDefinitionID,
140                         $entityID
141                     )
142                 );
143             }
144         } catch (\Exception $e) {
145             $this->getIo()->error($e->getMessage());
146
147             return 1;
148         }
149     }
150 }