TableSchemaSubscriber   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 27.59%

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 48
ccs 8
cts 29
cp 0.2759
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 33 9
1
<?php
2
3
namespace Cdf\BiCoreBundle\Subscriber;
4
5
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
6
use Doctrine\ORM\Id\SequenceGenerator;
7
8
class TableSchemaSubscriber implements \Doctrine\Common\EventSubscriber
9
{
10
11
    private string $schemaprefix;
12
13 42
    public function __construct(string $schemaprefix)
14
    {
15 42
        $this->schemaprefix = $schemaprefix;
16
    }
17
18 42
    public function getSubscribedEvents(): array
19
    {
20 42
        return array('loadClassMetadata');
21
    }
22
23 40
    public function loadClassMetadata(LoadClassMetadataEventArgs $args): void
24
    {
25 40
        $classMetadata = $args->getClassMetadata();
26 40
        if (!$this->schemaprefix || ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity())) {
27 40
            return;
28
        }
29
        $classMetadata->setPrimaryTable(array('name' => $this->schemaprefix . '.' . $classMetadata->getTableName()));
30
        foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
0 ignored issues
show
Bug introduced by
The method getAssociationMappings() does not exist on Doctrine\Persistence\Mapping\ClassMetadata. Did you maybe mean getAssociationNames()? ( Ignorable by Annotation )

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

30
        foreach ($classMetadata->/** @scrutinizer ignore-call */ getAssociationMappings() as $fieldName => $mapping) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
31
            if (\Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY == $mapping['type'] &&
32
                    isset($classMetadata->associationMappings[$fieldName]['joinTable']['name'])
0 ignored issues
show
Bug introduced by
Accessing associationMappings on the interface Doctrine\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
33
            ) {
34
                $mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name'];
35
                $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->schemaprefix . '.' . $mappedTableName;
36
            }
37
        }
38
39
        if ($classMetadata->isIdGeneratorSequence()) {
40
            $newDefinition = $classMetadata->sequenceGeneratorDefinition;
0 ignored issues
show
Bug introduced by
Accessing sequenceGeneratorDefinition on the interface Doctrine\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
41
            $newDefinition['sequenceName'] = $this->schemaprefix . '.' . $newDefinition['sequenceName'];
42
43
            $classMetadata->setSequenceGeneratorDefinition($newDefinition);
44
            $em = $args->getEntityManager();
45
            /** @phpstan-ignore-next-line */
46
            if (isset($classMetadata->idGenerator)) {
0 ignored issues
show
Bug introduced by
Accessing idGenerator on the interface Doctrine\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
47
                $sequenceGenerator = new SequenceGenerator(
48
                    $em->getConfiguration()->getQuoteStrategy()->getSequenceName(
49
                        $newDefinition,
50
                        $classMetadata,
51
                        $em->getConnection()->getDatabasePlatform()
52
                    ),
53
                    (int)$newDefinition['allocationSize']
54
                );
55
                $classMetadata->setIdGenerator($sequenceGenerator);
56
            }
57
        }
58
    }
59
}
60