Passed
Pull Request — master (#29)
by Sébastien
03:40
created

Run   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 15.25 %

Coupling/Cohesion

Components 2
Dependencies 11

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 11
dl 9
loc 59
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 1
A configure() 0 6 1
A execute() 0 20 2
A createProcessor() 0 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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