Completed
Push — master ( dfcaa1...caabc7 )
by Thomas Mauro
07:54
created

getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\Laminas\Messenger\Subscriber;
6
7
use Doctrine\Persistence\ManagerRegistry;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
10
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent;
11
12
class DoctrineClearEntityManagerWorkerSubscriber implements EventSubscriberInterface
13
{
14
    /** @var ManagerRegistry */
15
    private $managerRegistry;
16
17 3
    public function __construct(ManagerRegistry $managerRegistry)
18
    {
19 3
        $this->managerRegistry = $managerRegistry;
20 3
    }
21
22 1
    public function onWorkerMessageHandled(): void
23
    {
24 1
        $this->clearEntityManagers();
25 1
    }
26
27 1
    public function onWorkerMessageFailed(): void
28
    {
29 1
        $this->clearEntityManagers();
30 1
    }
31
32
    /**
33
     * @return iterable<string, string>
0 ignored issues
show
Documentation introduced by
The doc-type iterable<string, could not be parsed: Expected "|" or "end of type", but got "<" at position 8. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
34
     */
35 1
    public static function getSubscribedEvents(): iterable
36
    {
37 1
        yield WorkerMessageHandledEvent::class => 'onWorkerMessageHandled';
38 1
        yield WorkerMessageFailedEvent::class => 'onWorkerMessageFailed';
39 1
    }
40
41 2
    private function clearEntityManagers(): void
42
    {
43 2
        foreach ($this->managerRegistry->getManagers() as $manager) {
44 2
            $manager->clear();
45
        }
46 2
    }
47
}
48