Completed
Push — master ( 0d0012...5d4e7a )
by Dmitriy
02:24
created

Config::getCriteriaMap()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
namespace T4webInfrastructure;
3
4
class Config
5
{
6
    /**
7
     * @var array
8
     */
9
    protected $entityMap = [];
10
11
    public function __construct(array $entityMap)
12
    {
13
        $this->entityMap = $entityMap;
14
    }
15
16
    /**
17
     * @param string $entityName
18
     *
19
     * @return string
20
     */
21
    public function getTable($entityName)
22
    {
23
        if (!isset($this->entityMap[$entityName]['table'])) {
24
            throw new ConfigException(sprintf("entity_map not configured for %s", $entityName));
25
        }
26
27
        return $this->entityMap[$entityName]['table'];
28
    }
29
30
    /**
31
     * @param string $entityName
32
     * @param string $attribute
33
     * @return string
34
     */
35
    public function getFiled($entityName, $attribute)
36
    {
37 View Code Duplication
        if (!isset($this->entityMap[$entityName]['columnsAsAttributesMap'])) {
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...
38
            throw new ConfigException(sprintf("entity_map[columnsAsAttributesMap] not configured for %s", $entityName));
39
        }
40
41
        $field = array_search($attribute, $this->entityMap[$entityName]['columnsAsAttributesMap']);
42
43
        if (!$field) {
44
            throw new ConfigException(sprintf("attributes %s not exists in entity_map[columnsAsAttributesMap] config", $attribute));
45
        }
46
47
        return $field;
48
    }
49
50
    /**
51
     * @param string $entityName
52
     * @param string $joinEntityName
53
     *
54
     * @return string
55
     */
56
    public function getRelationExpression($entityName, $joinEntityName)
57
    {
58 View Code Duplication
        if (!isset($this->entityMap[$entityName]['relations'][$joinEntityName])) {
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...
59
            throw new ConfigException(
60
                sprintf(
61
                    "entity_map for %s not configured with relation %s",
62
                    $entityName,
63
                    $joinEntityName
64
                )
65
            );
66
        }
67
68
        if (!isset($this->entityMap[$entityName]['relations'][$joinEntityName][0])
69
            || !isset($this->entityMap[$entityName]['relations'][$joinEntityName][1])) {
70
            throw new ConfigException(
71
                sprintf(
72
                    "entity_map for %s with relation %s must be array [field, joined-filed]",
73
                    $entityName,
74
                    $joinEntityName
75
                )
76
            );
77
        }
78
79
        return $this->entityMap[$entityName]['relations'][$joinEntityName][0].' = '
80
            . $this->entityMap[$entityName]['relations'][$joinEntityName][1];
81
    }
82
83
    /**
84
     * @param string $entityName
85
     * @param string $joinEntityName
86
     * @return bool
87
     */
88
    public function isRelationManyToMany($entityName, $joinEntityName)
89
    {
90 View Code Duplication
        if (!isset($this->entityMap[$entityName]['relations'][$joinEntityName])) {
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...
91
            throw new ConfigException(
92
                sprintf(
93
                    "entity_map for %s not configured with relation %s",
94
                    $entityName,
95
                    $joinEntityName
96
                )
97
            );
98
        }
99
100
        if (!isset($this->entityMap[$entityName]['relations'][$joinEntityName][0])
101
            || !isset($this->entityMap[$entityName]['relations'][$joinEntityName][1])
102
            || !isset($this->entityMap[$entityName]['relations'][$joinEntityName][2])) {
103
            return false;
104
        }
105
106
        return true;
107
    }
108
109
    /**
110
     * @param string $entityName
111
     * @param string $joinEntityName
112
     * @return array
113
     */
114
    public function getRelationManyToMany($entityName, $joinEntityName)
115
    {
116
        if (!$this->isRelationManyToMany($entityName, $joinEntityName)) {
117
            throw new ConfigException(
118
                sprintf(
119
                    "entity_map for %s with relation %s must be array [link-table, field, joined-filed]",
120
                    $entityName,
121
                    $joinEntityName
122
                )
123
            );
124
        }
125
126
        return [
127
            $this->entityMap[$entityName]['relations'][$joinEntityName][0],
128
            $this->entityMap[$entityName]['relations'][$joinEntityName][1],
129
            $this->entityMap[$entityName]['relations'][$joinEntityName][2],
130
        ];
131
    }
132
133
    /**
134
     * @param string $entityName
135
     *
136
     * @return array
137
     */
138
    public function getColumnsAsAttributesMap($entityName)
139
    {
140 View Code Duplication
        if (!isset($this->entityMap[$entityName]['columnsAsAttributesMap'])) {
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...
141
            throw new ConfigException(
142
                sprintf("entity_map[columnsAsAttributesMap] not configured for %s", $entityName)
143
            );
144
        }
145
146 View Code Duplication
        if (!is_array($this->entityMap[$entityName]['columnsAsAttributesMap'])) {
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...
147
            throw new ConfigException(
148
                sprintf("entity_map[columnsAsAttributesMap] for %s must be array", $entityName)
149
            );
150
        }
151
152
        return $this->entityMap[$entityName]['columnsAsAttributesMap'];
153
    }
154
155
    /**
156
     * @param string $entityName
157
     * @return string|null
158
     */
159
    public function getPrimaryKey($entityName)
160
    {
161
        if (isset($this->entityMap[$entityName]['primaryKey'])) {
162
            return $this->entityMap[$entityName]['primaryKey'];
163
        }
164
    }
165
166
    /**
167
     * @param string $entityName
168
     * @return string|null
169
     */
170
    public function getSequence($entityName)
171
    {
172
        if (isset($this->entityMap[$entityName]['sequence'])) {
173
            return $this->entityMap[$entityName]['sequence'];
174
        }
175
    }
176
177
    /**
178
     * @param string $entityName
179
     * @return string|null
180
     */
181
    public function getCriteriaMap($entityName)
182
    {
183
        if (isset($this->entityMap[$entityName]['criteriaMap'])) {
184
            return $this->entityMap[$entityName]['criteriaMap'];
185
        }
186
    }
187
188
    /**
189
     * @param string $entityName
190
     * @return string
191
     */
192
    public function getNamespace($entityName)
193
    {
194
        if (isset($this->entityMap[$entityName]['namespace'])) {
195
            return $this->entityMap[$entityName]['namespace'];
196
        }
197
198
        return "{$entityName}s\\$entityName";
199
    }
200
}
201