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