|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bernard\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Bernard\Producer; |
|
6
|
|
|
use Bernard\Message\PlainMessage; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
11
|
|
|
|
|
12
|
|
|
class ProduceCommand extends \Symfony\Component\Console\Command\Command |
|
13
|
|
|
{ |
|
14
|
|
|
protected $producer; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param Producer $producer |
|
18
|
|
|
*/ |
|
19
|
3 |
|
public function __construct(Producer $producer) |
|
20
|
|
|
{ |
|
21
|
3 |
|
$this->producer = $producer; |
|
22
|
|
|
|
|
23
|
3 |
|
parent::__construct('bernard:produce'); |
|
24
|
3 |
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
3 |
|
public function configure() |
|
30
|
|
|
{ |
|
31
|
3 |
|
$this |
|
32
|
3 |
|
->addOption('queue', null, InputOption::VALUE_OPTIONAL, 'Name of a queue to add this job to. By default the queue is guessed from the message name.', null) |
|
33
|
3 |
|
->addArgument('name', InputArgument::REQUIRED, 'Name for the message eg. "ImportUsers".') |
|
34
|
3 |
|
->addArgument('message', InputArgument::OPTIONAL, 'JSON encoded string that is used for message properties.') |
|
35
|
|
|
; |
|
36
|
3 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
3 |
|
public function execute(InputInterface $input, OutputInterface $output) |
|
42
|
|
|
{ |
|
43
|
3 |
|
$name = $input->getArgument('name'); |
|
44
|
3 |
|
$queue = $input->getOption('queue'); |
|
45
|
3 |
|
$message = []; |
|
46
|
|
|
|
|
47
|
3 |
|
if ($input->getArgument('message')) { |
|
48
|
2 |
|
$message = json_decode($input->getArgument('message'), true); |
|
49
|
|
|
|
|
50
|
2 |
|
if (json_last_error()) { |
|
51
|
1 |
|
throw new \RuntimeException('Could not decode invalid JSON ['.json_last_error().']'); |
|
52
|
|
|
} |
|
53
|
1 |
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
$this->producer->produce(new PlainMessage($name, $message), $queue); |
|
|
|
|
|
|
56
|
2 |
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.