Passed
Branch master (f63365)
by Adrian Florin
02:50
created

BaseConsumerCommand::getConsumer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
namespace NeedleProject\LaravelRabbitMq\Command;
3
4
use Illuminate\Console\Command;
5
use NeedleProject\LaravelRabbitMq\ConsumerInterface;
6
7
/**
8
 * Class BaseConsumerCommand
9
 *
10
 * @package NeedleProject\LaravelRabbitMq\Command
11
 */
12
class BaseConsumerCommand extends Command
13
{
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'rabbitmq:consume {consumer} {--time=60} {--messages=100} {--memory=64}';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Start consuming messages';
27
28
    /**
29
     * @param string $consumerAliasName
30
     * @return ConsumerInterface
31
     */
32
    protected function getConsumer(string $consumerAliasName): ConsumerInterface
33
    {
34
        return app()->makeWith(ConsumerInterface::class, [$consumerAliasName]);
35
    }
36
37
    /**
38
     * Execute the console command.
39
     * @return int
40
     */
41 2
    public function handle()
42
    {
43 2
        $messageCount = $this->input->getOption('messages');
44 2
        $waitTime = $this->input->getOption('time');
45 2
        $memoryLimit = $this->input->getOption('memory');
46
47
        /** @var ConsumerInterface $consumer */
48 2
        $consumer = $this->getConsumer($this->input->getArgument('consumer'));
49
        try {
50 2
            return $consumer->startConsuming($messageCount, $waitTime, $memoryLimit);
51 1
        } catch (\Throwable $e) {
52 1
            $consumer->stopConsuming();
53 1
            $this->output->error($e->getMessage());
54 1
            return -1;
55
        }
56
    }
57
}
58