DeletableListener   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 8
dl 0
loc 23
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A loadClassMetadata() 0 11 3
A getSubscribedEvents() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Christian Gripp <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Core23\Doctrine\EventListener\ORM;
13
14
use Core23\Doctrine\Model\Traits\DeleteableTrait;
15
use Core23\Doctrine\Util\ClassUtils;
16
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
17
use Doctrine\ORM\Events;
18
use Doctrine\ORM\Mapping\MappingException;
19
20
final class DeletableListener extends AbstractListener
21
{
22
    public function getSubscribedEvents()
23
    {
24
        return [
25
            Events::loadClassMetadata,
26
        ];
27
    }
28
29
    /**
30
     * @throws MappingException
31
     */
32
    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
33
    {
34
        $meta = $eventArgs->getClassMetadata();
35
36
        $reflClass = $meta->getReflectionClass();
37
38
        if (null === $reflClass || !ClassUtils::containsTrait($reflClass, DeleteableTrait::class)) {
39
            return;
40
        }
41
42
        $this->createDateTimeField($meta, 'deletedAt', true);
43
    }
44
}
45