GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( e9c651...e1baf6 )
by Christian
02:19
created

LifecycleDateListener::loadClassMetadata()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 24
rs 9.2222
c 0
b 0
f 0
cc 6
nc 6
nop 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\LifecycleDateTimeInterface;
15
use Core23\Doctrine\Model\Traits\LifecycleDateTimeTrait;
16
use Doctrine\ORM\Event\LifecycleEventArgs;
17
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
18
use Doctrine\ORM\Events;
19
use Doctrine\ORM\Mapping\ClassMetadata;
20
use Doctrine\ORM\Mapping\MappingException;
21
22
final class LifecycleDateListener extends AbstractListener
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function getSubscribedEvents()
28
    {
29
        return [
30
            Events::prePersist,
31
            Events::preUpdate,
32
            Events::loadClassMetadata,
33
        ];
34
    }
35
36
    /**
37
     * Start lifecycle.
38
     *
39
     * @param LifecycleEventArgs $args
40
     */
41
    public function prePersist(LifecycleEventArgs $args): void
42
    {
43
        $object = $args->getObject();
44
45
        if ($object instanceof LifecycleDateTimeInterface) {
46
            $object->setCreatedAt(new \DateTime());
47
            $object->setUpdatedAt(new \DateTime());
48
        }
49
    }
50
51
    /**
52
     * Update LifecycleDateTime.
53
     *
54
     * @param LifecycleEventArgs $args
55
     */
56
    public function preUpdate(LifecycleEventArgs $args): void
57
    {
58
        $object = $args->getObject();
59
60
        if ($object instanceof LifecycleDateTimeInterface) {
61
            $object->setUpdatedAt(new \DateTime());
62
        }
63
    }
64
65
    /**
66
     * @param LoadClassMetadataEventArgs $eventArgs
67
     *
68
     * @throws MappingException
69
     */
70
    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
71
    {
72
        $meta = $eventArgs->getClassMetadata();
73
74
        if (!$meta instanceof ClassMetadata) {
0 ignored issues
show
introduced by
$meta is always a sub-type of Doctrine\ORM\Mapping\ClassMetadata.
Loading history...
75
            throw new \LogicException(sprintf('Class metadata was no ORM but %s', \get_class($meta)));
76
        }
77
78
        $reflClass = $meta->getReflectionClass();
79
80
        if (null === $reflClass || !$this->containsTrait($reflClass, LifecycleDateTimeTrait::class)) {
81
            return;
82
        }
83
84
        if (!$meta->hasField('createdAt')) {
85
            $meta->mapField([
86
                'type'      => 'datetime',
87
                'fieldName' => 'createdAt',
88
            ]);
89
        }
90
        if (!$meta->hasField('updatedAt')) {
91
            $meta->mapField([
92
                'type'      => 'datetime',
93
                'fieldName' => 'updatedAt',
94
            ]);
95
        }
96
    }
97
}
98