Passed
Push — master ( 791d56...81a424 )
by Damien
02:11 queued 12s
created

AuditEventSubscriber::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
c 1
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DH\Auditor\EventSubscriber;
6
7
use DH\Auditor\Auditor;
8
use DH\Auditor\Event\LifecycleEvent;
9
use Exception;
10
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11
12
/**
13
 * @see \DH\Auditor\Tests\EventSubscriber\AuditEventSubscriberTest
14
 */
15
final class AuditEventSubscriber implements EventSubscriberInterface
16
{
17
    private Auditor $auditor;
18
19
    public function __construct(Auditor $auditor)
20
    {
21
        $this->auditor = $auditor;
22
    }
23
24
    public static function getSubscribedEvents(): array
25
    {
26
        return [
27
            LifecycleEvent::class => [
28
                ['onAuditEvent', -1_000_000],  // should be fired last
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting ',' or ']' on line 28 at column 35
Loading history...
29
            ],
30
        ];
31
    }
32
33
    public function onAuditEvent(LifecycleEvent $event): LifecycleEvent
34
    {
35
        foreach ($this->auditor->getProviders() as $provider) {
36
            if ($provider->supportsStorage()) {
37
                try {
38
                    $provider->persist($event);
39
                } catch (Exception) {
40
                    // do nothing to ensure other providers are called
41
                }
42
            }
43
        }
44
45
        return $event;
46
    }
47
}
48