Passed
Pull Request — master (#29)
by Nicolas
02:56
created

Run::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Puzzle\AMQP\Commands\Worker;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Puzzle\AMQP\Workers\ProcessorInterfaceAdapter;
10
use Puzzle\AMQP\Client;
11
use Puzzle\AMQP\Workers\WorkerProvider;
12
use Puzzle\AMQP\Workers\WorkerContext;
13
use Puzzle\Pieces\OutputInterfaceAware;
14
use Puzzle\Pieces\EventDispatcher\EventDispatcherAware;
15
use Puzzle\Pieces\EventDispatcher\NullEventDispatcher;
16
17
class Run extends Command
18
{
19
    use EventDispatcherAware;
20
21
    private
22
        $client,
23
        $workerProvider,
24
        $outputInterfaceAware;
25
26 View Code Duplication
    public function __construct(Client $client, WorkerProvider $workerProvider, OutputInterfaceAware $outputInterfaceAware)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
    {
28
        parent::__construct();
29
30
        $this->client = $client;
31
        $this->workerProvider = $workerProvider;
32
        $this->outputInterfaceAware = $outputInterfaceAware;
33
        $this->eventDispatcher = new NullEventDispatcher();
34
    }
35
36
    protected function configure()
37
    {
38
        $this->setName('run')
39
            ->setDescription('Launch AMQP worker')
40
            ->addArgument('task', InputArgument::REQUIRED, 'worker name to run');
41
    }
42
43
    protected function execute(InputInterface $input, OutputInterface $output)
44
    {
45
        $this->outputInterfaceAware->register($output);
46
47
        $workerName = $input->getArgument('task');
48
        $workerContext = $this->workerProvider->getWorker($workerName);
49
50
        if($workerContext instanceof WorkerContext)
51
        {
52
            $output->writeln("Launching <info>$workerName</info>");
53
54
            $processor = $this->createProcessor($workerContext);
55
56
            $this->eventDispatcher->dispatch('worker.run');
57
58
            return $workerContext->getConsumer()->consume($processor, $this->client, $workerContext);
59
        }
60
61
        $output->writeln("<error>Worker $workerName not found</error>");
62
    }
63
64
    private function createProcessor(WorkerContext $workerContext)
65
    {
66
        $processor = new ProcessorInterfaceAdapter($workerContext);
67
        $processor->setEventDispatcher($this->eventDispatcher);
68
69
        $processor->setMessageProcessors(
70
            $this->workerProvider->getMessageProcessors()
71
        );
72
73
        return $processor;
74
    }
75
}
76