Passed
Push — master ( 48f148...06d57e )
by Andrea
36:47
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 51
    public function __construct($schemaprefix)
12
    {
13 51
        $this->schemaprefix = $schemaprefix;
14 51
    }
15
16 51
    public function getSubscribedEvents()
17
    {
18 51
        return array('loadClassMetadata');
19
    }
20
21 37
    public function loadClassMetadata(LoadClassMetadataEventArgs $args)
22
    {
23 37
        $classMetadata = $args->getClassMetadata();
24 37
        if (!$this->schemaprefix || ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity())) {
25 37
            return;
26
        }
27 37
        $classMetadata->setPrimaryTable(array('name' => $this->schemaprefix.'.'.$classMetadata->getTableName()));
28 37
        foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
29 35
            if (\Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY == $mapping['type'] &&
30 35
                    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 37
        if ($classMetadata->isIdGeneratorSequence()) {
38 37
            $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 37
            $newDefinition['sequenceName'] = $this->schemaprefix.'.'.$newDefinition['sequenceName'];
40
41 37
            $classMetadata->setSequenceGeneratorDefinition($newDefinition);
42 37
            $em = $args->getEntityManager();
43 37
            if (isset($classMetadata->idGenerator)) {
44 37
                $sequenceGenerator = new \Doctrine\ORM\Id\SequenceGenerator(
45 37
                    $em->getConfiguration()->getQuoteStrategy()->getSequenceName(
46 37
                        $newDefinition,
47 37
                        $classMetadata,
48 37
                        $em->getConnection()->getDatabasePlatform()
49
                    ),
50 37
                    $newDefinition['allocationSize']
51
                );
52 37
                $classMetadata->setIdGenerator($sequenceGenerator);
53
            }
54
        }
55 37
    }
56
}
57