Passed
Push — master ( 88d705...44ae42 )
by Andrea
12:16
created

TableSchemaSubscriber::loadClassMetadata()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 43.2954

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 10
nop 1
dl 0
loc 25
ccs 2
cts 21
cp 0.0952
crap 43.2954
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace Fi\CoreBundle\Subscriber;
4
5
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
6
use Doctrine\ORM\Mapping\ClassMetadataInfo;
7
use Doctrine\ORM\Id\SequenceGenerator;
8
9
class TableSchemaSubscriber implements \Doctrine\Common\EventSubscriber
10
{
11
    protected $prefix = '';
12
13 42
    public function __construct($prefix)
14
    {
15 42
        $this->prefix = (string) $prefix;
16 42
    }
17
18 42
    public function getSubscribedEvents()
19
    {
20 42
        return array('loadClassMetadata');
21
    }
22
23 36
    public function loadClassMetadata(LoadClassMetadataEventArgs $args)
24
    {
25 36
        if ($this->prefix != '') {
26
            $classMetadata = $args->getClassMetadata();
27
28
            $classMetadata->setPrimaryTable(array('name' => $this->prefix.'.'.$classMetadata->getTableName()));
29
30
            foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
31
                $jointablename = $classMetadata->associationMappings[$fieldName]['joinTable']['name'];
32
                if ($mapping['type'] == ClassMetadataInfo::MANY_TO_MANY && isset($jointablename)) {
33
                    $mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name'];
34
                    $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix.'.'.$mappedTableName;
35
                }
36
            }
37
            if ($classMetadata->isIdGeneratorSequence()) {
38
                $newDefinition = $classMetadata->sequenceGeneratorDefinition;
39
                $newDefinition['sequenceName'] = $this->prefix.'.'.$newDefinition['sequenceName'];
40
41
                $classMetadata->setSequenceGeneratorDefinition($newDefinition);
42
                $em = $args->getEntityManager();
43
                if (isset($classMetadata->idGenerator)) {
44
                    $sequncename = $em->getConfiguration()->getQuoteStrategy()
45
                            ->getSequenceName($newDefinition, $classMetadata, $em->getConnection()->getDatabasePlatform());
46
                    $sequenceGenerator = new SequenceGenerator($sequncename, $newDefinition['allocationSize']);
0 ignored issues
show
Bug introduced by
It seems like $newDefinition['allocationSize'] can also be of type string; however, parameter $allocationSize of Doctrine\ORM\Id\SequenceGenerator::__construct() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

46
                    $sequenceGenerator = new SequenceGenerator($sequncename, /** @scrutinizer ignore-type */ $newDefinition['allocationSize']);
Loading history...
47
                    $classMetadata->setIdGenerator($sequenceGenerator);
48
                }
49
            }
50
        }
51 36
    }
52
}
53