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

UnderscoredClassNamespacePrefix::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 11
cts 11
cp 1
rs 9.2
c 0
b 0
f 0
cc 3
eloc 15
nc 2
nop 1
crap 3
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\UnderscoreNamingStrategy;
13
14
class UnderscoredClassNamespacePrefix extends UnderscoreNamingStrategy
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $map;
20
21
    /**
22
     * @var array
23
     */
24
    protected $whitelist;
25
26
    /**
27
     * @var array
28
     */
29
    protected $blacklist;
30
31
    /**
32
     * @var bool
33
     */
34
    protected $joinTableFieldSuffix;
35
36 8
    public function __construct(array $configuration = array())
37
    {
38 8
        $configuration = array_merge(array(
39 8
            'case' => CASE_LOWER,
40
            'map' => array(),
41
            'whitelist' => array(),
42
            'blacklist' => array(),
43
            'joinTableFieldSuffix' => true
44
        ), $configuration);
45
46 8
        if (count($configuration['whitelist']) > 0 && count($configuration['blacklist']) > 0) {
47 1
            throw new \RuntimeException('You can use whitelist or blacklist or none of mentioned lists, but not booth.');
48
        }
49
50 7
        $this->map = $configuration['map'];
51 7
        $this->blacklist = $configuration['blacklist'];
52 7
        $this->whitelist = $configuration['whitelist'];
53 7
        $this->joinTableFieldSuffix = $configuration['joinTableFieldSuffix'];
54
55 7
        parent::__construct($configuration['case']);
56
57 7
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 7
    public function classToTableName($className)
63
    {
64 7
        return (($prefix = $this->getTableNamePrefix($className)) ? $prefix . '_' : '') . parent::classToTableName($className);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 1
    public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
71
    {
72
        return
73 1
            parent::joinTableName($sourceEntity, $targetEntity, $propertyName)
74
            .
75 1
            (($this->joinTableFieldSuffix && !empty($propertyName)) ? '_' . $this->propertyToColumnName($propertyName, $sourceEntity) : '');
76
    }
77
78
    /**
79
     * Find prefix for table from map.
80
     *
81
     * @param string $className
82
     * @return string
83
     */
84 7
    private function getTableNamePrefix($className)
85
    {
86 7
        foreach ($this->blacklist as $blacklistedFqcn) {
87
88 1
            if (strpos($className, $blacklistedFqcn) === 0) {
89 1
                return '';
90
            }
91
        }
92
93 6
        foreach ($this->map as $fqcnPrefix => $tablePrefix) {
94
95 6
            if (strpos($className, $fqcnPrefix) === 0) {
96
97 5
                if (count($this->whitelist) > 0) {
98
99
                    foreach ($this->whitelist as $whitelistedFqcn) {
100
101
                        if (strpos($className, $whitelistedFqcn) === 0) {
102
                            return $this->propertyToColumnName($tablePrefix);
103
                        }
104
                    }
105
106
                    return '';
107
108
                } else {
109
110 6
                    return $this->propertyToColumnName($tablePrefix);
111
                }
112
            }
113
        }
114
115 2
        return '';
116
    }
117
}
118