Completed
Push — master ( 49e156...690b6b )
by Nikola
03:38
created

UnderscoredNamerCollection   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 87.8%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 2
dl 0
loc 147
ccs 36
cts 41
cp 0.878
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 2
A registerNamingStrategy() 0 9 2
A classToTableName() 0 4 1
A propertyToColumnName() 0 4 1
A embeddedFieldToColumnName() 0 10 3
A referenceColumnName() 0 4 1
A joinColumnName() 0 4 1
A joinTableName() 0 8 3
A joinKeyColumnName() 0 6 2
A findNamer() 0 17 4
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 6
    public function __construct(NamingStrategy $defaultNamingStrategy, array $concurrentNamingStrategies = [], array $configuration = [])
38
    {
39 6
        $this->defaultNamingStrategy = $defaultNamingStrategy;
40
41 6
        $this->concurrentNamingStrategies = [];
42
43 6
        foreach ($concurrentNamingStrategies as $namingStrategy) {
44
            $this->registerNamingStrategy($namingStrategy);
45
        }
46
47 6
        $configuration = array_merge([
48 6
            'joinTableFieldSuffix' => true,
49
        ], $configuration);
50
51 6
        $this->joinTableFieldSuffix = $configuration['joinTableFieldSuffix'];
52 6
    }
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 6
    public function registerNamingStrategy(NamingStrategy $namingStrategy)
64
    {
65 6
        if ($namingStrategy === $this->defaultNamingStrategy) {
66
            throw new InvalidArgumentException('Concurent naming strategy can not be default naming strategy.');
67
        }
68
69 6
        $this->concurrentNamingStrategies[] = $namingStrategy;
70 6
        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 1
    public function joinKeyColumnName($entityName, $referencedColumnName = null)
135
    {
136 1
        $namer = $this->findNamer($entityName);
137
138 1
        return $namer->classToTableName($entityName) . '_' . ($namer->propertyToColumnName($referencedColumnName) ?: $namer->referenceColumnName());
139
    }
140
141
142
    /**
143
     * Find applicable naming strategy for given class.
144
     *
145
     * @param string $className
146
     *
147
     * @return NamingStrategy
148
     */
149 5
    private function findNamer($className)
150
    {
151 5
        if ($className === null) {
152 3
            return $this->defaultNamingStrategy;
153
        }
154
155 5
        $defaultName = strtolower($this->defaultNamingStrategy->classToTableName($className));
156
157 5
        foreach ($this->concurrentNamingStrategies as $concurrentNamer) {
158
159 5
            if (strtolower($concurrentNamer->classToTableName($className)) !== $defaultName) {
160 5
                return $concurrentNamer;
161
            }
162
        }
163
164 2
        return $this->defaultNamingStrategy;
165
    }
166
}
167