-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathAddCommand.php
More file actions
89 lines (73 loc) · 1.85 KB
/
Copy pathAddCommand.php
File metadata and controls
89 lines (73 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
declare(strict_types=1);
namespace Queue\Command;
use Cake\Command\Command;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Queue\Console\Io;
use Queue\Queue\TaskFinder;
class AddCommand extends Command {
/**
* @return string
*/
public static function getDescription(): string {
return 'Add a job to the queue.';
}
/**
* @inheritDoc
*/
public static function defaultName(): string {
return 'queue add';
}
/**
* @return \Cake\Console\ConsoleOptionParser
*/
public function getOptionParser(): ConsoleOptionParser {
$parser = parent::getOptionParser();
$parser->addArgument('task', [
'help' => 'Task name',
'required' => false,
]);
$parser->addArgument('data', [
'help' => 'Additional data if needed',
'required' => false,
]);
$parser->setDescription(
'Adds a job into the queue. Only tasks that implement AddInterface can be added through CLI.',
);
return $parser;
}
/**
* @param \Cake\Console\Arguments $args Arguments
* @param \Cake\Console\ConsoleIo $io ConsoleIo
*
* @return int|null|void
*/
public function execute(Arguments $args, ConsoleIo $io) {
$tasks = $this->getTasks();
$taskName = $args->getArgument('task');
if (!$taskName) {
$io->out(count($tasks) . ' tasks available:');
foreach (array_keys($tasks) as $task) {
$io->out(' - ' . $task);
}
return;
}
if (!array_key_exists($taskName, $tasks)) {
$io->abort('Not a supported task.');
}
/** @var class-string<\Queue\Queue\AddInterface> $taskClass */
$taskClass = $tasks[$taskName];
/** @var \Queue\Queue\AddInterface $task */
$task = new $taskClass(new Io($io));
$task->add($args->getArgument('data'));
}
/**
* @return array<string>
*/
protected function getTasks(): array {
$taskFinder = new TaskFinder();
return $taskFinder->allAddable();
}
}