Completed
Push — master ( c8cd34...e54e2c )
by Nikola
11:06
created

UnderscoredBundleNamePrefix::joinTableName()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 7
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
cc 3
eloc 4
nc 4
nop 3
crap 3
1
<?php
2
/*
3
 * This file is part of the Doctrine Naming Strategy Bundle, an RunOpenCode project.
4
 *
5
 * (c) 2016 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\UnderscoreNamingStrategy;
13
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
14
use Symfony\Component\HttpKernel\KernelInterface;
15
16
class UnderscoredBundleNamePrefix extends UnderscoreNamingStrategy
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $map;
22
23
    /**
24
     * @var bool
25
     */
26
    protected $joinTableFieldSuffix;
27
28 12
    public function __construct(KernelInterface $kernel, array $configuration = array())
29
    {
30 12
        $configuration = array_merge(array(
31 12
            'case' => CASE_LOWER,
32 12
            'map' => array(),
33 12
            'whitelist' => array(),
34 12
            'blacklist' => array(),
35
            'joinTableFieldSuffix' => true
36 12
        ), $configuration);
37
38 12 View Code Duplication
        if (count($configuration['whitelist']) > 0 && count($configuration['blacklist']) > 0) {
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...
39 2
            throw new \RuntimeException('You can use whitelist or blacklist or none of mentioned lists, but not booth.');
40
        }
41
42 10
        parent::__construct($configuration['case']);
43 10
        $this->buildMap($kernel, $configuration);
44
45 10
        $this->joinTableFieldSuffix = $configuration['joinTableFieldSuffix'];
46 10
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 10
    public function classToTableName($className)
52
    {
53 10
        return (($prefix = $this->getTableNamePrefix($className)) ? $prefix . '_' : '') . parent::classToTableName($className);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 2 View Code Duplication
    public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
60
    {
61
        return
62 2
            parent::joinTableName($sourceEntity, $targetEntity, $propertyName)
63
            .
64 2
            (($this->joinTableFieldSuffix && !empty($propertyName)) ? '_' . $this->propertyToColumnName($propertyName, $sourceEntity) : '');
65
    }
66
67
    /**
68
     * Build bundle naming map.
69
     *
70
     * @param KernelInterface $kernel
71
     * @param array $configuration
72
     */
73 10
    private function buildMap(KernelInterface $kernel, array $configuration)
74
    {
75 10
        $this->map = array();
76
        /**
77
         * @var BundleInterface $bundle;
78
         */
79 10
        foreach ($kernel->getBundles() as $bundle) {
80
81 10
            if (count($configuration['blacklist']) > 0 && in_array($bundle->getName(), $configuration['blacklist'])) {
82 2
                continue;
83
            }
84
85 8
            if (count($configuration['whitelist']) > 0 && !in_array($bundle->getName(), $configuration['whitelist'])) {
86 2
                continue;
87
            }
88
89 6
            $bundleClass = new \ReflectionClass(get_class($bundle));
90
91 6
            if (isset($configuration['map'][$bundle->getName()])) {
92 6
                $this->map[$this->propertyToColumnName($configuration['map'][$bundle->getName()])] = $bundleClass->getNamespaceName();
93 6
            } elseif (isset($configuration['map'][str_replace('Bundle', '', $bundle->getName())])) {
94
                $this->map[$this->propertyToColumnName($configuration['map'][str_replace('Bundle', '', $bundle->getName())])] = $bundleClass->getNamespaceName();
95
            } else {
96
                $this->map[str_replace('_bundle', '', $this->propertyToColumnName($bundle->getName()))] = $bundleClass->getNamespaceName();
97
            }
98 10
        }
99 10
    }
100
101
    /**
102
     * Find prefix for table from map.
103
     *
104
     * @param string $className
105
     * @return string
106
     */
107 10
    private function getTableNamePrefix($className)
108
    {
109 10
        foreach ($this->map as $prefix => $namespace) {
110
111 6
            if (strpos($className, $namespace) === 0) {
112 6
                return $prefix;
113
            }
114 5
        }
115
116 4
        return '';
117
    }
118
}
119