Passed
Push — master ( d19abb...9378fd )
by Andrea
20:33
created

TableSchemaSubscriber   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 46
ccs 26
cts 28
cp 0.9286
rs 10
c 0
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 3 1
A __construct() 0 3 1
B loadClassMetadata() 0 32 9
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 39
        $classMetadata->setPrimaryTable(array('name' => $this->schemaprefix.'.'.$classMetadata->getTableName()));
28 39
        foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
29 37
            if (\Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY == $mapping['type'] &&
30 37
                    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 39
        if ($classMetadata->isIdGeneratorSequence()) {
38 39
            $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 39
            $newDefinition['sequenceName'] = $this->schemaprefix.'.'.$newDefinition['sequenceName'];
40
41 39
            $classMetadata->setSequenceGeneratorDefinition($newDefinition);
42 39
            $em = $args->getEntityManager();
43 39
            if (isset($classMetadata->idGenerator)) {
44 39
                $sequenceGenerator = new \Doctrine\ORM\Id\SequenceGenerator(
45 39
                    $em->getConfiguration()->getQuoteStrategy()->getSequenceName(
46 39
                        $newDefinition,
47 39
                        $classMetadata,
48 39
                        $em->getConnection()->getDatabasePlatform()
49
                    ),
50 39
                    $newDefinition['allocationSize']
51
                );
52 39
                $classMetadata->setIdGenerator($sequenceGenerator);
53
            }
54
        }
55 39
    }
56
}
57