StartQueueWorkerCommand::execute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 1
eloc 9
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\Output\OutputInterface;
19
20
/**
21
 * Queue Worker
22
 */
23
class StartQueueWorkerCommand extends Command
24
{
25
    const NAME = 'queue:worker:start';
26
27
    const DESCRIPTION = 'Process queue worker.';
28
29
    const HELP = <<< 'EOT'
30
Process queue worker.
31
EOT;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    protected function configure()
37
    {
38
        $this
39
            ->setName(self::NAME)
40
            ->setDescription(self::DESCRIPTION)
41
            ->setHelp(self::HELP)
42
            ->setDefinition([
43
                new InputArgument(
44
                    'name',
45
                    InputArgument::REQUIRED,
46
                    'The queue name.'
47
                ),
48
            ])
49
        ;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    protected function execute(InputInterface $input, OutputInterface $output)
56
    {
57
        /**
58
         * Strange issue
59
         * Without this we can't use $this->getHelper('name')
60
         * but $this->getApplication()->getHelperSet()->get('name')
61
         */
62
        $this->setApplication($this->getApplication());
63
        $queuePluginManager = $this->getHelper('qm');
64
        $worker = $this->getHelper('worker');
65
        $logger = $this->getHelper('logger');
66
67
        $this->setProcessTitle('queue.' . $input->getArgument('name'));
68
69
        $output->writeln(
70
            'Processing queue <info>' . $input->getArgument('name') . '</info> PID <info>' . getmypid() . '</info>'
71
        );
72
73
        return $this->executeWorkerCommand($input, $output, $queuePluginManager, $worker, $logger);
0 ignored issues
show
Compatibility introduced by
$queuePluginManager of type object<Symfony\Component...Helper\HelperInterface> is not a sub-type of object<Aist\Queue\Console\Helper\QueueHelper>. It seems like you assume a concrete implementation of the interface Symfony\Component\Console\Helper\HelperInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
74
    }
75
76
    /**
77
     * Execute worker
78
     *
79
     * @param InputInterface $input
80
     * @param OutputInterface $output
81
     * @param QueueHelper $queuePluginManager
82
     * @param $worker
83
     *
84
     * @return int
85
     */
86
    protected function executeWorkerCommand(
87
        InputInterface $input,
88
        OutputInterface $output,
89
        QueueHelper $queuePluginManager,
90
        $worker,
91
        $logger
92
    ) {
93
        $options = $input->getOptions();
94
        $name    = $input->getArgument('name');
95
        $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...
96
97
        try {
98
            $messages = $worker->processQueue($queue, $options);
99
        } catch (ExceptionInterface $e) {
100
            $logger->error(
101
                self::class,
102
                [
103
                    'queue' => $name,
104
                    'code' => $e->getCode(),
105
                    'message' => $e->getMessage(),
106
                ]
107
            );
108
            throw new WorkerProcessException(
109
                'Caught exception while processing queue ' . $name,
110
                $e->getCode(),
111
                $e
112
            );
113
        }
114
        $output->writeln(sprintf('Finished worker for queue <info>%s</info>', $name));
115
        $output->writeln($messages);
116
117
        return 0;
118
    }
119
}
120