RunWorker   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 57
ccs 0
cts 19
cp 0
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 14 1
A execute() 0 10 2
A listWorkers() 0 15 2
A __construct() 0 4 1
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/boot project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Boot\Console\Command;
10
11
use Daikon\AsyncJob\Worker\WorkerInterface;
12
use Daikon\AsyncJob\Worker\WorkerMap;
13
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Question\ChoiceQuestion;
18
19
class RunWorker extends Command
20
{
21
    use DialogTrait;
22
23
    protected WorkerMap $workerMap;
24
25
    public function __construct(WorkerMap $workerMap)
26
    {
27
        parent::__construct();
28
        $this->workerMap = $workerMap;
29
    }
30
31
    protected function configure(): void
32
    {
33
        $this
34
            ->setName('worker:run')
35
            ->setDescription('Run an asynchronous job worker.')
36
            ->addArgument(
37
                'queue',
38
                InputArgument::REQUIRED,
39
                'Name of the message queue from which to execute jobs.'
40
            )
41
            ->addArgument(
42
                'worker',
43
                InputArgument::OPTIONAL,
44
                'Name of the worker from which to execute jobs.'
45
            );
46
    }
47
48
    //@todo support running multiple queues
49
    protected function execute(InputInterface $input, OutputInterface $output): int
50
    {
51
        if (!is_string($workerName = $input->getArgument('worker'))) {
52
            $workerName = $this->listWorkers($input, $output);
53
        }
54
        /** @var WorkerInterface $worker */
55
        $worker = $this->workerMap->get($workerName);
56
        $worker->run(['queue' => $input->getArgument('queue')]);
57
        //@todo return int from worker
58
        return 0;
59
    }
60
61
    protected function listWorkers(InputInterface $input, OutputInterface $output): string
62
    {
63
        if (!count($this->workerMap)) {
64
            $output->writeln('<error>There are no workers available.</error>');
65
            $output->writeln('');
66
            exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
67
        }
68
69
        $helper = $this->getHelper('question');
70
        $question = new ChoiceQuestion(
71
            'Please select a worker: ',
72
            $this->workerMap->keys() //@todo show workers with filtered by queue
73
        );
74
75
        return $helper->ask($input, $output, $question);
76
    }
77
}
78