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

ProcessorInterfaceAdapter::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
crap 2
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
        $messageAdapterFactory,
18
        $workerContext;
19
    
20 2
    public function __construct(WorkerContext $workerContext, MessageAdapterFactory $factory = null)
21
    {
22 2
        $this->workerContext = $workerContext;
23 2
        $this->eventDispatcher = new NullEventDispatcher();
24
        
25 2
        if(! $factory instanceof MessageAdapterFactory)
26 2
        {
27 2
            $factory = new MessageAdapterFactory();
28 2
        }
29 2
        $this->messageAdapterFactory = $factory;
30 2
    }
31
    
32 2
    public function process(\Swarrot\Broker\Message $message, array $options)
33
    {
34 2
        $message = $this->messageAdapterFactory->build($message);
35
        
36 2
        $message = $this->onConsume($message);
37
        
38 2
        $this->workerContext->getLogger()->debug((string) $message);
39
        
40 2
        $this->onWorkerProcess();
41
42
        try
43
        {
44 2
            $processResult = $this->workerContext->getWorker()->process($message);
45
        }
46 2
        catch(\Exception $exception)
47
        {
48
            $this->onWorkerProcessed();
49
50
            throw $exception;
51
        }
52
        
53 2
        $this->onWorkerProcessed();
54
55 2
        return $processResult;
56
    }
57
58 2
    private function onWorkerProcess()
59
    {
60 2
        $this->eventDispatcher->dispatch('worker.process');
61 2
    }
62
    
63 2
    private function onWorkerProcessed()
64
    {
65 2
        $this->eventDispatcher->dispatch('worker.processed');
66 2
    }
67
}
68