Completed
Pull Request — master (#53)
by Julien
02:22
created

Mapping::setConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
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 array
30
     */
31
    private $mapping = [];
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 array $mapping
95
     * @access public
96
     * @return Mapping
97
     */
98
    public function setMapping(array $mapping)
99
    {
100
        $this->mapping = $mapping;
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 array
124
     */
125
    public function getMappingKeys()
126
    {
127
        return array_map(
128
            function ($mapping) {
129
                return $mapping->getKey();
130
            },
131
            $this->mapping
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->mapping as $mapping) {
167
            if ($modelName === $mapping->getModelName()) {
168
                return $mapping->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
     */
182 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...
183
    {
184
        foreach ($this->mapping as $mapping) {
185
            if ($modelName === $mapping->getModelName()) {
186
                return $mapping;
187
            }
188
        }
189
190
        throw new MappingException($modelName . ' model is not mapped');
191
    }
192
193
    /**
194
     * hasClassMetadata
195
     *
196
     * @param string $modelName
197
     * @access public
198
     * @return boolean
199
     */
200
    public function hasClassMetadata($modelName)
201
    {
202
        foreach ($this->mapping as $mapping) {
203
            if ($modelName === $mapping->getModelName()) {
204
                return true;
205
            }
206
        }
207
208
        return false;
209
    }
210
211
    /**
212
     * getMappingByKey
213
     *
214
     * @param string $key
215
     * @access public
216
     * @return ClassMetadata|null
217
     */
218
    public function getClassMetadataByKey($key)
219
    {
220
        foreach ($this->mapping as $mapping) {
221
            if ($key === $mapping->getKey()) {
222
                return $mapping;
223
            }
224
        }
225
    }
226
227
    /**
228
     * checkMappingExistence
229
     *
230
     * @param string $key
231
     * @param string|null $subKey
232
     * @access private
233
     * @return void
234
     */
235
    private function checkMappingExistence($key, $subKey = null)
236
    {
237
        if (empty($key)) {
238
            throw new MappingException('key is not set');
239
        }
240
241
        $metadata = $this->getClassMetadataByKey($key);
242
        if (!$metadata) {
243
            throw new MappingException($key . ' key is not mapped');
244
        }
245
246
        if (!empty($subKey)) {
247
            $methodName = 'get' . ucfirst($subKey);
248
            if (!$metadata->$methodName()) {
249
                throw new MappingException($key . ' key is mapped but no ' . $subKey . ' found');
250
            }
251
        }
252
    }
253
254
    /**
255
     * removePrefix
256
     *
257
     * @param mixed $value
258
     * @access private
259
     * @return string
260
     */
261
    private function removePrefix($value)
262
    {
263
        if ($this->idPrefix && strpos($value, $this->idPrefix) !== false) {
264
            return substr($value, $this->idPrefixLength);
265
        }
266
267
        return $value;
268
    }
269
}
270