RunJobCommand::execute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace SfCod\QueueBundle\Command;
4
5
use SfCod\QueueBundle\Worker\Options;
6
use SfCod\QueueBundle\Worker\Worker;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
/**
13
 * Command to run jobs by id
14
 *
15
 * @author Virchenko Maksim <[email protected]>
16
 */
17
class RunJobCommand extends Command
18
{
19
    /**
20
     * @var Worker
21
     */
22
    protected $worker;
23
24
    /**
25
     * RunJobCommand constructor.
26
     *
27
     * @param Worker $worker
28
     */
29
    public function __construct(Worker $worker)
30
    {
31
        $this->worker = $worker;
32
33
        parent::__construct();
34
    }
35
36
    /**
37
     * Configure command
38
     */
39
    protected function configure()
40
    {
41
        $this->setName('job-queue:run-job')
42
            ->setDescription('Runs single job by id.')
43
            ->addArgument('id', InputArgument::REQUIRED, 'The id of the job.')
44
            ->addOption('connection', null, InputArgument::OPTIONAL, 'The name of the connection.', 'default')
45
            ->addOption('queue', null, InputArgument::OPTIONAL, 'The name of the queue.', 'default')
46
            ->addOption('delay', null, InputArgument::OPTIONAL, 'Delay before retry failed job.', 0)
47
            ->addOption('memory', null, InputArgument::OPTIONAL, 'Maximum memory usage limit.', 128)
48
            ->addOption('sleep', null, InputArgument::OPTIONAL, 'Sleep time before getting new job.', 3)
49
            ->addOption('maxTries', null, InputArgument::OPTIONAL, 'Max tries to run job.', 1)
50
            ->addOption('timeout', null, InputArgument::OPTIONAL, 'Daemon timeout.', 60);
51
    }
52
53
    /**
54
     * Execute command
55
     *
56
     * @param InputInterface $input
57
     * @param OutputInterface $output
58
     *
59
     * @return int|void|null
60
     */
61
    public function execute(InputInterface $input, OutputInterface $output)
62
    {
63
        $options = new Options(
64
            $input->getOption('delay'),
65
            $input->getOption('memory'),
66
            $input->getOption('timeout'),
67
            $input->getOption('sleep'),
68
            $input->getOption('maxTries')
69
        );
70
        $connection = $input->getOption('connection');
71
        $queue = $input->getOption('queue');
72
        $jobId = $input->getArgument('id');
73
74
        $this->worker->runJobById($connection, $queue, $jobId, $options);
0 ignored issues
show
Bug introduced by
It seems like $jobId defined by $input->getArgument('id') on line 72 can also be of type array<integer,string> or null; however, SfCod\QueueBundle\Worker\Worker::runJobById() does only seem to accept string, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
75
76
        return 0;
77
    }
78
}
79