1 | <?php |
||
2 | |||
3 | /* |
||
4 | * (c) Christian Gripp <[email protected]> |
||
5 | * |
||
6 | * For the full copyright and license information, please view the LICENSE |
||
7 | * file that was distributed with this source code. |
||
8 | */ |
||
9 | |||
10 | namespace Core23\DoctrineExtensions\EventListener\ORM; |
||
11 | |||
12 | use Core23\DoctrineExtensions\Model\Traits\DeleteableTrait; |
||
13 | use Doctrine\ORM\Event\LoadClassMetadataEventArgs; |
||
14 | use Doctrine\ORM\Events; |
||
15 | use Doctrine\ORM\Mapping\ClassMetadata; |
||
16 | use Doctrine\ORM\Mapping\MappingException; |
||
17 | |||
18 | View Code Duplication | final class DeletableListener extends AbstractListener |
|
0 ignored issues
–
show
|
|||
19 | { |
||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | */ |
||
23 | public function getSubscribedEvents() |
||
24 | { |
||
25 | return array( |
||
26 | Events::loadClassMetadata, |
||
27 | ); |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * @param LoadClassMetadataEventArgs $eventArgs |
||
32 | * |
||
33 | * @throws MappingException |
||
34 | */ |
||
35 | public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void |
||
36 | { |
||
37 | $meta = $eventArgs->getClassMetadata(); |
||
38 | |||
39 | if (!$meta instanceof ClassMetadata) { |
||
40 | throw new \LogicException(sprintf('Class metadata was no ORM but %s', get_class($meta))); |
||
41 | } |
||
42 | |||
43 | if (!$this->containsTrait($meta->getReflectionClass(), DeleteableTrait::class)) { |
||
44 | return; |
||
45 | } |
||
46 | |||
47 | if (!$meta->hasField('deletedAt')) { |
||
48 | $meta->mapField(array( |
||
49 | 'type' => 'datetime', |
||
50 | 'fieldName' => 'deletedAt', |
||
51 | 'nullable' => true, |
||
52 | )); |
||
53 | } |
||
54 | } |
||
55 | } |
||
56 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.