Passed
Push — develop ( 51f67e...bde052 )
by Andrea
12:00
created

TablePrefixSubscriber::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Cdf\BiCoreBundle\Subscriber;
4
5
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
6
7
class TablePrefixSubscriber implements \Doctrine\Common\EventSubscriber
8
{
9
    private string $tableprefix;
10
11 42
    public function __construct(string $tableprefix)
12
    {
13 42
        $this->tableprefix = $tableprefix;
14 42
    }
15
16 42
    public function getSubscribedEvents(): array
17
    {
18 42
        return array('loadClassMetadata');
19
    }
20
21 40
    public function loadClassMetadata(LoadClassMetadataEventArgs $args): void
22
    {
23 40
        $classMetadata = $args->getClassMetadata();
24 40
        if ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity()) {
25
            // if we are in an inheritance hierarchy, only apply this once
26 40
            return;
27
        }
28 40
        if (false !== strpos($classMetadata->namespace, 'Cdf\BiCoreBundle')) {
29 39
            $tableprefix = $this->tableprefix;
30 39
            $classMetadata->setPrimaryTable(array('name' => $tableprefix . $classMetadata->getTableName()));
31 39
            foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
32 37
                if (\Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY == $mapping['type'] &&
33 37
                    isset($classMetadata->associationMappings[$fieldName]['joinTable']['name'])
34
                ) {
35
                    $mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name'];
36
                    $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $tableprefix . $mappedTableName;
37
                }
38
            }
39
        }
40 40
    }
41
}
42