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 | public function getSubscribedEvents() |
||
25 | { |
||
26 | return [ |
||
27 | Events::loadClassMetadata, |
||
28 | ]; |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @throws MappingException |
||
33 | */ |
||
34 | public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void |
||
35 | { |
||
36 | $meta = $eventArgs->getClassMetadata(); |
||
37 | |||
38 | if (!$meta instanceof ClassMetadata) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
39 | throw new LogicException('Class metadata was no ORM'); |
||
40 | } |
||
41 | |||
42 | $reflClass = $meta->getReflectionClass(); |
||
43 | |||
44 | if (null === $reflClass || !ClassUtils::containsTrait($reflClass, ConfirmableTrait::class)) { |
||
45 | return; |
||
46 | } |
||
47 | |||
48 | $this->createDateTimeField($meta, 'confirmedAt', true); |
||
49 | } |
||
50 | } |
||
51 |