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

BaseConsumerCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 46
ccs 10
cts 12
cp 0.8333
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getConsumer() 0 4 1
A handle() 0 16 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