Completed
Push — master ( 690b6b...6753b6 )
by Nikola
05:20
created

UnderscoredNamerCollection::referenceColumnName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/*
3
 * This file is part of the Doctrine Naming Strategy Bundle, an RunOpenCode project.
4
 *
5
 * (c) 2017 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Bundle\DoctrineNamingStrategy\NamingStrategy;
11
12
use Doctrine\ORM\Mapping\NamingStrategy;
13
use RunOpenCode\Bundle\DoctrineNamingStrategy\Exception\InvalidArgumentException;
14
15
/**
16
 * Class UnderscoredNamerCollection
17
 *
18
 * @package RunOpenCode\Bundle\DoctrineNamingStrategy\NamingStrategy
19
 */
20
class UnderscoredNamerCollection implements NamingStrategy
21
{
22
    /**
23
     * @var NamingStrategy
24
     */
25
    protected $defaultNamingStrategy;
26
27
    /**
28
     * @var NamingStrategy[]
29
     */
30
    protected $concurrentNamingStrategies;
31
32
    /**
33
     * @var bool
34
     */
35
    protected $joinTableFieldSuffix;
36
37
    /**
38
     * UnderscoredNamerCollection constructor.
39
     *
40
     * @param NamingStrategy $defaultNamingStrategy
41
     * @param NamingStrategy[] $concurrentNamingStrategies
42
     * @param array $configuration
43
     */
44 8
    public function __construct(NamingStrategy $defaultNamingStrategy, array $concurrentNamingStrategies = [], array $configuration = [])
45
    {
46 8
        $this->defaultNamingStrategy = $defaultNamingStrategy;
47
48 8
        $this->concurrentNamingStrategies = [];
49
50 8
        foreach ($concurrentNamingStrategies as $namingStrategy) {
51 8
            $this->registerNamingStrategy($namingStrategy);
52
        }
53
54 8
        $configuration = array_merge([
55 8
            'join_table_field_suffix' => true,
56
        ], $configuration);
57
58 8
        $this->joinTableFieldSuffix = $configuration['join_table_field_suffix'];
59 8
    }
60
61
    /**
62
     * Register naming strategy.
63
     *
64
     * @param NamingStrategy $namingStrategy
65
     *
66
     * @return UnderscoredNamerCollection $this
67
     *
68
     * @throws \RunOpenCode\Bundle\DoctrineNamingStrategy\Exception\InvalidArgumentException
69
     */
70 8
    public function registerNamingStrategy(NamingStrategy $namingStrategy)
71
    {
72 8
        if ($namingStrategy === $this->defaultNamingStrategy) {
73 1
            throw new InvalidArgumentException('Concurent naming strategy can not be default naming strategy.');
74
        }
75
76 8
        $this->concurrentNamingStrategies[] = $namingStrategy;
77 8
        return $this;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 2
    public function classToTableName($className)
84
    {
85 2
        return $this->findNamer($className)->classToTableName($className);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 2
    public function propertyToColumnName($propertyName, $className = null)
92
    {
93 2
        return $this->findNamer($className)->propertyToColumnName($propertyName, $className);
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 1
    public function embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className = null, $embeddedClassName = null)
100
    {
101 1
        $namer = $this->findNamer($embeddedClassName);
102
103 1
        if ($namer === $this->defaultNamingStrategy && null !== $className) {
104 1
            $namer = $this->findNamer($className);
105
        }
106
107 1
        return $namer->embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className, $embeddedClassName);
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 1
    public function referenceColumnName()
114
    {
115 1
        return $this->defaultNamingStrategy->referenceColumnName();
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 1
    public function joinColumnName($propertyName, $className = null)
122
    {
123 1
        return $this->findNamer($className)->joinColumnName($propertyName, $className);
0 ignored issues
show
Unused Code introduced by
The call to NamingStrategy::joinColumnName() has too many arguments starting with $className.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 1
    public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
130
    {
131
        return
132 1
            $this->classToTableName($sourceEntity).'_'.$this->classToTableName($targetEntity)
133
            .
134 1
            (($this->joinTableFieldSuffix && !empty($propertyName)) ? '_'.$this->propertyToColumnName($propertyName, $sourceEntity) : '')
135
            ;
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141 1
    public function joinKeyColumnName($entityName, $referencedColumnName = null)
142
    {
143 1
        $namer = $this->findNamer($entityName);
144
145 1
        return $namer->classToTableName($entityName).'_'.($namer->propertyToColumnName($referencedColumnName) ?: $namer->referenceColumnName());
146
    }
147
148
149
    /**
150
     * Find applicable naming strategy for given class.
151
     *
152
     * @param string $className
153
     *
154
     * @return NamingStrategy
155
     */
156 6
    private function findNamer($className)
157
    {
158 6
        if ($className === null) {
159 3
            return $this->defaultNamingStrategy;
160
        }
161
162 6
        $defaultName = strtolower($this->defaultNamingStrategy->classToTableName($className));
163
164 6
        foreach ($this->concurrentNamingStrategies as $concurrentNamer) {
165
166 6
            if (strtolower($concurrentNamer->classToTableName($className)) !== $defaultName) {
167 6
                return $concurrentNamer;
168
            }
169
        }
170
171 2
        return $this->defaultNamingStrategy;
172
    }
173
}
174