Passed
Push — master ( 9378fd...6ee0b3 )
by Andrea
18:34 queued 11s
created

TableSchemaSubscriber::loadClassMetadata()   B

Complexity

Conditions 9
Paths 10

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 54.6649

Importance

Changes 0
Metric Value
cc 9
eloc 22
nc 10
nop 1
dl 0
loc 32
ccs 4
cts 23
cp 0.1739
crap 54.6649
rs 8.0555
c 0
b 0
f 0
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 52
    public function __construct($schemaprefix)
12
    {
13 52
        $this->schemaprefix = $schemaprefix;
14 52
    }
15
16 52
    public function getSubscribedEvents()
17
    {
18 52
        return array('loadClassMetadata');
19
    }
20
21 39
    public function loadClassMetadata(LoadClassMetadataEventArgs $args)
22
    {
23 39
        $classMetadata = $args->getClassMetadata();
24 39
        if (!$this->schemaprefix || ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity())) {
25 39
            return;
26
        }
27
        $classMetadata->setPrimaryTable(array('name' => $this->schemaprefix.'.'.$classMetadata->getTableName()));
28
        foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
29
            if (\Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY == $mapping['type'] &&
30
                    isset($classMetadata->associationMappings[$fieldName]['joinTable']['name'])
31
            ) {
32
                $mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 53 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
33
                $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->schemaprefix.'.'.$mappedTableName;
34
            }
35
        }
36
37
        if ($classMetadata->isIdGeneratorSequence()) {
38
            $newDefinition = $classMetadata->sequenceGeneratorDefinition;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 17 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
39
            $newDefinition['sequenceName'] = $this->schemaprefix.'.'.$newDefinition['sequenceName'];
40
41
            $classMetadata->setSequenceGeneratorDefinition($newDefinition);
42
            $em = $args->getEntityManager();
43
            if (isset($classMetadata->idGenerator)) {
44
                $sequenceGenerator = new \Doctrine\ORM\Id\SequenceGenerator(
45
                    $em->getConfiguration()->getQuoteStrategy()->getSequenceName(
46
                        $newDefinition,
47
                        $classMetadata,
48
                        $em->getConnection()->getDatabasePlatform()
49
                    ),
50
                    $newDefinition['allocationSize']
51
                );
52
                $classMetadata->setIdGenerator($sequenceGenerator);
53
            }
54
        }
55
    }
56
}
57