AuditSubscriber   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 63
rs 10
c 1
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A onAuditEvent() 0 37 2
A getSubscribedEvents() 0 4 1
1
<?php
2
3
namespace DH\DoctrineAuditBundle\Event;
4
5
use DH\DoctrineAuditBundle\Transaction\TransactionManager;
6
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7
8
class AuditSubscriber implements EventSubscriberInterface
9
{
10
    /**
11
     * @var TransactionManager
12
     */
13
    private $transactionManager;
14
15
    public function __construct(TransactionManager $transactionManager)
16
    {
17
        $this->transactionManager = $transactionManager;
18
    }
19
20
    public static function getSubscribedEvents(): array
21
    {
22
        return [
23
            LifecycleEvent::class => 'onAuditEvent',
24
        ];
25
    }
26
27
    /**
28
     * @param LifecycleEvent $event
29
     *
30
     * @throws \Doctrine\DBAL\DBALException
31
     *
32
     * @return LifecycleEvent
33
     */
34
    public function onAuditEvent(LifecycleEvent $event): LifecycleEvent
35
    {
36
        $payload = $event->getPayload();
37
        $auditTable = $payload['table'];
38
        unset($payload['table'], $payload['entity']);
39
40
        $fields = [
41
            'type' => ':type',
42
            'object_id' => ':object_id',
43
            'discriminator' => ':discriminator',
44
            'transaction_hash' => ':transaction_hash',
45
            'diffs' => ':diffs',
46
            'blame_id' => ':blame_id',
47
            'blame_user' => ':blame_user',
48
            'blame_user_fqdn' => ':blame_user_fqdn',
49
            'blame_user_firewall' => ':blame_user_firewall',
50
            'ip' => ':ip',
51
            'created_at' => ':created_at',
52
        ];
53
54
        $query = sprintf(
55
            'INSERT INTO %s (%s) VALUES (%s)',
56
            $auditTable,
57
            implode(', ', array_keys($fields)),
58
            implode(', ', array_values($fields))
59
        );
60
61
        $storage = $this->transactionManager->selectStorageSpace($this->transactionManager->getConfiguration()->getEntityManager());
62
        $statement = $storage->getConnection()->prepare($query);
63
64
        foreach ($payload as $key => $value) {
65
            $statement->bindValue($key, $value);
66
        }
67
68
        $statement->execute();
69
70
        return $event;
71
    }
72
}
73