Completed
Push — master ( efdc68...e73f70 )
by Tomasz
03:28
created

BaseSymfonyWorker::beforeGetProcessorHook()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
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\QueueBeforeGetProcessorEvent;
11
use Gendoria\CommandQueueBundle\Event\QueueBeforeTranslateEvent;
12
use Gendoria\CommandQueueBundle\Event\QueueEvents;
13
use Gendoria\CommandQueueBundle\Event\QueueProcessErrorEvent;
14
use Gendoria\CommandQueueBundle\Event\QueueProcessEvent;
15
use Monolog\Handler\FingersCrossedHandler;
16
use Monolog\Logger;
17
use Psr\Log\LoggerInterface;
18
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19
20
/**
21
 * Base for Symfony based queue workers.
22
 * 
23
 * This class takes care of correctly handling long-running process logs, 
24
 * as well as sending some events on Symfony event bus.
25
 *
26
 * @author Tomasz Struczyński <[email protected]>
27
 */
28
abstract class BaseSymfonyWorker extends BaseWorker
29
{
30
    /**
31
     * Symfony event dispatcher.
32
     *
33
     * @var EventDispatcherInterface
34
     */
35
    private $eventDispatcher;
36
    
37
    public function __construct(ProcessorFactoryInterface $processorFactory, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger = null)
38
    {
39
        parent::__construct($processorFactory, $logger);
40
        $this->eventDispatcher = $eventDispatcher;
41
    }
42
43
    /**
44
     * Get subsystem name.
45
     * 
46
     * This name may be used further to identify worker subsystem in logs or events.
47
     * 
48
     * @return string
49
     */
50
    abstract public function getSubsystemName();
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    protected function beforeTranslateHook(&$commandData)
56
    {
57
        parent::beforeTranslateHook($commandData);
58
59
        $this->eventDispatcher->dispatch(QueueEvents::WORKER_RUN_BEFORE_TRANSLATE, new QueueBeforeTranslateEvent($this, $commandData, $this->getSubsystemName()));
60
        $this->clearLogs();
61
    }
62
    
63
    /**
64
     * {@inheritdoc}
65
     */
66
    protected function beforeGetProcessorHook(CommandInterface $command)
67
    {
68
        parent::beforeGetProcessorHook($command);
69
        $this->eventDispatcher->dispatch(QueueEvents::WORKER_RUN_BEFORE_GET_PROCESSOR, new QueueBeforeGetProcessorEvent($this, $command, $this->getSubsystemName()));
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    protected function beforeProcessHook(CommandInterface $command, CommandProcessorInterface $processor)
76
    {
77
        parent::beforeProcessHook($command, $processor);
78
        $this->eventDispatcher->dispatch(QueueEvents::WORKER_RUN_BEFORE_PROCESS, new QueueProcessEvent($this, $command, $processor, $this->getSubsystemName()));
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    protected function afterProcessHook(CommandInterface $command, CommandProcessorInterface $processor)
85
    {
86
        parent::afterProcessHook($command, $processor);
87
        $this->eventDispatcher->dispatch(QueueEvents::WORKER_RUN_AFTER_PROCESS, new QueueProcessEvent($this, $command, $processor, $this->getSubsystemName()));
88
        $this->clearLogs();
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    protected function processorErrorHook(CommandInterface $command, CommandProcessorInterface $processor, Exception $e)
95
    {
96
        parent::processorErrorHook($command, $processor, $e);
97
        $this->eventDispatcher->dispatch(QueueEvents::WORKER_RUN_PROCESSOR_ERROR, new QueueProcessErrorEvent($this, $command, $processor, $e, $this->getSubsystemName()));
98
    }
99
100
    /**
101
     * Clear logs.
102
     * 
103
     * @return void
104
     */
105
    private function clearLogs()
106
    {
107
        if (!$this->logger instanceof Logger) {
108
            return;
109
        }
110
        /* @var $logger Logger */
111
        $logger = $this->logger;
112
113
        foreach ($logger->getHandlers() as $handler) {
114
            if ($handler instanceof FingersCrossedHandler) {
115
                $handler->clear();
116
            }
117
        }
118
    }
119
120
}
121