Passed
Pull Request — feature/publishable (#31)
by Vincent
19:39 queued 09:22
created

PublishableListener::loadClassMetadata()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 11.8736

Importance

Changes 0
Metric Value
eloc 21
c 0
b 0
f 0
dl 0
loc 32
ccs 4
cts 19
cp 0.2105
rs 9.584
cc 4
nc 5
nop 1
crap 11.8736
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\Mapping\ClassMetadataInfo;
17
use Doctrine\ORM\Mapping\NamingStrategy;
18
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs;
19
use Silverback\ApiComponentBundle\Publishable\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->isPublishable($metadata->getName())) {
38 9
            return;
39
        }
40
41
        $configuration = $this->publishableHelper->getConfiguration($metadata->getName());
42
        /** @var NamingStrategy $namingStrategy */
43
        $namingStrategy = $eventArgs
44
            ->getEntityManager()
0 ignored issues
show
Bug introduced by
The method getEntityManager() does not exist on Doctrine\Persistence\Eve...dClassMetadataEventArgs. It seems like you code against a sub-type of Doctrine\Persistence\Eve...dClassMetadataEventArgs such as Doctrine\ORM\Event\LoadClassMetadataEventArgs. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
            ->/** @scrutinizer ignore-call */ getEntityManager()
Loading history...
45
            ->getConfiguration()
46
            ->getNamingStrategy();
47
48
        if (!$metadata->hasField($configuration->fieldName)) {
49
            $metadata->mapField([
50
                'fieldName' => $configuration->fieldName,
51
                'type' => 'date',
52
                'nullable' => true,
53
            ]);
54
        }
55
56
        if (!$metadata->hasAssociation($configuration->associationName)) {
57
            $metadata->mapOneToOne([
58
                'fieldName' => $configuration->associationName,
59
                'targetEntity' => $metadata->getName(),
60
                'joinColumns' => [
61
                    [
62
                        'name' => $namingStrategy->joinKeyColumnName($metadata->getName()),
63
                        'referencedColumnName' => $namingStrategy->referenceColumnName(),
64
                        'onDelete' => 'SET NULL',
65
                    ],
66
                ],
67
            ]);
68
        }
69
    }
70
}
71