Passed
Push — master ( 046444...56e09b )
by Andrea
28:16 queued 22:41
created

TableSchemaSubscriber::__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
use Doctrine\ORM\Id\SequenceGenerator;
7
8
class TableSchemaSubscriber implements \Doctrine\Common\EventSubscriber
9
{
10
    private $schemaprefix;
11
12 42
    public function __construct($schemaprefix)
13
    {
14 42
        $this->schemaprefix = $schemaprefix;
15 42
    }
16
17 42
    public function getSubscribedEvents()
18
    {
19 42
        return array('loadClassMetadata');
20
    }
21
22 40
    public function loadClassMetadata(LoadClassMetadataEventArgs $args)
23
    {
24 40
        $classMetadata = $args->getClassMetadata();
25 40
        if (!$this->schemaprefix || ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity())) {
26 40
            return;
27
        }
28 40
        $classMetadata->setPrimaryTable(array('name' => $this->schemaprefix.'.'.$classMetadata->getTableName()));
29 40
        foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
30 38
            if (\Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY == $mapping['type'] &&
31 38
                    isset($classMetadata->associationMappings[$fieldName]['joinTable']['name'])
32
            ) {
33
                $mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name'];
34
                $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->schemaprefix.'.'.$mappedTableName;
35
            }
36
        }
37
38 40
        if ($classMetadata->isIdGeneratorSequence()) {
39 40
            $newDefinition = $classMetadata->sequenceGeneratorDefinition;
40 40
            $newDefinition['sequenceName'] = $this->schemaprefix.'.'.$newDefinition['sequenceName'];
41
42 40
            $classMetadata->setSequenceGeneratorDefinition($newDefinition);
43 40
            $em = $args->getEntityManager();
44 40
            if (isset($classMetadata->idGenerator)) {
45 40
                $sequenceGenerator = new SequenceGenerator(
46 40
                    $em->getConfiguration()->getQuoteStrategy()->getSequenceName(
47 40
                        $newDefinition,
48
                        $classMetadata,
49 40
                        $em->getConnection()->getDatabasePlatform()
50
                    ),
51 40
                    $newDefinition['allocationSize']
52
                );
53 40
                $classMetadata->setIdGenerator($sequenceGenerator);
54
            }
55
        }
56 40
    }
57
}
58