Completed
Push — master ( 0bf81b...91e598 )
by Ma
06:39
created

StartQueueWorkerCommand::formatOutput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 2
1
<?php
2
3
/**
4
 * Aist Queue (http://mateuszsitek.com/projects/queue)
5
 *
6
 * @copyright Copyright (c) 2017 DIGITAL WOLVES LTD (http://digitalwolves.ltd) All rights reserved.
7
 * @license   http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
8
 */
9
10
namespace Aist\Queue\Console\Command;
11
12
use Aist\Queue\Console\Helper\QueueHelper;
13
use SlmQueue\Controller\Exception\WorkerProcessException;
14
use SlmQueue\Exception\ExceptionInterface;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
class StartQueueWorkerCommand extends Command
22
{
23
    const NAME = 'queue:worker:start';
24
25
    const DESCRIPTION = 'Process queue worker.';
26
27
    const HELP = <<< 'EOT'
28
Process queue worker.
29
EOT;
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    protected function configure()
35
    {
36
        $this
37
            ->setName(self::NAME)
38
            ->setDescription(self::DESCRIPTION)
39
            ->setHelp(self::HELP)
40
            ->setDefinition([
41
                new InputArgument(
42
                    'name',
43
                    InputArgument::REQUIRED,
44
                    'The queue name.'
45
                ),
46
            ])
47
        ;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55
//        $queueHelper = $this->getHelper('qm');
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
56
        $queuePluginManager = $this->getApplication()->getHelperSet()->get('qm');
57
58
        $worker = $this->getApplication()->getHelperSet()->get('worker');
59
60
        $output->writeln(
61
            'Processing queue <info>' . $input->getArgument('name') . '</info> PID <info>' . getmypid() . '</info>'
62
        );
63
64
        return $this->executeWorkerCommand($input, $output, $queuePluginManager, $worker);
65
    }
66
67
    /**
68
     * Execute worker
69
     *
70
     * @param InputInterface $input
71
     * @param OutputInterface $output
72
     * @param QueueHelper $queuePluginManager
73
     * @param $worker
74
     *
75
     * @return int
76
     */
77
    protected function executeWorkerCommand(
78
        InputInterface $input,
79
        OutputInterface $output,
80
        QueueHelper $queuePluginManager,
81
        $worker
82
    ) {
83
        $options = $input->getOptions();
84
        $name    = $input->getArgument('name');
85
        $queue   = $queuePluginManager->get($name);
0 ignored issues
show
Documentation Bug introduced by
The method get does not exist on object<Aist\Queue\Console\Helper\QueueHelper>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
86
87
        try {
88
            $messages = $worker->processQueue($queue, $options);
89
        } catch (ExceptionInterface $e) {
90
            throw new WorkerProcessException(
91
                'Caught exception while processing queue ' . $name,
92
                $e->getCode(),
93
                $e
94
            );
95
        }
96
        $output->writeln(sprintf('Finished worker for queue <info>%s</info>', $name));
97
        $output->writeln($messages);
98
99
        return 0;
100
    }
101
}
102