Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Database / LogClearCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Database\LogClearCommand.
6  */
7
8 namespace Drupal\Console\Command\Database;
9
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Symfony\Component\Console\Command\Command;
15 use Drupal\Core\Database\Connection;
16 use Drupal\Console\Core\Command\Shared\CommandTrait;
17 use Drupal\Core\Logger\RfcLogLevel;
18 use Drupal\Console\Core\Style\DrupalStyle;
19
20 class LogClearCommand extends Command
21 {
22     use CommandTrait;
23
24     /**
25      * @var Connection
26      */
27     protected $database;
28
29     /**
30      * LogClearCommand constructor.
31      *
32      * @param Connection $database
33      */
34     public function __construct(Connection $database)
35     {
36         $this->database = $database;
37         parent::__construct();
38     }
39
40     /**
41      * {@inheritdoc}
42      */
43     protected function configure()
44     {
45         $this
46             ->setName('database:log:clear')
47             ->setDescription($this->trans('commands.database.log.clear.description'))
48             ->addArgument(
49                 'event-id',
50                 InputArgument::OPTIONAL,
51                 $this->trans('commands.database.log.clear.arguments.event-id')
52             )
53             ->addOption(
54                 'type',
55                 null,
56                 InputOption::VALUE_OPTIONAL,
57                 $this->trans('commands.database.log.clear.options.type')
58             )
59             ->addOption(
60                 'severity',
61                 null,
62                 InputOption::VALUE_OPTIONAL,
63                 $this->trans('commands.database.log.clear.options.severity')
64             )
65             ->addOption(
66                 'user-id',
67                 null,
68                 InputOption::VALUE_OPTIONAL,
69                 $this->trans('commands.database.log.clear.options.user-id')
70             );
71     }
72
73     /**
74      * {@inheritdoc}
75      */
76     protected function execute(InputInterface $input, OutputInterface $output)
77     {
78         $io = new DrupalStyle($input, $output);
79
80         $eventId = $input->getArgument('event-id');
81         $eventType = $input->getOption('type');
82         $eventSeverity = $input->getOption('severity');
83         $userId = $input->getOption('user-id');
84
85         if ($eventId) {
86             $this->clearEvent($io, $eventId);
87         } else {
88             $this->clearEvents($io, $eventType, $eventSeverity, $userId);
89         }
90
91         return 0;
92     }
93
94     /**
95      * @param DrupalStyle $io
96      * @param $eventId
97      * @return bool
98      */
99     private function clearEvent(DrupalStyle $io, $eventId)
100     {
101         $result = $this->database->delete('watchdog')->condition('wid', $eventId)->execute();
102
103         if (!$result) {
104             $io->error(
105                 sprintf(
106                     $this->trans('commands.database.log.clear.messages.not-found'),
107                     $eventId
108                 )
109             );
110
111             return false;
112         }
113
114         $io->success(
115             sprintf(
116                 $this->trans('commands.database.log.clear.messages.event-deleted'),
117                 $eventId
118             )
119         );
120
121         return true;
122     }
123
124     /**
125      * @param DrupalStyle   $io
126      * @param $eventType
127      * @param $eventSeverity
128      * @param $userId
129      * @return bool
130      */
131     protected function clearEvents(DrupalStyle $io, $eventType, $eventSeverity, $userId)
132     {
133         $severity = RfcLogLevel::getLevels();
134         $query = $this->database->delete('watchdog');
135
136         if ($eventType) {
137             $query->condition('type', $eventType);
138         }
139
140         if ($eventSeverity) {
141             if (!in_array($eventSeverity, $severity)) {
142                 $io->error(
143                     sprintf(
144                         $this->trans('commands.database.log.clear.messages.invalid-severity'),
145                         $eventSeverity
146                     )
147                 );
148
149                 return false;
150             }
151
152             $query->condition('severity', array_search($eventSeverity, $severity));
153         }
154
155         if ($userId) {
156             $query->condition('uid', $userId);
157         }
158
159         $result = $query->execute();
160
161         if (!$result) {
162             $io->error(
163                 $this->trans('commands.database.log.clear.messages.clear-error')
164             );
165
166             return false;
167         }
168
169         $io->success(
170             $this->trans('commands.database.log.clear.messages.clear-sucess')
171         );
172
173         return true;
174     }
175 }