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

ProcessorInterfaceAdapter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 0
loc 58
ccs 24
cts 26
cp 0.9231
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
B process() 0 25 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
        $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