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

ProcessorInterfaceAdapter::onWorkerProcessed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 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