Completed
Pull Request — master (#56)
by
unknown
02:11
created

Mapping::tryGetClassMetadataByShortName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 3
eloc 4
nc 3
nop 1
1
<?php
2
3
namespace Mapado\RestClientSdk;
4
5
use Mapado\RestClientSdk\Exception\MappingException;
6
use Mapado\RestClientSdk\Mapping\ClassMetadata;
7
8
/**
9
 * Class Mapping
10
 * @author Julien Deniau <[email protected]>
11
 */
12
class Mapping
13
{
14
    const DEFAULT_CONFIG = [
15
        'collectionKey' => 'hydra:member',
16
    ];
17
18
    /**
19
     * @var string
20
     */
21
    private $idPrefix;
22
23
    /**
24
     * @var int
25
     */
26
    private $idPrefixLength;
27
28
    /**
29
     * @var ClassMetadata[]
30
     */
31
    private $classMetadatas = [];
32
33
    /**
34
     * config
35
     *
36
     * @var array
37
     * @access private
38
     */
39
    private $config;
40
41
    /**
42
     * Constructor.
43
     *
44
     * @param string $idPrefix
45
     * @access public
46
     */
47
    public function __construct($idPrefix = '', $config = [])
48
    {
49
        $this->idPrefix = $idPrefix;
50
        $this->idPrefixLength = strlen($idPrefix);
51
        $this->setConfig($config);
52
    }
53
54
    /**
55
     * getIdPrefix
56
     *
57
     * @access public
58
     * @return string
59
     */
60
    public function getIdPrefix()
61
    {
62
        return $this->idPrefix;
63
    }
64
65
    /**
66
     * Getter for config
67
     *
68
     * @return array
69
     */
70
    public function getConfig()
71
    {
72
        return $this->config;
73
    }
74
75
    /**
76
     * Setter for config
77
     *
78
     * @param array $config
79
     * @return Mapping
80
     */
81
    public function setConfig(array $config)
82
    {
83
        $this->config = array_merge(
84
            self::DEFAULT_CONFIG,
85
            $config
86
        );
87
88
        return $this;
89
    }
90
91
    /**
92
     * setMapping
93
     *
94
     * @param ClassMetadata[] $classMetadatas
95
     * @access public
96
     * @return Mapping
97
     */
98
    public function setMapping(array $classMetadatas)
99
    {
100
        $this->classMetadatas = $classMetadatas;
101
102
        return $this;
103
    }
104
105
    /**
106
     * return a model class name for a given key
107
     *
108
     * @param string $key
109
     * @access public
110
     * @return string
111
     */
112
    public function getModelName($key)
113
    {
114
        $this->checkMappingExistence($key, 'modelName');
115
116
        return $this->getClassMetadataByKey($key)->getModelName();
117
    }
118
119
    /**
120
     * return the list of mapping keys
121
     *
122
     * @access public
123
     * @return string[]
124
     */
125
    public function getMappingKeys()
126
    {
127
        return array_map(
128
            function (ClassMetadata $classMetadata) {
129
                return $classMetadata->getKey();
130
            },
131
            $this->classMetadatas
132
        );
133
    }
134
135
    /**
136
     * get the key from an id (path)
137
     *
138
     * @param string $id
139
     * @access public
140
     * @return string
141
     */
142
    public function getKeyFromId($id)
143
    {
144
        $id = $this->removePrefix($id);
145
146
        $lastSeparator = strrpos($id, '/');
147
        $secondLast = strrpos($id, '/', $lastSeparator - strlen($id) - 1) + 1;
148
149
        $keyLength = abs($secondLast - $lastSeparator);
150
        $key = substr($id, $secondLast, $keyLength);
151
        $this->checkMappingExistence($key);
152
153
        return $key;
154
    }
155
156
    /**
157
     * getKeyFromModel
158
     *
159
     * @param string $modelName model name
160
     * @access public
161
     * @return string
162
     * @throws MappingException
163
     */
164 View Code Duplication
    public function getKeyFromModel($modelName)
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...
165
    {
166
        foreach ($this->classMetadatas as $classMetadata) {
167
            if ($modelName === $classMetadata->getModelName()) {
168
                return $classMetadata->getKey();
169
            }
170
        }
171
172
        throw new MappingException('Model name ' . $modelName . ' not found in mapping');
173
    }
174
175
    /**
176
     * getClassMetadata for model name
177
     *
178
     * @param string $modelName
179
     * @access public
180
     * @return ClassMetadata
181
     * @throws MappingException
182
     */
183 View Code Duplication
    public function getClassMetadata($modelName)
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...
184
    {
185
        foreach ($this->classMetadatas as $classMetadata) {
186
            if ($modelName === $classMetadata->getModelName()) {
187
                return $classMetadata;
188
            }
189
        }
190
191
        throw new MappingException($modelName . ' model is not mapped');
192
    }
193
194
    /**
195
     * getClassMetadata for model short name
196
     *
197
     * @param string $modelShortName
198
     * @access public
199
     * @return ClassMetadata|null
200
     */
201
    public function tryGetClassMetadataByShortName($modelShortName)
202
    {
203
        foreach ($this->classMetadatas as $classMetadata) {
204
            if ($modelShortName === $classMetadata->getModelShortName()) {
205
                return $classMetadata;
206
            }
207
        }
208
    }
209
210
    /**
211
     * hasClassMetadata
212
     *
213
     * @param string $modelName
214
     * @access public
215
     * @return boolean
216
     */
217
    public function hasClassMetadata($modelName)
218
    {
219
        foreach ($this->classMetadatas as $classMetadata) {
220
            if ($modelName === $classMetadata->getModelName()) {
221
                return true;
222
            }
223
        }
224
225
        return false;
226
    }
227
228
    /**
229
     * getMappingByKey
230
     *
231
     * @param string $key
232
     * @access public
233
     * @return ClassMetadata|null
234
     */
235
    public function getClassMetadataByKey($key)
236
    {
237
        foreach ($this->classMetadatas as $classMetadata) {
238
            if ($key === $classMetadata->getKey()) {
239
                return $classMetadata;
240
            }
241
        }
242
    }
243
244
    /**
245
     * checkMappingExistence
246
     *
247
     * @param string $key
248
     * @param string|null $subKey
249
     * @access private
250
     * @return void
251
     */
252
    private function checkMappingExistence($key, $subKey = null)
253
    {
254
        if (empty($key)) {
255
            throw new MappingException('key is not set');
256
        }
257
258
        $metadata = $this->getClassMetadataByKey($key);
259
        if (!$metadata) {
260
            throw new MappingException($key . ' key is not mapped');
261
        }
262
263
        if (!empty($subKey)) {
264
            $methodName = 'get' . ucfirst($subKey);
265
            if (!$metadata->$methodName()) {
266
                throw new MappingException($key . ' key is mapped but no ' . $subKey . ' found');
267
            }
268
        }
269
    }
270
271
    /**
272
     * removePrefix
273
     *
274
     * @param mixed $value
275
     * @access private
276
     * @return string
277
     */
278
    private function removePrefix($value)
279
    {
280
        if ($this->idPrefix && strpos($value, $this->idPrefix) !== false) {
281
            return substr($value, $this->idPrefixLength);
282
        }
283
284
        return $value;
285
    }
286
}
287