Test Failed
Push — master ( 159af9...b2d1de )
by Banciu N. Cristian Mihai
03:26
created

PublisherCommand::handleInternal()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 30
rs 9.4222
cc 5
nc 7
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BinaryCube\CarrotMQ\Support\Laravel\Console\Commands;
6
7
use BinaryCube\CarrotMQ\Builder\MessageBuilder;
8
9
use function feof;
10
use function fread;
11
use function vsprintf;
12
13
/**
14
 * Class PublisherCommand
15
 */
16
class PublisherCommand extends BaseCommand
17
{
18
19
    /**
20
     * The name and signature of the console command.
21
     *
22
     * @var string
23
     */
24
    protected $signature = '
25
                            carrot-mq:publish
26
                            {publisher}
27
                            {--route= : Routing Key}';
28
29
    /**
30
     * The console command description.
31
     *
32
     * @var string
33
     */
34
    protected $description = 'Executes a publisher that reads data from STDIN';
35
36
    /**
37
     * Execute the console command.
38
     *
39
     * @return integer
40
     *
41
     * @throws \Exception
42
     * @throws \Throwable
43
     */
44
    public function handleInternal()
45
    {
46
        $publisherId = (string) $this->input->getArgument('publisher');
47
        $route       = (string) $this->input->getOption('route');
48
        $data        = '';
49
50
        if (! $this->carrot->container()->publishers()->has($publisherId)) {
51
            $this->error(vsprintf('Publisher "%s" not found.', [$publisherId]));
52
            return 0;
53
        }
54
55
        $publisher = $this->carrot->container()->publishers()->get($publisherId);
56
57
        while (! feof(STDIN)) {
58
            $data .= fread(STDIN, 8192);
59
        }
60
61
        if (empty($data)) {
62
            return 0;
63
        }
64
65
        $message = MessageBuilder::create()->body($data)->build();
66
67
        if (! empty($route)) {
68
            $message->setRoutingKey($route);
69
        }
70
71
        $publisher->publish($message);
72
73
        return 0;
74
    }
75
76
}
77