Completed
Push — master ( 8ae834...5a6d20 )
by Tomasz
10:40
created

RunWorkerCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
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
    public function setRunnerManager(WorkerRunnerManager $runnerManager)
31
    {
32
        $this->runnerManager = $runnerManager;
33
    }
34
    
35
    protected function configure()
36
    {
37
        $this->setName('cmq:worker:run')
38
            ->setDescription('Runs a worker process. Specific worker has to be registered by driver or application.')
39
            ->addArgument('name', InputArgument::REQUIRED, 'Worker name.');
40
    }
41
    
42
    public function execute(InputInterface $input, OutputInterface $output)
43
    {
44
        if (null === $this->runnerManager) {
45
            $output->writeln("<error>Runner manager not provided to command. Command is not correctly initialized.</error>");
46
            return 1;
47
        }
48
        $name = $input->getArgument('name');
49
        if (!$this->runnerManager->has($name)) {
50
            $runners = $this->runnerManager->getRunners();
51
            $runnersFormatted = array_map(array($this, 'formatRunnerName'), $runners);
52
            $output->writeln(sprintf('<error>Worker "%s" not registered.</error>', $name));
53
            $output->writeln('Registered workers:');
54
            $output->writeln($runnersFormatted);
55
            return 1;
56
        }
57
        $this->runnerManager->run($name, $output);
58
    }
59
    
60
    public function formatRunnerName($name)
61
    {
62
        return sprintf("  * <info>%s</info>", $name);
63
    }
64
}
65