Completed
Push — master ( 42ab41...f5b0ad )
by Tomasz
02:38
created

DirectProcessingDriver::getProcessor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Gendoria\CommandQueue\SendDriver;
4
5
use Gendoria\CommandQueue\Command\CommandInterface;
6
use Gendoria\CommandQueue\ProcessorFactoryInterface;
7
use Gendoria\CommandQueue\ProcessorNotFoundException;
8
use Gendoria\CommandQueue\Worker\Exception\ProcessorErrorException;
9
use Gendoria\CommandQueue\Worker\Exception\TranslateErrorException;
10
use Gendoria\CommandQueue\Worker\WorkerInterface;
11
use Psr\Log\LoggerAwareInterface;
12
13
/**
14
 * Description of DirectProcessingDriver.
15
 *
16
 * @author Tomasz Struczyński <[email protected]>
17
 */
18
class DirectProcessingDriver implements SendDriverInterface, WorkerInterface, LoggerAwareInterface
19
{
20
    use \Psr\Log\LoggerAwareTrait;
21
    
22
    /**
23
     * Processor factory.
24
     *
25
     * @var ProcessorFactoryInterface
26
     */
27
    private $processorFactory;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 4
    public function send(CommandInterface $command)
33
    {
34
        try {
35 4
            $this->process($command);
36 4
        } catch (ProcessorNotFoundException $e) {
37 2
            if ($this->logger) {
38 1
                $this->logger->error('Exception while sending command: '.$e->getMessage());
39 1
            }
40 3
        } catch (\Exception $e) {
41 1
            if ($this->logger) {
42 1
                $this->logger->error('Exception while sending command: '.$e->getMessage());
43 1
            }
44
        }
45 4
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 5
    public function process($command)
51
    {
52 5
        if (!$command instanceof CommandInterface) {
53
            throw new TranslateErrorException($command, "Command has to be instance of command interface for direct processing driver", 500);
54
        }
55 5
        $processor = $this->getProcessor($command);
56
        try {
57 2
            $processor->process($command);
58 2
        } catch (\Exception $e) {
59 1
            throw new ProcessorErrorException($command, $processor, $e->getMessage(), $e->getCode(), $e);
60
        }
61 1
    }
62
    
63
    /**
64
     * {@inheritdoc}
65
     */
66 5
    public function getProcessor(CommandInterface $command)
67
    {
68 5
        $processor = $this->processorFactory->getProcessor($command);
69 2
        if ($this->logger) {
70 2
            $processor->setLogger($this->logger);
71 2
        }
72 2
        return $processor;
73
    }    
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 5
    public function setProcessorFactory(ProcessorFactoryInterface $processorFactory)
79
    {
80 5
        $this->processorFactory = $processorFactory;
81 5
    }
82
}
83