RunWorkerCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Gendoria\CommandQueue\Console\Command;
4
5
use Gendoria\CommandQueue\Worker\WorkerRunnerManager;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * Description of RunWorkerCommand
13
 *
14
 * @author Tomasz Struczyński <[email protected]>
15
 */
16
class RunWorkerCommand extends Command
17
{
18
    /**
19
     * Worker runner manager.
20
     * 
21
     * @var WorkerRunnerManager
22
     */
23
    private $runnerManager;
24
    
25
    /**
26
     * Set worker runner manager.
27
     * 
28
     * @param WorkerRunnerManager $runnerManager Worker runner manager instance.
29
     */
30 2
    public function setRunnerManager(WorkerRunnerManager $runnerManager)
31
    {
32 2
        $this->runnerManager = $runnerManager;
33 2
    }
34
    
35 3
    protected function configure()
36
    {
37 3
        $this->setName('cmq:worker:run')
38 3
            ->setDescription('Runs a worker process. Specific worker has to be registered by driver or application.')
39 3
            ->addArgument('name', InputArgument::REQUIRED, 'Worker name.');
40 3
    }
41
    
42 3
    public function execute(InputInterface $input, OutputInterface $output)
43
    {
44 3
        if (null === $this->runnerManager) {
45 1
            $output->writeln("<error>Runner manager not provided to command. Command is not correctly initialized.</error>");
46 1
            return 1;
47
        }
48 2
        $name = $input->getArgument('name');
49 2
        if (!$this->runnerManager->has($name)) {
50 1
            $runners = $this->runnerManager->getRunners();
51 1
            $runnersFormatted = array_map(array($this, 'formatRunnerName'), $runners);
52 1
            $output->writeln(sprintf('<error>Worker "%s" not registered.</error>', $name));
53 1
            $output->writeln('Registered workers:');
54 1
            $output->writeln($runnersFormatted);
55 1
            return 1;
56
        }
57 1
        $this->runnerManager->run($name, $output);
58 1
    }
59
    
60 1
    public function formatRunnerName($name)
61
    {
62 1
        return sprintf("  * <info>%s</info>", $name);
63
    }
64
}
65