Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Views / DisableCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Views\DisableCommand.
6  */
7
8 namespace Drupal\Console\Command\Views;
9
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Drupal\Console\Core\Command\Command;
14 use Drupal\Core\Entity\EntityTypeManagerInterface;
15 use Drupal\Core\Entity\Query\QueryFactory;
16
17 /**
18  * Class DisableCommand
19  *
20  * @package Drupal\Console\Command\Views
21  */
22 class DisableCommand extends Command
23 {
24     /**
25      * @var EntityTypeManagerInterface
26      */
27     protected $entityTypeManager;
28
29     /**
30      * @var QueryFactory
31      */
32     protected $entityQuery;
33
34     /**
35      * DisableCommand constructor.
36      *
37      * @param EntityTypeManagerInterface $entityTypeManager
38      * @param QueryFactory               $entityQuery
39      */
40     public function __construct(
41         EntityTypeManagerInterface $entityTypeManager,
42         QueryFactory $entityQuery
43     ) {
44         $this->entityTypeManager = $entityTypeManager;
45         $this->entityQuery = $entityQuery;
46         parent::__construct();
47     }
48
49     /**
50      * {@inheritdoc}
51      */
52     protected function configure()
53     {
54         $this
55             ->setName('views:disable')
56             ->setDescription($this->trans('commands.views.disable.description'))
57             ->addArgument(
58                 'view-id',
59                 InputArgument::OPTIONAL,
60                 $this->trans('commands.debug.views.arguments.view-id')
61             )
62             ->setAliases(['vd']);
63     }
64
65     /**
66      * {@inheritdoc}
67      */
68     protected function interact(InputInterface $input, OutputInterface $output)
69     {
70         $viewId = $input->getArgument('view-id');
71         if (!$viewId) {
72             $views = $this->entityQuery
73                 ->get('view')
74                 ->condition('status', 1)
75                 ->execute();
76             $viewId = $this->getIo()->choiceNoList(
77                 $this->trans('commands.debug.views.arguments.view-id'),
78                 $views
79             );
80             $input->setArgument('view-id', $viewId);
81         }
82     }
83
84     /**
85      * {@inheritdoc}
86      */
87     protected function execute(InputInterface $input, OutputInterface $output)
88     {
89         $viewId = $input->getArgument('view-id');
90
91         $view = $this->entityTypeManager->getStorage('view')->load($viewId);
92
93         if (empty($view)) {
94             $this->getIo()->error(sprintf($this->trans('commands.debug.views.messages.not-found'), $viewId));
95
96             return 1;
97         }
98
99         try {
100             $view->disable()->save();
101
102             $this->getIo()->success(sprintf($this->trans('commands.views.disable.messages.disabled-successfully'), $view->get('label')));
103         } catch (\Exception $e) {
104             $this->getIo()->error($e->getMessage());
105
106             return 1;
107         }
108
109         return 0;
110     }
111 }