Completed
Push — master ( 75271b...efdc68 )
by Tomasz
04:57
created

BaseSymfonyWorker   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 75
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
getSubsystemName() 0 1 ?
A beforeTranslateHook() 0 7 1
A afterProcessHook() 0 6 1
A processorErrorHook() 0 5 1
A clearLogs() 0 14 4
1
<?php
2
3
namespace Gendoria\CommandQueueBundle\Worker;
4
5
use Exception;
6
use Gendoria\CommandQueue\Command\CommandInterface;
7
use Gendoria\CommandQueue\CommandProcessor\CommandProcessorInterface;
8
use Gendoria\CommandQueue\ProcessorFactoryInterface;
9
use Gendoria\CommandQueue\Worker\BaseWorker;
10
use Gendoria\CommandQueueBundle\Event\QueueEvents;
11
use Gendoria\CommandQueueBundle\Event\QueueWorkerRunEvent;
12
use Monolog\Handler\FingersCrossedHandler;
13
use Monolog\Logger;
14
use Psr\Log\LoggerInterface;
15
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16
17
/**
18
 * Base for Symfony based queue workers.
19
 * 
20
 * This class takes care of correctly handling long-running process logs, 
21
 * as well as sending some events on Symfony event bus.
22
 *
23
 * @author Tomasz Struczyński <[email protected]>
24
 */
25
abstract class BaseSymfonyWorker extends BaseWorker
26
{
27
    /**
28
     * Symfony event dispatcher.
29
     *
30
     * @var EventDispatcherInterface
31
     */
32
    private $eventDispatcher;
33
    
34
    public function __construct(ProcessorFactoryInterface $processorFactory, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger = null)
35
    {
36
        parent::__construct($processorFactory, $logger);
37
        $this->eventDispatcher = $eventDispatcher;
38
    }
39
40
    /**
41
     * Get subsystem name.
42
     * 
43
     * This name may be used further to identify worker subsystem in logs or events.
44
     * 
45
     * @return string
46
     */
47
    abstract public function getSubsystemName();
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function beforeTranslateHook(&$commandData)
53
    {
54
        parent::beforeTranslateHook($commandData);
55
56
        $this->eventDispatcher->dispatch(QueueEvents::WORKER_RUN_BEFORE_TRANSLATE, new QueueWorkerRunEvent($this, $this->getSubsystemName()));
57
        $this->clearLogs();
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    protected function afterProcessHook(CommandInterface $command, CommandProcessorInterface $processor)
64
    {
65
        parent::afterProcessHook($command, $processor);
66
        $this->eventDispatcher->dispatch(QueueEvents::WORKER_RUN_AFTER_PROCESS, new QueueWorkerRunEvent($this, $this->getSubsystemName()));
67
        $this->clearLogs();
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    protected function processorErrorHook(CommandInterface $command, CommandProcessorInterface $processor, Exception $e)
74
    {
75
        parent::processorErrorHook($command, $processor, $e);
76
        $this->eventDispatcher->dispatch(QueueEvents::WORKER_RUN_PROCESSOR_ERROR, new QueueWorkerRunEvent($this, $this->getSubsystemName()));
77
    }
78
79
    /**
80
     * Clear logs.
81
     * 
82
     * @return void
83
     */
84
    private function clearLogs()
85
    {
86
        if (!$this->logger instanceof Logger) {
87
            return;
88
        }
89
        /* @var $logger Logger */
90
        $logger = $this->logger;
91
92
        foreach ($logger->getHandlers() as $handler) {
93
            if ($handler instanceof FingersCrossedHandler) {
94
                $handler->clear();
95
            }
96
        }
97
    }
98
99
}
100