Completed
Pull Request — master (#3)
by Kevin
03:40
created

SubscriberCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 47.83%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 8
dl 0
loc 54
ccs 11
cts 23
cp 0.4783
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A configure() 0 10 1
A execute() 0 19 4
1
<?php
2
3
namespace Zenstruck\Queue\Bridge\Symfony\Console;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Zenstruck\Queue\Subscriber;
11
use Zenstruck\Queue\Subscriber\ExitStrategy\ChainExitStrategy;
12
use Zenstruck\Queue\Subscriber\ExitStrategy\MaxCountExitStrategy;
13
use Zenstruck\Queue\Subscriber\ExitStrategy\MemoryLimitExitStrategy;
14
use Zenstruck\Queue\Subscriber\ExitStrategy\TimeoutExitStrategy;
15
16
/**
17
 * @author Kevin Bond <[email protected]>
18
 */
19
class SubscriberCommand extends Command
20
{
21
    private $subscriber;
22
23
    /**
24
     * @param Subscriber $subscriber
25
     * @param string     $name
26
     * @param string     $description
27
     */
28 4
    public function __construct(Subscriber $subscriber, $name, $description)
29
    {
30
        parent::__construct($name);
31
        $this->setDescription($description);
32
33 4
        $this->subscriber = $subscriber;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 4
    protected function configure()
40
    {
41 4
        $this
42 4
            ->addArgument('max-attempts', InputArgument::OPTIONAL, 'The number of times to attempt a job before marking as failed, 0 for unlimited', 50)
43 4
            ->addOption('wait-time', null, InputOption::VALUE_REQUIRED, 'Time in seconds to wait before consuming another job')
44 4
            ->addOption('max-jobs', null, InputOption::VALUE_REQUIRED, 'The number of jobs to consume before exiting')
45 4
            ->addOption('timeout', null, InputOption::VALUE_REQUIRED, 'The number of seconds to consume jobs before exiting')
46 4
            ->addOption('memory-limit', null, InputOption::VALUE_REQUIRED, 'The memory limit in MB - will exit if exceeded')
47 4
        ;
48 4
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55
        $exitStrategy = new ChainExitStrategy();
56
57
        if (null !== $maxJobs = $input->getOption('max-jobs')) {
58
            $exitStrategy->addExitStrategy(new MaxCountExitStrategy($maxJobs));
59
        }
60
61
        if (null !== $timeout = $input->getOption('timeout')) {
62
            $exitStrategy->addExitStrategy(new TimeoutExitStrategy($timeout));
63
        }
64
65
        if (null !== $memoryLimit = $input->getOption('memory-limit')) {
66
            $exitStrategy->addExitStrategy(new MemoryLimitExitStrategy($memoryLimit));
67
        }
68
69
        $reason = $this->subscriber->subscribe($exitStrategy, null, $input->getOption('wait-time'), $input->getArgument('max-attempts'));
70
        $output->writeln(sprintf('<comment>Subscriber Exited</comment>: %s', $reason));
71
    }
72
}
73