LogListener::postRemoveHandler()   A
last analyzed

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
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace ControleOnline\Listener;
4
5
use ControleOnline\Entity\User;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Doctrine\ORM\EntityManagerInterface;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\EntityManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Doctrine\ORM\Event\PrePersistEventArgs;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\Event\PrePersistEventArgs was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Doctrine\ORM\Event\PostPersistEventArgs;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\Event\PostPersistEventArgs was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Doctrine\ORM\Event\PreUpdateEventArgs;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\Event\PreUpdateEventArgs was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Doctrine\ORM\Event\PostUpdateEventArgs;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\Event\PostUpdateEventArgs was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Doctrine\ORM\Event\PreRemoveEventArgs;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\Event\PreRemoveEventArgs was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Doctrine\ORM\Event\PostRemoveEventArgs;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\Event\PostRemoveEventArgs was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Doctrine\ORM\Event\PreFlushEventArgs;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\Event\PreFlushEventArgs was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Doctrine\ORM\Event\PostLoadEventArgs;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\Event\PostLoadEventArgs was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
class LogListener
17
{
18
    /**
19
     * @var array<string, mixed>[]
20
     */
21
    private array $log = [];
22
23
    /**
24
     * @var object|null
25
     */
26
    private $user = null;
27
28
    public function prePersistHandler(PrePersistEventArgs $event): void
29
    {
30
        $this->logEntity($event->getObject(), 'prePersist', $event->getObjectManager());
31
    }
32
33
    public function postPersistHandler(PostPersistEventArgs $event): void
34
    {
35
        $this->logEntity($event->getObject(), 'postPersist', $event->getObjectManager());
36
    }
37
38
    public function preUpdateHandler(PreUpdateEventArgs $event): void
39
    {
40
        $this->logEntity($event->getObject(), 'preUpdate', $event->getObjectManager());
41
    }
42
43
    public function postUpdateHandler(PostUpdateEventArgs $event): void
44
    {
45
        $this->logEntity($event->getObject(), 'postUpdate', $event->getObjectManager());
46
    }
47
48
    public function preRemoveHandler(PreRemoveEventArgs $event): void
49
    {
50
        $this->logEntity($event->getObject(), 'preRemove', $event->getObjectManager());
51
    }
52
53
    public function postRemoveHandler(PostRemoveEventArgs $event): void
54
    {
55
        $this->logEntity($event->getObject(), 'postRemove', $event->getObjectManager());
56
    }
57
58
    public function preFlushHandler(PreFlushEventArgs $event): void
59
    {
60
        $this->logEntity(null, 'preFlush', $event->getObjectManager());
61
    }
62
63
    public function postLoadHandler(PostLoadEventArgs $event): void
64
    {
65
        $this->logEntity($event->getObject(), 'postLoad', $event->getObjectManager());
66
    }
67
68
    private function logEntity(?object $entity, string $action, EntityManagerInterface $em): void
69
    {
70
71
        return;
72
        if ($entity === null && $action !== 'preFlush') {
0 ignored issues
show
Unused Code introduced by
IfNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
73
            return;
74
        }
75
76
        if ($entity && $entity instanceof User) {
77
            $this->user = $entity;
78
            return;
79
        }
80
81
        if ($entity) {
82
            $className = $em->getClassMetadata(get_class($entity))->getName();
83
            $this->log[] = [
84
                'action' => $action,
85
                'class' => $className,
86
                'object' => $this->getObject($entity, $em),
87
            ];
88
        }
89
    }
90
91
    /**
92
     * @return array<string, mixed>
93
     */
94
    private function getObject(object $entity, EntityManagerInterface $em): array
95
    {
96
        $methods = preg_grep('/^get/', get_class_methods($entity));
97
        $array = [];
98
99
        foreach ($methods as $method) {
100
            $content = $entity->$method();
101
            $property = lcfirst(substr($method, 3));
102
103
            if (!is_object($content)) {
104
                $array[$property] = $content;
105
            } elseif (!$content instanceof \Doctrine\ORM\PersistentCollection) {
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\PersistentCollection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
106
                $array[$property] = $this->findIdentifier($content, $em);
107
            } else {
108
                $array[$property] = [];
109
                foreach ($content as $item) {
110
                    $array[$property][] = $this->findIdentifier($item, $em);
111
                }
112
            }
113
        }
114
115
        return $array;
116
    }
117
118
    private function findIdentifier(object $entity, EntityManagerInterface $em): mixed
119
    {
120
        $meta = $em->getClassMetadata(get_class($entity));
121
        $identifier = $meta->getSingleIdentifierFieldName();
122
        $getter = 'get' . ucfirst($identifier);
123
124
        if (!method_exists($entity, $getter)) {
125
            return null;
126
        }
127
128
        $value = $entity->$getter();
129
        if (is_object($value)) {
130
            return $this->findIdentifier($value, $em);
131
        }
132
133
        return $value;
134
    }
135
136
    public function flushLogs(EntityManagerInterface $em): void
137
    {
138
        foreach ($this->log as $logData) {
139
            $log = new \ControleOnline\Entity\Log();
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\Log was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
140
            $log->setObject(json_encode($logData['object']));
141
            $log->setUser($this->user);
142
            $log->setAction($logData['action']);
143
            $log->setClass($logData['class']);
144
145
            $em->persist($log);
146
        }
147
148
        $this->log = [];
149
        $em->flush();
150
    }
151
}
152