Passed
Push — master ( 5e8bf6...1d619d )
by Andrea
25:03
created

TablePrefixSubscriber   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 30
ccs 16
cts 17
cp 0.9412
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
B loadClassMetadata() 0 16 7
A __construct() 0 3 1
A getSubscribedEvents() 0 3 1
1
<?php
2
namespace Cdf\BiCoreBundle\Subscriber;
3
4
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
5
6
class TablePrefixSubscriber implements \Doctrine\Common\EventSubscriber
7
{
8
    private $tableprefix;
9
10 50
    public function __construct($tableprefix)
11
    {
12 50
        $this->tableprefix = $tableprefix;
13 50
    }
14
    
15 50
    public function getSubscribedEvents()
16
    {
17 50
        return array('loadClassMetadata');
18
    }
19
20 37
    public function loadClassMetadata(LoadClassMetadataEventArgs $args)
21
    {
22 37
        $classMetadata = $args->getClassMetadata();
23 37
        if ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity()) {
24
            // if we are in an inheritance hierarchy, only apply this once
25 37
            return;
26
        }
27 37
        if (false !== strpos($classMetadata->namespace, 'Cdf\BiCoreBundle')) {
28 36
            $tableprefix = $this->tableprefix;
29 36
            $classMetadata->setPrimaryTable(array('name' =>  $tableprefix . $classMetadata->getTableName()));
30 36
            foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
31 34
                if ($mapping['type'] == \Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY &&
32 34
                    isset($classMetadata->associationMappings[$fieldName]['joinTable']['name'])
33
                ) {
34
                    $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...
35 34
                    $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $tableprefix . $mappedTableName;
36
                }
37
            }
38
        }
39 37
    }
40
}
41