EventFlusherSubscriber   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 2
cbo 1
dl 0
loc 48
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getSubscribedEvents() 0 9 1
A flushEvents() 0 4 1
A resetEvents() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Infrastructure\EventDispatcher;
16
17
use Acme\App\Core\Port\EventDispatcher\BufferedEventDispatcherInterface;
18
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
19
use Symfony\Component\HttpKernel\KernelEvents;
20
21
final class EventFlusherSubscriber implements EventSubscriberInterface
22
{
23
    private const DEFAULT_PRIORITY = 5;
24
25
    /**
26
     * @var BufferedEventDispatcherInterface
27
     */
28
    private $bufferedEventDispatcher;
29
30
    /**
31
     * @var int
32
     */
33
    private static $priority = self::DEFAULT_PRIORITY;
34
35
    public function __construct(
36
        BufferedEventDispatcherInterface $bufferedEventDispatcher,
37
        int $eventFlusherSubscriberPriority = self::DEFAULT_PRIORITY
38
    ) {
39
        $this->bufferedEventDispatcher = $bufferedEventDispatcher;
40
        self::$priority = $eventFlusherSubscriberPriority;
41
    }
42
43
    /**
44
     * Return the subscribed events, their methods and possibly their priorities
45
     * (the higher the priority the earlier the method is called).
46
     *
47
     * @see http://symfony.com/doc/current/event_dispatcher.html#creating-an-event-subscriber
48
     */
49
    public static function getSubscribedEvents(): array
50
    {
51
        return [
52
            KernelEvents::TERMINATE => ['flushEvents', self::$priority],
53
            // In the case that both the Exception and Response events are triggered, we want to make sure the
54
            // events will not be dispatched.
55
            KernelEvents::EXCEPTION => ['resetEvents', self::$priority + 1],
56
        ];
57
    }
58
59
    public function flushEvents(): void
60
    {
61
        $this->bufferedEventDispatcher->flush();
62
    }
63
64
    public function resetEvents(): void
65
    {
66
        $this->bufferedEventDispatcher->reset();
67
    }
68
}
69