Completed
Push — master ( 067e0f...49e156 )
by Nikola
03:59
created

UnderscoredNamerCollection::joinTableName()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 4
nop 3
crap 12
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 5
    public function __construct(NamingStrategy $defaultNamingStrategy, array $concurrentNamingStrategies = [], array $configuration = [])
38
    {
39 5
        $this->defaultNamingStrategy = $defaultNamingStrategy;
40
41 5
        $this->concurrentNamingStrategies = [];
42
43 5
        foreach ($concurrentNamingStrategies as $namingStrategy) {
44
            $this->registerNamingStrategy($namingStrategy);
45
        }
46
47 5
        $configuration = array_merge([
48 5
            'joinTableFieldSuffix' => true,
49
        ], $configuration);
50
51 5
        $this->joinTableFieldSuffix = $configuration['joinTableFieldSuffix'];
52 5
    }
53
54
    /**
55
     * Register naming strategy.
56
     *
57
     * @param NamingStrategy $namingStrategy
58
     *
59
     * @return UnderscoredNamerCollection $this
60
     *
61
     * @throws \RunOpenCode\Bundle\DoctrineNamingStrategy\Exception\InvalidArgumentException
62
     */
63 5
    public function registerNamingStrategy(NamingStrategy $namingStrategy)
64
    {
65 5
        if ($namingStrategy === $this->defaultNamingStrategy) {
66
            throw new InvalidArgumentException('Concurent naming strategy can not be default naming strategy.');
67
        }
68
69 5
        $this->concurrentNamingStrategies[] = $namingStrategy;
70 5
        return $this;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 1
    public function classToTableName($className)
77
    {
78 1
        return $this->findNamer($className)->classToTableName($className);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 1
    public function propertyToColumnName($propertyName, $className = null)
85
    {
86 1
        return $this->findNamer($className)->propertyToColumnName($propertyName, $className);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 1
    public function embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className = null, $embeddedClassName = null)
93
    {
94 1
        $namer = $this->findNamer($embeddedClassName);
95
96 1
        if ($namer === $this->defaultNamingStrategy && null !== $className) {
97 1
            $namer = $this->findNamer($className);
98
        }
99
100 1
        return $namer->embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className, $embeddedClassName);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 1
    public function referenceColumnName()
107
    {
108 1
        return $this->defaultNamingStrategy->referenceColumnName();
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 1
    public function joinColumnName($propertyName, $className = null)
115
    {
116 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...
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
123
    {
124
        return
125
            $this->classToTableName($sourceEntity) . '_' . $this->classToTableName($targetEntity)
126
            .
127
            (($this->joinTableFieldSuffix && !empty($propertyName)) ? '_' . $this->propertyToColumnName($propertyName, $sourceEntity) : '')
128
            ;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function joinKeyColumnName($entityName, $referencedColumnName = null)
135
    {
136
        return $this->classToTableName($entityName) . '_' . ($referencedColumnName ?: $this->referenceColumnName());
137
    }
138
139
140
    /**
141
     * Find applicable naming strategy for given class.
142
     *
143
     * @param string $className
144
     *
145
     * @return NamingStrategy
146
     */
147 4
    private function findNamer($className)
148
    {
149 4
        if ($className === null) {
150 3
            return $this->defaultNamingStrategy;
151
        }
152
153 4
        $defaultName = strtolower($this->defaultNamingStrategy->classToTableName($className));
154
155 4
        foreach ($this->concurrentNamingStrategies as $concurrentNamer) {
156
157 4
            if (strtolower($concurrentNamer->classToTableName($className)) !== $defaultName) {
158 4
                return $concurrentNamer;
159
            }
160
        }
161
162 2
        return $this->defaultNamingStrategy;
163
    }
164
}
165