Passed
Pull Request — master (#54)
by Vincent
06:15
created

PublishableListener   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 96.55%

Importance

Changes 0
Metric Value
wmc 7
eloc 31
c 0
b 0
f 0
dl 0
loc 54
ccs 28
cts 29
cp 0.9655
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B loadClassMetadata() 0 45 6
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component Bundle Project
5
 *
6
 * (c) Daniel West <[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
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentBundle\EventListener\Doctrine;
15
16
use Doctrine\ORM\EntityManagerInterface;
17
use Doctrine\ORM\Mapping\ClassMetadataInfo;
18
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs;
19
use Silverback\ApiComponentBundle\Helper\PublishableHelper;
20
21
/**
22
 * @author Vincent Chalamon <[email protected]>
23
 */
24
final class PublishableListener
25
{
26
    private PublishableHelper $publishableHelper;
27
28 9
    public function __construct(PublishableHelper $publishableHelper)
29
    {
30 9
        $this->publishableHelper = $publishableHelper;
31 9
    }
32
33 9
    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
34
    {
35
        /** @var ClassMetadataInfo $metadata */
36 9
        $metadata = $eventArgs->getClassMetadata();
37 9
        if (!$this->publishableHelper->isConfigured($metadata->getName())) {
38 9
            return;
39
        }
40
41 9
        $configuration = $this->publishableHelper->getConfiguration($metadata->getName());
42
43 9
        $em = $eventArgs->getObjectManager();
44 9
        if (!$em instanceof EntityManagerInterface) {
45
            return;
46
        }
47 9
        $namingStrategy = $em->getConfiguration()->getNamingStrategy();
48
49 9
        if (!$metadata->hasField($configuration->fieldName)) {
50 9
            $metadata->mapField([
51 9
                'fieldName' => $configuration->fieldName,
52 9
                'type' => 'datetime',
53
                'nullable' => true,
54
            ]);
55
        }
56
57 9
        if (!$metadata->hasAssociation($configuration->associationName)) {
58 9
            $metadata->mapOneToOne([
59 9
                'fieldName' => $configuration->associationName,
60 9
                'targetEntity' => $metadata->getName(),
61
                'joinColumns' => [
62
                    [
63 9
                        'name' => $namingStrategy->joinKeyColumnName($metadata->getName()),
64 9
                        'referencedColumnName' => $namingStrategy->referenceColumnName(),
65 9
                        'onDelete' => 'SET NULL',
66
                        'nullable' => true,
67
                    ],
68
                ],
69 9
                'inversedBy' => $configuration->reverseAssociationName,
70
            ]);
71
        }
72
73 9
        if (!$metadata->hasAssociation($configuration->reverseAssociationName)) {
74 9
            $metadata->mapOneToOne([
75 9
                'fieldName' => $configuration->reverseAssociationName,
76 9
                'targetEntity' => $metadata->getName(),
77 9
                'mappedBy' => $configuration->associationName,
78
            ]);
79
        }
80 9
    }
81
}
82