PeekCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 0
cts 10
cp 0
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace TreeHouse\WorkerBundle\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Helper\Table;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use TreeHouse\WorkerBundle\QueueManager;
12
13
class PeekCommand extends Command
14
{
15
    /**
16
     * @var QueueManager
17
     */
18
    protected $manager;
19
20
    /**
21
     * @param QueueManager $queueManager
22
     */
23
    public function __construct(QueueManager $queueManager)
24
    {
25
        $this->manager = $queueManager;
26
27
        parent::__construct();
28
    }
29
30
    /**
31
     * @inheritdoc
32
     */
33 View Code Duplication
    protected function configure()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        $this
36
            ->setName('worker:peek')
37
            ->addArgument('action', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Selects which actions to peek for. Defaults to all actions')
38
            ->addOption('state', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The state of job to peek for. Valid options are <comment>ready</comment>, <comment>delayed</comment> and <comment>buried</comment>.', ['ready'])
39
            ->setDescription('Peeks the next job from the queue')
40
            ->setHelp('Inspects the next job from the queue for one or more actions. Note that this does not reserve the job, so it will still be given to a worker if/once it\'s ready.')
41
        ;
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    protected function execute(InputInterface $input, OutputInterface $output)
48
    {
49
        $actions = $input->getArgument('action');
50
        if (empty($actions)) {
51
            $actions = array_keys($this->manager->getExecutors());
52
        }
53
54
        $states = $input->getOption('state');
55
56
        foreach ($actions as $action) {
0 ignored issues
show
Bug introduced by
The expression $actions of type string|array<integer,integer|string> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
57
            $output->writeln(sprintf('Action: <info>%s</info>', $action));
58
59
            $table = new Table($output);
60
            $table->setHeaders(['state', 'id', 'data']);
61
            foreach ($states as $state) {
0 ignored issues
show
Bug introduced by
The expression $states of type string|array<integer,string>|boolean|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
62
                $job = $this->manager->peek($action, $state);
63
64
                if (is_null($job)) {
65
                    $table->addRow([$state, '-', '-']);
66
                } else {
67
                    $table->addRow([$state, $job->getId(), $job->getData()]);
68
                }
69
            }
70
71
            $table->render();
72
            $output->writeln('');
73
        }
74
    }
75
}
76