Passed
Pull Request — feature/publishable (#31)
by Vincent
05:42
created

PublishableEventSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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\Doctrine\EventSubscriber;
15
16
use Doctrine\Common\EventSubscriber;
17
use Doctrine\ORM\Events;
18
use Doctrine\ORM\Mapping\ClassMetadataInfo;
19
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs;
20
use Silverback\ApiComponentBundle\Entity\Utility\PublishableInterface;
21
22
/**
23
 * @author Vincent Chalamon <[email protected]>
24
 */
25
final class PublishableEventSubscriber implements EventSubscriber
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function getSubscribedEvents(): array
31
    {
32
        return [
33
            Events::loadClassMetadata,
34
        ];
35
    }
36
37
    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
38
    {
39
        /** @var ClassMetadataInfo $metadata */
40
        $metadata = $eventArgs->getClassMetadata();
41
        if (!is_a($metadata->getName(), PublishableInterface::class, true)) {
42
            return;
43
        }
44
45
        $metadata->mapOneToOne([
46
            'fieldName' => 'publishedResource',
47
            'targetEntity' => $metadata->getName(),
48
            'joinColumns' => [
49
                [
50
                    'name' => 'published_resource_id',
51
                    'referencedColumnName' => $metadata->getSingleIdentifierColumnName(),
52
                ],
53
            ],
54
        ]);
55
    }
56
}