e2df4760cb5fc02b711ce6ffda73395d0c184806
[yaffs-website] / vendor / drupal / console / src / Command / Queue / RunCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Queue\DebugCommand.
6  */
7
8 namespace Drupal\Console\Command\Queue;
9
10 use Symfony\Component\Console\Command\Command;
11 use Symfony\Component\Console\Input\InputArgument;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Drupal\Core\Queue\QueueWorkerManagerInterface;
15 use Drupal\Core\Queue\QueueFactory;
16 use Drupal\Console\Core\Command\Shared\CommandTrait;
17 use Drupal\Console\Core\Style\DrupalStyle;
18
19 /**
20  * Class RunCommand
21  *
22  * @package Drupal\Console\Command\Queue
23  */
24 class RunCommand extends Command
25 {
26     use CommandTrait;
27
28     /**
29      * @var QueueWorkerManagerInterface
30      */
31     protected $queueWorker;
32
33
34     /**
35      * @var QueueFactory
36      */
37     protected $queue;
38
39     /**
40      * DebugCommand constructor.
41      *
42      * @param QueueWorkerManagerInterface $queueWorker
43      * @param QueueFactory                $queue
44      */
45     public function __construct(
46         QueueWorkerManagerInterface $queueWorker,
47         QueueFactory $queue
48     ) {
49         $this->queueWorker = $queueWorker;
50         $this->queue = $queue;
51         parent::__construct();
52     }
53
54     /**
55      * {@inheritdoc}
56      */
57     protected function configure()
58     {
59         $this
60             ->setName('queue:run')
61             ->setDescription($this->trans('commands.queue.run.description'))
62             ->addArgument(
63                 'name',
64                 InputArgument::OPTIONAL,
65                 $this->trans('commands.queue.run.arguments.name')
66             );
67     }
68
69     /**
70      * {@inheritdoc}
71      */
72     protected function execute(InputInterface $input, OutputInterface $output)
73     {
74         $io = new DrupalStyle($input, $output);
75         $name = $input->getArgument('name');
76
77         if (!$name) {
78             $io->error(
79                 $this->trans('commands.queue.run.messages.missing-name')
80             );
81
82             return 1;
83         }
84
85         try {
86             $worker = $this->queueWorker->createInstance($name);
87         } catch (\Exception $e) {
88             $io->error(
89                 sprintf(
90                     $this->trans('commands.queue.run.messages.invalid-name'),
91                     $name
92                 )
93             );
94
95             return 1;
96         }
97
98         $start = microtime(true);
99         $result = $this->runQueue($worker);
100         $time = microtime(true) - $start;
101
102         if (!empty($result['error'])) {
103             $io->error(
104                 sprintf(
105                     $this->trans('commands.queue.run.messages.failed'),
106                     $name,
107                     $result['error']
108                 )
109             );
110
111             return 1;
112         }
113
114         $io->success(
115             sprintf(
116                 $this->trans('commands.queue.run.success'),
117                 $name,
118                 $result['count'],
119                 $result['total'],
120                 round($time, 2)
121             )
122         );
123
124         return 0;
125     }
126
127     /**
128      * @param $worker
129      *
130      * @return array
131      */
132     private function runQueue($worker)
133     {
134         $result['count'] = 0;
135         $result['total'] = $this->queue->numberOfItems();
136         while ($item = $this->queue->claimItem()) {
137             try {
138                 $worker->processItem($item->data);
139                 $this->queue->deleteItem($item);
140                 $result['count']++;
141             } catch (SuspendQueueException $e) {
142                 $this->queue->releaseItem($item);
143                 $result['error'] = $e;
144             }
145         }
146
147         return $result;
148     }
149 }