|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stu\Component\Event; |
|
4
|
|
|
|
|
5
|
|
|
use Stu\Component\Concurrency\EntityLockManagerInterface; |
|
6
|
|
|
use Stu\Component\Event\Strategy\EventStrategyInterface; |
|
7
|
|
|
use Stu\Module\Logging\LoggerTypeEnum; |
|
8
|
|
|
use Stu\Module\Logging\LoggerUtilFactoryInterface; |
|
9
|
|
|
use Stu\Module\Logging\RotatingLoggerInterface; |
|
10
|
|
|
use Stu\Orm\Entity\EventInterface; |
|
11
|
|
|
use Stu\Orm\Transaction\SwitchableEntityManagerInterface; |
|
12
|
|
|
|
|
13
|
|
|
class EventProcessor implements EventProcessorInterface |
|
14
|
|
|
{ |
|
15
|
|
|
private RotatingLoggerInterface $logger; |
|
16
|
|
|
|
|
17
|
2 |
|
public function __construct( |
|
18
|
|
|
private SwitchableEntityManagerInterface $switchableEntityManager, |
|
19
|
|
|
private EntityLockManagerInterface $entityLockManager, |
|
20
|
|
|
LoggerUtilFactoryInterface $loggerUtilFactory |
|
21
|
|
|
) { |
|
22
|
2 |
|
$this->logger = $loggerUtilFactory->getRotatingLogger(LoggerTypeEnum::EVENT_AND_ENTITY_LOCK); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
2 |
|
public function processEvent(EventInterface $event, EventStrategyInterface $eventStrategy): void |
|
26
|
|
|
{ |
|
27
|
|
|
try { |
|
28
|
2 |
|
$startTime = microtime(true); |
|
29
|
2 |
|
$this->switchableEntityManager->wrapInTransaction(function () use ($event, $eventStrategy) { |
|
30
|
|
|
|
|
31
|
2 |
|
$startTime = microtime(true); |
|
32
|
2 |
|
$eventStrategy->processEvent($event); |
|
33
|
2 |
|
$endTime = microtime(true); |
|
34
|
|
|
|
|
35
|
2 |
|
$this->logger->logf(' processing took %F ms', ($endTime - $startTime) * 1000); |
|
36
|
2 |
|
}); |
|
37
|
1 |
|
$endTime = microtime(true); |
|
38
|
1 |
|
$this->logger->logf(' transaction took %F ms', ($endTime - $startTime) * 1000); |
|
39
|
|
|
} finally { |
|
40
|
2 |
|
$this->entityLockManager->clearEntityLocks(); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|