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

ProcessorInterfaceAdapter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 90.48%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 0
loc 50
ccs 19
cts 21
cp 0.9048
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B process() 0 24 2
A onWorkerProcess() 0 4 1
A onWorkerProcessed() 0 4 1
1
<?php
2
3
namespace Puzzle\AMQP\Workers;
4
5
use Swarrot\Processor\ProcessorInterface;
6
use Puzzle\Pieces\EventDispatcher\NullEventDispatcher;
7
use Puzzle\Pieces\EventDispatcher\EventDispatcherAware;
8
use Puzzle\AMQP\Clients\Processors\MessageProcessorAware;
9
10
class ProcessorInterfaceAdapter implements ProcessorInterface
11
{
12
    use
13
        EventDispatcherAware,
14
        MessageProcessorAware;
15
    
16
    private
17
        $workerContext;
18
    
19 2
    public function __construct(WorkerContext $workerContext)
20
    {
21 2
        $this->workerContext = $workerContext;
22 2
        $this->eventDispatcher = new NullEventDispatcher();
23 2
    }
24
    
25 2
    public function process(\Swarrot\Broker\Message $message, array $options)
26
    {
27 2
        $message = new MessageAdapter($message);
28 2
        $message = $this->onConsume($message);
29
        
30 2
        $this->workerContext->getLogger()->debug((string) $message);
31
        
32 2
        $this->onWorkerProcess();
33
34
        try
35
        {
36 2
            $processResult = $this->workerContext->getWorker()->process($message);
37
        }
38 2
        catch(\Exception $exception)
39
        {
40
            $this->onWorkerProcessed();
41
42
            throw $exception;
43
        }
44
        
45 2
        $this->onWorkerProcessed();
46
47 2
        return $processResult;
48
    }
49
50 2
    private function onWorkerProcess()
51
    {
52 2
        $this->eventDispatcher->dispatch('worker.process');
53 2
    }
54
    
55 2
    private function onWorkerProcessed()
56
    {
57 2
        $this->eventDispatcher->dispatch('worker.processed');
58 2
    }
59
}
60