Completed
Push — master ( f5b0ad...8bc0a6 )
by Tomasz
02:28
created

BaseWorker   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 0
loc 134
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A process() 0 18 3
A getProcessor() 0 6 1
translateCommand() 0 1 ?
A setProcessorFactory() 0 4 1
A beforeTranslateHook() 0 3 1
A beforeGetProcessorHook() 0 3 1
A beforeProcessHook() 0 3 1
A afterProcessHook() 0 3 1
1
<?php
2
3
namespace Gendoria\CommandQueue\Worker;
4
5
use Exception;
6
use Gendoria\CommandQueue\Command\CommandInterface;
7
use Gendoria\CommandQueue\CommandProcessor\CommandProcessorInterface;
8
use Gendoria\CommandQueue\ProcessorFactory;
9
use Gendoria\CommandQueue\ProcessorFactoryInterface;
10
use Gendoria\CommandQueue\ProcessorNotFoundException;
11
use Gendoria\CommandQueue\Worker\Exception\ProcessorErrorException;
12
use Gendoria\CommandQueue\Worker\Exception\TranslateErrorException;
13
use Psr\Log\LoggerInterface;
14
use Psr\Log\NullLogger;
15
16
/**
17
 * Base command queue worker
18
 *
19
 * @author Tomasz Struczyński <[email protected]>
20
 */
21
abstract class BaseWorker implements WorkerInterface
22
{
23
24
    /**
25
     * Processor factory instance.
26
     *
27
     * @var ProcessorFactoryInterface
28
     */
29
    private $processorFactory;
30
31
    /**
32
     * Logger instance.
33
     *
34
     * @var LoggerInterface
35
     */
36
    private $logger;
37
38
    /**
39
     * Class constructor.
40
     *
41
     * @param ProcessorFactoryInterface         $processorFactory
42
     * @param LoggerInterface          $logger             Logger instance.
43
     */
44 4
    public function __construct(ProcessorFactoryInterface $processorFactory, LoggerInterface $logger = null)
45
    {
46 4
        $this->processorFactory = $processorFactory;
47 4
        $this->logger = $logger ? $logger : new NullLogger();
48 4
    }
49
50
    /**
51
     * 
52
     * @param mixed $commandData
53
     * @return void
54
     * 
55
     * @throws TranslateErrorException Thrown, when translation process resulted in an error.
56
     * @throws ProcessorNotFoundException Thrown, when processor for given command has not been found.
57
     * @throws ProcessorErrorException Thrown, when processor resulted in an error.
58
     */
59 4
    public function process($commandData)
60
    {
61 4
        $this->beforeTranslateHook($commandData);
62
        try {
63 4
            $command = $this->translateCommand($commandData);
64 4
        } catch (Exception $e) {
65 1
            throw new TranslateErrorException($commandData, $e->getMessage(), $e->getCode(), $e);
66
        }
67 3
        $this->beforeGetProcessorHook($command);
68 3
        $processor = $this->getProcessor($command);
69 2
        $this->beforeProcessHook($command, $processor);
70
        try {
71 2
            $processor->process($command);
72 2
        } catch (Exception $e) {
73 1
            throw new ProcessorErrorException($command, $processor, $e->getMessage(), $e->getCode(), $e);
74
        }
75 1
        $this->afterProcessHook($command, $processor);
76 1
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 3
    public function getProcessor(CommandInterface $command)
82
    {
83 3
        $processor = $this->processorFactory->getProcessor($command);
84 2
        $processor->setLogger($this->logger);
85 2
        return $processor;
86
    }
87
    
88
    /**
89
     * {@inheritdoc}
90
     */
91 1
    public function setProcessorFactory(ProcessorFactoryInterface $processorFactory)
92
    {
93 1
        $this->processorFactory = $processorFactory;
94 1
    }
95
    
96
    /**
97
     * Get command from command data.
98
     * 
99
     * @param mixed $commandData
100
     * 
101
     * @return CommandInterface
102
     * @throws \Exception Thrown, when translation has been unsuccessfull.
103
     */
104
    abstract protected function translateCommand($commandData);
105
106
    /**
107
     * Hook called before command translation.
108
     * 
109
     * @param string $commandData
110
     * @return void
111
     * 
112
     * @codeCoverageIgnore
113
     */
114
    protected function beforeTranslateHook(&$commandData)
115
    {
116
    }
117
    
118
    /**
119
     * Hook called before getting processor for command.
120
     * 
121
     * @param CommandInterface $command
122
     * @return void
123
     * 
124
     * @codeCoverageIgnore
125
     */
126
    protected function beforeGetProcessorHook(CommandInterface $command)
127
    {
128
    }
129
130
    /**
131
     * Hook called before processing command.
132
     * 
133
     * @param CommandInterface $command
134
     * @return void
135
     * 
136
     * @codeCoverageIgnore
137
     */
138
    protected function beforeProcessHook(CommandInterface $command, CommandProcessorInterface $processor)
139
    {
140
    }
141
    
142
    /**
143
     * Hook called after successfull processing of command.
144
     * 
145
     * @param CommandInterface $command
146
     * @param CommandProcessorInterface $processor
147
     * @return void
148
     * 
149
     * @codeCoverageIgnore
150
     */
151
    protected function afterProcessHook(CommandInterface $command, CommandProcessorInterface $processor)
152
    {
153
    }    
154
}
155