Passed
Pull Request — master (#37)
by Nicolas
03:21
created

Run   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 14.71 %

Coupling/Cohesion

Components 2
Dependencies 11

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 11
dl 10
loc 68
ccs 0
cts 43
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 1
A setMessageAdapterFactory() 0 6 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
use Puzzle\AMQP\Workers\MessageAdapterFactory;
17
18
class Run extends Command
19
{
20
    use EventDispatcherAware;
21
22
    private
23
        $client,
24
        $workerProvider,
25
        $messageAdapterFactory,
26
        $outputInterfaceAware;
27
28 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...
29
    {
30
        parent::__construct();
31
32
        $this->client = $client;
33
        $this->workerProvider = $workerProvider;
34
        $this->messageAdapterFactory = null;
35
        $this->outputInterfaceAware = $outputInterfaceAware;
36
        $this->eventDispatcher = new NullEventDispatcher();
37
    }
38
    
39
    public function setMessageAdapterFactory(MessageAdapterFactory $factory)
40
    {
41
        $this->messageAdapterFactory = $factory;
42
        
43
        return $this;
44
    }
45
46
    protected function configure()
47
    {
48
        $this->setName('run')
49
            ->setDescription('Launch AMQP worker')
50
            ->addArgument('task', InputArgument::REQUIRED, 'worker name to run');
51
    }
52
53
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55
        $this->outputInterfaceAware->register($output);
56
57
        $workerName = $input->getArgument('task');
58
        $workerContext = $this->workerProvider->getWorker($workerName);
59
60
        if($workerContext instanceof WorkerContext)
61
        {
62
            $output->writeln("Launching <info>$workerName</info>");
63
64
            $processor = $this->createProcessor($workerContext);
65
66
            $this->eventDispatcher->dispatch('worker.run');
67
68
            return $workerContext->getConsumer()->consume($processor, $this->client, $workerContext);
69
        }
70
71
        $output->writeln("<error>Worker $workerName not found</error>");
72
    }
73
74
    private function createProcessor(WorkerContext $workerContext)
75
    {
76
        $processor = new ProcessorInterfaceAdapter($workerContext, $this->messageAdapterFactory);
77
        $processor->setEventDispatcher($this->eventDispatcher);
78
79
        $processor->setMessageProcessors(
80
            $this->workerProvider->getMessageProcessors()
81
        );
82
83
        return $processor;
84
    }
85
}
86