Completed
Push — master ( 21f22a...492613 )
by Nikola
11:38
created

UnderscoredNamerCollection::findNamer()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RunOpenCode\Bundle\DoctrineNamingStrategy\NamingStrategy;
6
7
use Doctrine\ORM\Mapping\NamingStrategy;
8
use RunOpenCode\Bundle\DoctrineNamingStrategy\Exception\InvalidArgumentException;
9
10
/**
11
 * @psalm-suppress UnusedClass
12
 */
13
final class UnderscoredNamerCollection implements NamingStrategy
14
{
15
    protected NamingStrategy $defaultNamingStrategy;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
16
17
    /**
18
     * @var NamingStrategy[]
19
     */
20
    protected array $concurrentNamingStrategies;
21
22
    protected bool $joinTableFieldSuffix;
23
24
    /**
25
     * @param NamingStrategy   $defaultNamingStrategy
26
     * @param NamingStrategy[] $concurrentNamingStrategies
27
     * @param array<array-key,mixed> $configuration
28
     */
29
    public function __construct(NamingStrategy $defaultNamingStrategy, array $concurrentNamingStrategies = [], array $configuration = [])
30
    {
31
        $this->defaultNamingStrategy      = $defaultNamingStrategy;
32
        $this->concurrentNamingStrategies = [];
33
34
        foreach ($concurrentNamingStrategies as $namingStrategy) {
35
            $this->registerNamingStrategy($namingStrategy);
36
        }
37
38
        $configuration = \array_merge([
39
            'join_table_field_suffix' => true,
40
        ], $configuration);
41
42
        $this->joinTableFieldSuffix = (bool)$configuration['join_table_field_suffix'];
43
    }
44 8
45
    /**
46 8
     * @throws InvalidArgumentException
47
     */
48 8
    public function registerNamingStrategy(NamingStrategy $namingStrategy): void
49
    {
50 8
        if ($namingStrategy === $this->defaultNamingStrategy) {
51 8
            throw new InvalidArgumentException('Concurent naming strategy can not be default naming strategy.');
52
        }
53
54 8
        $this->concurrentNamingStrategies[] = $namingStrategy;
55 8
    }
56 8
57
    /**
58 8
     * {@inheritdoc}
59 8
     */
60
    public function classToTableName($className): string
61
    {
62
        return $this->findNamer($className)->classToTableName($className);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function propertyToColumnName($propertyName, $className = null): string
69
    {
70 8
        return $this->findNamer($className)->propertyToColumnName($propertyName, $className);
71
    }
72 8
73 1
    /**
74
     * {@inheritdoc}
75
     */
76 8
    public function embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className = null, $embeddedClassName = null): string
77 8
    {
78
        $namer = $this->findNamer($embeddedClassName);
79
80
        if ($namer === $this->defaultNamingStrategy && null !== $className) {
81
            $namer = $this->findNamer($className);
82
        }
83 2
84
        return $namer->embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className, $embeddedClassName);
85 2
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function referenceColumnName(): string
91 2
    {
92
        return $this->defaultNamingStrategy->referenceColumnName();
93 2
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function joinColumnName($propertyName): string
99 1
    {
100
        return $this->findNamer()->joinColumnName($propertyName);
101 1
    }
102
103 1
    /**
104 1
     * {@inheritdoc}
105
     */
106
    public function joinTableName($sourceEntity, $targetEntity, $propertyName = null): string
107 1
    {
108
        return
109
            $this->classToTableName($sourceEntity) . '_' . $this->classToTableName($targetEntity)
110
            .
111
            (($this->joinTableFieldSuffix && !empty($propertyName)) ? '_' . $this->propertyToColumnName($propertyName, $sourceEntity) : '');
112
    }
113 1
114
    /**
115 1
     * {@inheritdoc}
116
     */
117
    public function joinKeyColumnName($entityName, $referencedColumnName = null): string
118
    {
119
        $namer              = $this->findNamer($entityName);
120
        $classTableName     = $namer->classToTableName($entityName);
121 1
        $propertyColumnName = $namer->referenceColumnName();
122
123 1
        if (null !== $referencedColumnName) {
124
            $propertyColumnName = $namer->propertyToColumnName($referencedColumnName) ?: $namer->referenceColumnName();
125
        }
126
127
        return \sprintf('%s_%s', $classTableName, $propertyColumnName);
128
    }
129 1
130
    private function findNamer(?string $className = null): NamingStrategy
131
    {
132 1
        if ($className === null) {
133
            return $this->defaultNamingStrategy;
134 1
        }
135
136
        $defaultName = \strtolower($this->defaultNamingStrategy->classToTableName($className));
137
138
        foreach ($this->concurrentNamingStrategies as $concurrentNamer) {
139
            if (\strtolower($concurrentNamer->classToTableName($className)) !== $defaultName) {
140
                return $concurrentNamer;
141 1
            }
142
        }
143 1
144
        return $this->defaultNamingStrategy;
145 1
    }
146
}
147