Version 1
[yaffs-website] / vendor / drupal / console / src / Command / State / DeleteCommand.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\Console\Command\Config\DeleteCommand.
5  */
6
7 namespace Drupal\Console\Command\State;
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\KeyValueStore\KeyValueFactoryInterface;
14 use Drupal\Core\State\StateInterface;
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 StateInterface
24      */
25     protected $state;
26
27     /**
28      * @var KeyValueFactoryInterface
29      */
30     protected $keyValue;
31
32     /**
33      * DeleteCommand constructor.
34      *
35      * @param StateInterface           $state
36      * @param KeyValueFactoryInterface $keyValue
37      */
38     public function __construct(
39         StateInterface $state,
40         KeyValueFactoryInterface $keyValue
41     ) {
42         $this->state = $state;
43         $this->keyValue = $keyValue;
44         parent::__construct();
45     }
46
47     /**
48      * {@inheritdoc}
49      */
50     protected function configure()
51     {
52         $this
53             ->setName('state:delete')
54             ->setDescription($this->trans('commands.state.delete.description'))
55             ->addArgument(
56                 'name',
57                 InputArgument::OPTIONAL,
58                 $this->trans('commands.state.delete.arguments.name')
59             );
60     }
61
62     /**
63      * {@inheritdoc}
64      */
65     protected function interact(InputInterface $input, OutputInterface $output)
66     {
67         $io = new DrupalStyle($input, $output);
68         $name = $input->getArgument('name');
69         if (!$name) {
70             $names = array_keys($this->keyValue->get('state')->getAll());
71             $name = $io->choiceNoList(
72                 $this->trans('commands.state.delete.arguments.name'),
73                 $names
74             );
75             $input->setArgument('name', $name);
76         }
77     }
78
79     /**
80      * {@inheritdoc}
81      */
82     protected function execute(InputInterface $input, OutputInterface $output)
83     {
84         $io = new DrupalStyle($input, $output);
85         $name = $input->getArgument('name');
86         if (!$name) {
87             $io->error($this->trans('commands.state.delete.messages.enter-name'));
88
89             return 1;
90         }
91
92         if (!$this->state->get($name)) {
93             $io->error(
94                 sprintf(
95                     $this->trans('commands.state.delete.messages.state-not-exists'),
96                     $name
97                 )
98             );
99
100             return 1;
101         }
102
103         try {
104             $this->state->delete($name);
105         } catch (\Exception $e) {
106             $io->error($e->getMessage());
107
108             return 1;
109         }
110
111         $io->success(
112             sprintf(
113                 $this->trans('commands.state.delete.messages.deleted'),
114                 $name
115             )
116         );
117
118         return 0;
119     }
120 }