Passed
Push — dev ( eeaa0f...91a85a )
by Janko
26:10
created

EventProcessor::processEvent()   A

Complexity

Conditions 1
Paths 5

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 5
nop 2
dl 0
loc 16
ccs 11
cts 11
cp 1
crap 1
rs 9.9
c 0
b 0
f 0
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