DefaultEventListener::prePersist()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 4
c 2
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace ControleOnline\Listener;
4
5
use ControleOnline\Service\ExtraDataService;
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\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...
9
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...
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 Psr\Container\ContainerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Container\ContainerInterface 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
14
15
class DefaultEventListener
16
{
17
    public function __construct(
18
        private EntityManagerInterface $manager,
19
        private ExtraDataService $extraDataService,
20
        private ContainerInterface $container,
21
    ) {}
22
23
    public function preUpdate(PreUpdateEventArgs $args): void
24
    {
25
        $this->execute($args->getObject(), 'preUpdate');
26
    }
27
28
    public function prePersist(PrePersistEventArgs $args): void
29
    {
30
        $entity = $args->getObject();
31
        $this->extraDataService->discoveryDevice($entity);
32
        $this->extraDataService->discoveryUser($entity);
33
        $this->execute($entity, 'prePersist');
34
    }
35
36
    public function postUpdate(PostUpdateEventArgs $args): void
37
    {
38
        $this->execute($args->getObject(), 'postUpdate');
39
    }
40
41
    public function postPersist(PostPersistEventArgs $args): void
42
    {
43
        $this->execute($args->getObject(), 'postPersist');
44
    }
45
46
    public function preRemove(PreRemoveEventArgs $args): void
47
    {
48
        $this->execute($args->getObject(), 'preRemove');
49
    }
50
51
    private function execute($entity, $method)
52
    {
53
        $class = get_class($entity);
54
        $serviceName = str_replace('Entity', 'Service', $class) . 'Service';
55
        $this->extraDataService->persist($entity);
56
        if ($this->container->has($serviceName)) {
57
            $service = $this->container->get($serviceName);
58
            if (method_exists($service, $method)) {
59
                $newEntity = $service->$method($entity);
60
61
                if ('postPersist' === $method && $newEntity)
62
                    $this->manager->refresh($newEntity);
63
            }
64
        }
65
        return $entity;
66
    }
67
}
68