Passed
Push — master ( d1ccc3...a5920b )
by Nicolas
04:01
created

ProcessorInterfaceAdapter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 0
loc 52
ccs 20
cts 22
cp 0.9091
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 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
        MessageAdapterFactoryAware,
15
        MessageProcessorAware;
16
    
17
    private
18
        $workerContext;
19
    
20 3
    public function __construct(WorkerContext $workerContext)
21
    {
22 3
        $this->workerContext = $workerContext;
23 3
        $this->eventDispatcher = new NullEventDispatcher();
24 3
        $this->messageAdapterFactory = null;
25 3
    }
26
    
27 3
    public function process(\Swarrot\Broker\Message $message, array $options)
28
    {
29 3
        $message = $this->createMessageAdapter($message);
30 3
        $message = $this->onConsume($message);
31
        
32 3
        $this->workerContext->getLogger()->debug((string) $message);
33
        
34 3
        $this->onWorkerProcess();
35
36
        try
37
        {
38 3
            $processResult = $this->workerContext->getWorker()->process($message);
39
        }
40 3
        catch(\Exception $exception)
41
        {
42
            $this->onWorkerProcessed();
43
44
            throw $exception;
45
        }
46
        
47 3
        $this->onWorkerProcessed();
48
49 3
        return $processResult;
50
    }
51
52 3
    private function onWorkerProcess()
53
    {
54 3
        $this->eventDispatcher->dispatch('worker.process');
55 3
    }
56
    
57 3
    private function onWorkerProcessed()
58
    {
59 3
        $this->eventDispatcher->dispatch('worker.processed');
60 3
    }
61
}
62