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\ConfirmableTrait; |
||
15 | use Doctrine\ORM\Event\LoadClassMetadataEventArgs; |
||
16 | use Doctrine\ORM\Events; |
||
17 | use Doctrine\ORM\Mapping\ClassMetadata; |
||
18 | use Doctrine\ORM\Mapping\MappingException; |
||
19 | |||
20 | final class ConfirmableListener extends AbstractListener |
||
21 | { |
||
22 | /** |
||
23 | * {@inheritdoc} |
||
24 | */ |
||
25 | public function getSubscribedEvents() |
||
26 | { |
||
27 | return [ |
||
28 | Events::loadClassMetadata, |
||
29 | ]; |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * @param LoadClassMetadataEventArgs $eventArgs |
||
34 | * |
||
35 | * @throws MappingException |
||
36 | */ |
||
37 | public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void |
||
38 | { |
||
39 | $meta = $eventArgs->getClassMetadata(); |
||
40 | |||
41 | if (!$meta instanceof ClassMetadata) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
42 | throw new \LogicException(sprintf('Class metadata was no ORM but %s', \get_class($meta))); |
||
43 | } |
||
44 | |||
45 | $reflClass = $meta->getReflectionClass(); |
||
46 | |||
47 | if (null === $reflClass || !$this->containsTrait($reflClass, ConfirmableTrait::class)) { |
||
48 | return; |
||
49 | } |
||
50 | |||
51 | if (!$meta->hasField('confirmedAt')) { |
||
52 | $meta->mapField([ |
||
53 | 'type' => 'datetime', |
||
54 | 'fieldName' => 'confirmedAt', |
||
55 | 'nullable' => true, |
||
56 | ]); |
||
57 | } |
||
58 | } |
||
59 | } |
||
60 |