Issues (38)

src/EventSubscriber/AuditEventSubscriber.php (1 issue)

Labels
Severity
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
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