Completed
Push — master ( e54e2c...44461e )
by Nikola
05:06
created

UnderscoredBundleNamePrefix::getNamingMap()   C

Complexity

Conditions 8
Paths 6

Size

Total Lines 37
Code Lines 18

Duplication

Lines 8
Ratio 21.62 %

Code Coverage

Tests 18
CRAP Score 8

Importance

Changes 0
Metric Value
dl 8
loc 37
ccs 18
cts 18
cp 1
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 18
nc 6
nop 2
crap 8
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\RuntimeException;
14
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
15
use Symfony\Component\HttpKernel\KernelInterface;
16
17
class UnderscoredBundleNamePrefix implements NamingStrategy
18
{
19
    /**
20
     * @var int
21
     */
22
    protected $case = CASE_LOWER;
23
24
    /**
25
     * @var bool
26
     */
27
    protected $joinTableFieldSuffix;
28
29
    /**
30
     * @var array
31
     */
32
    protected $map;
33
34 17
    public function __construct(KernelInterface $kernel, array $options = array())
35
    {
36 17
        $options = array_merge(array(
37 17
            'case' => CASE_LOWER,
38
            'map' => array(),
39
            'whitelist' => array(),
40
            'blacklist' => array(),
41
            'joinTableFieldSuffix' => true
42
        ), $options);
43
44 17
        if (count($options['whitelist']) > 0 && count($options['blacklist']) > 0) {
45 1
            throw new RuntimeException('You can use whitelist or blacklist or none of mentioned lists, but not booth.');
46
        }
47
48 16
        $this->case = $options['case'];
49 16
        $this->joinTableFieldSuffix = $options['joinTableFieldSuffix'];
50
51 16
        $this->map = $this->getNamingMap($kernel, $options);
52 16
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 9
    public function classToTableName($className)
58
    {
59 9
        $prefix = $this->getTableNamePrefix($className);
60
61 9
        if (strpos($className, '\\') !== false) {
62 9
            $className = substr($className, strrpos($className, '\\') + 1);
63
        }
64
65 9
        return $prefix.$this->underscore($className);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 4
    public function propertyToColumnName($propertyName, $className = null)
72
    {
73 4
        return $this->underscore($propertyName);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 2
    public function embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className = null, $embeddedClassName = null)
80
    {
81 2
        return $this->underscore($propertyName).'_'.$this->underscore($embeddedColumnName);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 5
    public function referenceColumnName()
88
    {
89 5
        return $this->case === CASE_UPPER ?  'ID' : 'id';
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 2
    public function joinColumnName($propertyName, $className = null)
96
    {
97 2
        return $this->underscore($propertyName) . '_' . $this->referenceColumnName();
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 3
    public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
104
    {
105 3
        $tableName = $this->classToTableName($sourceEntity).'_'.$this->classToTableName($targetEntity);
106
107
        return
108
            $tableName
109
            .
110 3
            (($this->joinTableFieldSuffix && null !== $propertyName) ? '_'.$this->propertyToColumnName($propertyName, $sourceEntity) : '');
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 2
    public function joinKeyColumnName($entityName, $referencedColumnName = null)
117
    {
118 2
        return $this->classToTableName($entityName).'_'.
119 2
            ($referencedColumnName ? $this->underscore($referencedColumnName) : $this->referenceColumnName());
120
    }
121
122
    /**
123
     * Get bundle naming map.
124
     *
125
     * @param KernelInterface $kernel
126
     * @param array $configuration
127
     *
128
     * @return array
129
     */
130 16
    private function getNamingMap(KernelInterface $kernel, array $configuration)
131
    {
132 16
        $map = [];
133
134
        /**
135
         * @var BundleInterface $bundle;
136
         */
137 16
        foreach ($kernel->getBundles() as $bundle) {
138
139 16
            if (count($configuration['blacklist']) > 0 && in_array($bundle->getName(), $configuration['blacklist'])) {
140 1
                continue;
141
            }
142
143 16
            if (count($configuration['whitelist']) > 0 && !in_array($bundle->getName(), $configuration['whitelist'])) {
144 1
                continue;
145
            }
146
147 16
            $bundleNamespace = (new \ReflectionClass(get_class($bundle)))->getNamespaceName();
148 16
            $bundleName = $bundle->getName();
149
150 16 View Code Duplication
            if (isset($configuration['map'][$bundleName])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151 10
                $map[ $this->underscore($configuration['map'][$bundleName]) ] = $bundleNamespace;
152 10
                continue;
153
            }
154
155 15
            $bundleName = preg_replace('/Bundle$/', '', $bundleName);
156
157 15 View Code Duplication
            if (isset($configuration['map'][$bundleName])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158 10
                $map[ $this->underscore($configuration['map'][$bundleName]) ] = $bundleNamespace;
159 10
                continue;
160
            }
161
162 15
            $map[ $this->underscore($bundleName) ] = $bundleNamespace;
163
        }
164
165 16
        return $map;
166
    }
167
168
    /**
169
     * Get prefix for table from map.
170
     *
171
     * @param string $className
172
     * @return string
173
     */
174 9
    protected function getTableNamePrefix($className)
175
    {
176 9
        $className = ltrim($className, '\\');
177
178 9
        foreach ($this->map as $prefix => $namespace) {
179
180 9
            if (strpos($className, $namespace) === 0) {
181 9
                return $prefix.'_';
182
            }
183
        }
184
185 2
        return '';
186
    }
187
188
189
    /**
190
     * Build underscore version of given string.
191
     *
192
     * @param string $string
193
     * @return string
194
     */
195 16
    protected function underscore($string)
196
    {
197 16
        $string = preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $string);
198
199 16
        if (CASE_UPPER === $this->case) {
200 7
            return strtoupper($string);
201
        }
202
203 10
        return strtolower($string);
204
    }
205
}
206