895db67d6f352618ea91ca407e15c239910fe905
[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\Output\OutputInterface;
12 use Symfony\Component\Console\Command\Command;
13 use Drupal\Core\Entity\EntityTypeRepository;
14 use Drupal\Core\Entity\EntityTypeManagerInterface;
15 use Drupal\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Console\Core\Style\DrupalStyle;
17
18 class DeleteCommand extends Command
19 {
20     use CommandTrait;
21
22     /**
23      * @var EntityTypeRepository
24      */
25     protected $entityTypeRepository;
26
27     /**
28      * @var EntityTypeManagerInterface
29      */
30     protected $entityTypeManager;
31
32     /**
33      * DeleteCommand constructor.
34      *
35      * @param EntityTypeRepository       $entityTypeRepository
36      * @param EntityTypeManagerInterface $entityTypeManager
37      */
38     public function __construct(
39         EntityTypeRepository $entityTypeRepository,
40         EntityTypeManagerInterface $entityTypeManager
41     ) {
42         $this->entityTypeRepository = $entityTypeRepository;
43         $this->entityTypeManager = $entityTypeManager;
44         parent::__construct();
45     }
46     /**
47      * {@inheritdoc}
48      */
49     protected function configure()
50     {
51         $this
52             ->setName('entity:delete')
53             ->setDescription($this->trans('commands.entity.delete.description'))
54             ->addArgument(
55                 'entity-definition-id',
56                 InputArgument::REQUIRED,
57                 $this->trans('commands.entity.delete.arguments.entity-definition-id')
58             )
59             ->addArgument(
60                 'entity-id',
61                 InputArgument::REQUIRED,
62                 $this->trans('commands.entity.delete.arguments.entity-id')
63             );
64     }
65
66     /**
67      * {@inheritdoc}
68      */
69     protected function interact(InputInterface $input, OutputInterface $output)
70     {
71         $io = new DrupalStyle($input, $output);
72         $entityDefinitionID = $input->getArgument('entity-definition-id');
73         $entityID = $input->getArgument('entity-id');
74
75         if (!$entityDefinitionID) {
76             $entityTypes = $this->entityTypeRepository->getEntityTypeLabels(true);
77
78             $entityType = $io->choice(
79                 $this->trans('commands.entity.delete.questions.entity-type'),
80                 array_keys($entityTypes)
81             );
82
83             $entityDefinitionID = $io->choice(
84                 $this->trans('commands.entity.delete.questions.entity-definition-id'),
85                 array_keys($entityTypes[$entityType])
86             );
87
88             $input->setArgument('entity-definition-id', $entityDefinitionID);
89         }
90
91         if (!$entityID) {
92             $entityID = $io->ask(
93                 $this->trans('commands.entity.delete.questions.entity-id')
94             );
95             $input->setArgument('entity-id', $entityID);
96         }
97     }
98
99     /**
100      * {@inheritdoc}
101      */
102     protected function execute(InputInterface $input, OutputInterface $output)
103     {
104         $io = new DrupalStyle($input, $output);
105
106         $entityDefinitionID = $input->getArgument('entity-definition-id');
107         $entityID = $input->getArgument('entity-id');
108
109         try {
110             $this->entityTypeManager->getStorage($entityDefinitionID)->load($entityID)->delete();
111         } catch (\Exception $e) {
112             $io->error($e->getMessage());
113
114             return 1;
115         }
116
117         $io->success(
118             sprintf(
119                 $this->trans('commands.entity.delete.messages.deleted'),
120                 $entityDefinitionID,
121                 $entityID
122             )
123         );
124     }
125 }