Passed
Push — master ( 9ce6d5...0632cf )
by Nicolas
04:06
created

ProcessorInterfaceAdapter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A onWorkerProcess() 0 4 1
A process() 0 29 3
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 5
    public function __construct(WorkerContext $workerContext)
21
    {
22 5
        $this->workerContext = $workerContext;
23 5
        $this->eventDispatcher = new NullEventDispatcher();
24 5
        $this->messageAdapterFactory = null;
25 5
    }
26
    
27 5
    public function process(\Swarrot\Broker\Message $message, array $options)
28
    {
29 5
        $message = $this->createMessageAdapter($message);
30 5
        $message = $this->onConsume($message);
31
        
32 5
        $this->workerContext->getLogger()->debug((string) $message);
33
        
34 5
        $this->onWorkerProcess();
35
36
        try
37
        {
38 5
            $processResult = $this->workerContext->getWorker()->process($message);
39
        }
40 2
        catch(\Throwable $exception)
41
        {
42 2
            $this->onWorkerProcessed();
43
44 2
            if($exception instanceof \Error)
45
            {
46 1
                $exception = new \ErrorException($exception->getMessage(), $exception->getCode(), E_ERROR, $exception->getFile(), $exception->getLine(), $exception);
47
            }
48
49 2
            throw $exception;
50
        }
51
52 3
        $this->onWorkerProcessed();
53
54 3
        return $processResult;
55
    }
56
57 5
    private function onWorkerProcess()
58
    {
59 5
        $this->eventDispatcher->dispatch('worker.process');
60 5
    }
61
    
62 5
    private function onWorkerProcessed()
63
    {
64 5
        $this->eventDispatcher->dispatch('worker.processed');
65 5
    }
66
}
67