Passed
Push — master ( 949b33...6e3bb1 )
by Andrea
73:17 queued 36:43
created

TableSchemaSubscriber::getSubscribedEvents()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Cdf\BiCoreBundle\Subscriber;
4
5
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
6
7
class TableSchemaSubscriber implements \Doctrine\Common\EventSubscriber
8
{
9
    private $schemaprefix;
10
11 48
    public function __construct($schemaprefix)
12
    {
13 48
        $this->schemaprefix = $schemaprefix;
14 48
    }
15
16 48
    public function getSubscribedEvents()
17
    {
18 48
        return array('loadClassMetadata');
19
    }
20
21 40
    public function loadClassMetadata(LoadClassMetadataEventArgs $args)
22
    {
23 40
        $classMetadata = $args->getClassMetadata();
24 40
        if (!$this->schemaprefix || ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity())) {
25 40
            return;
26
        }
27 40
        $classMetadata->setPrimaryTable(array('name' => $this->schemaprefix.'.'.$classMetadata->getTableName()));
28 40
        foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
29 38
            if (\Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY == $mapping['type'] &&
30 38
                    isset($classMetadata->associationMappings[$fieldName]['joinTable']['name'])
31
            ) {
32
                $mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name'];
33
                $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->schemaprefix.'.'.$mappedTableName;
34
            }
35
        }
36
37 40
        if ($classMetadata->isIdGeneratorSequence()) {
38 40
            $newDefinition = $classMetadata->sequenceGeneratorDefinition;
39 40
            $newDefinition['sequenceName'] = $this->schemaprefix.'.'.$newDefinition['sequenceName'];
40
41 40
            $classMetadata->setSequenceGeneratorDefinition($newDefinition);
42 40
            $em = $args->getEntityManager();
43 40
            if (isset($classMetadata->idGenerator)) {
44 40
                $sequenceGenerator = new \Doctrine\ORM\Id\SequenceGenerator(
45 40
                    $em->getConfiguration()->getQuoteStrategy()->getSequenceName(
46 40
                        $newDefinition,
47 40
                        $classMetadata,
48 40
                        $em->getConnection()->getDatabasePlatform()
49
                    ),
50 40
                    $newDefinition['allocationSize']
51
                );
52 40
                $classMetadata->setIdGenerator($sequenceGenerator);
53
            }
54
        }
55 40
    }
56
}
57