Completed
Pull Request — master (#58)
by
unknown
06:38
created

ClassMetadata::getIdGetter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Mapado\RestClientSdk\Mapping;
4
5
/**
6
 * Class ClassMetadata
7
 * @author Julien Deniau <[email protected]>
8
 */
9
class ClassMetadata
10
{
11
    /**
12
     * Model name (entity class with full namespace, ie: "Foo\Entity\Article").
13
     *
14
     * @var string
15
     * @access private
16
     */
17
    private $modelName;
18
19
    /**
20
     * Model key, used as path prefix for API calls.
21
     *
22
     * @var string
23
     * @access private
24
     */
25
    private $key;
26
27
    /**
28
     * Repository name (repository class with full namespace, ie: "Foo\Repository\ArticleRepository").
29
     *
30
     * @var string
31
     * @access private
32
     */
33
    private $repositoryName;
34
35
    /**
36
     * attributeList
37
     *
38
     * @var Attribute[]
39
     * @access private
40
     */
41
    private $attributeList;
42
43
    /**
44
     * relationList
45
     *
46
     * @var Relation[]
47
     * @access private
48
     */
49
    private $relationList;
50
51
    /**
52
     * identifierAttribute
53
     *
54
     * @var Attribute
55
     * @access private
56
     */
57
    private $identifierAttribute;
58
59
    /**
60
     * Constructor.
61
     *
62
     * @param string $key
63
     * @param string $modelName
64
     * @param string $repositoryName
65
     * @access public
66
     */
67
    public function __construct($key, $modelName, $repositoryName)
68
    {
69
        $this->key = $key;
70
        $this->modelName = $modelName;
71
        $this->repositoryName = $repositoryName;
72
    }
73
74
    /**
75
     * Getter for modelName
76
     *
77
     * @return string
78
     */
79
    public function getModelName()
80
    {
81
        return $this->modelName;
82
    }
83
84
    /**
85
     * Setter for modelName
86
     *
87
     * @param string $modelName
88
     * @return ClassMetadata
89
     */
90
    public function setModelName($modelName)
91
    {
92
        $this->modelName = $modelName;
93
        return $this;
94
    }
95
96
    /**
97
     * Getter for key
98
     *
99
     * @return string
100
     */
101
    public function getKey()
102
    {
103
        return $this->key;
104
    }
105
106
    /**
107
     * Setter for key
108
     *
109
     * @param string $key
110
     * @return ClassMetadata
111
     */
112
    public function setKey($key)
113
    {
114
        $this->key = $key;
115
116
        return $this;
117
    }
118
119
    /**
120
     * getAttribute
121
     *
122
     * @param string $name
123
     * @access public
124
     * @return Attribute
125
     */
126
    public function getAttribute($name)
127
    {
128
        return isset($this->attributeList[$name]) ? $this->attributeList[$name] : null;
129
    }
130
131
    /**
132
     * getIdentifierAttribute
133
     *
134
     * @access public
135
     * @return Attribute
136
     */
137
    public function getIdentifierAttribute()
138
    {
139
        return $this->identifierAttribute;
140
    }
141
142
    /**
143
     * Getter for attributeList
144
     *
145
     * @return Attribute[]
146
     */
147
    public function getAttributeList()
148
    {
149
        return $this->attributeList;
150
    }
151
152
    /**
153
     * Setter for attributeList
154
     *
155
     * @param Attribute[] $attributeList
156
     * @return ClassMetadata
157
     */
158
    public function setAttributeList($attributeList)
159
    {
160
        $this->attributeList = [];
161
        foreach ($attributeList as $attribute) {
162
            $this->attributeList[$attribute->getSerializedKey()] = $attribute;
163
164
            if ($attribute->isIdentifier()) {
165
                $this->identifierAttribute = $attribute;
166
            }
167
        }
168
        return $this;
169
    }
170
171
    /**
172
     * Getter for relationList
173
     *
174
     * @return Relation[]
175
     */
176
    public function getRelationList()
177
    {
178
        return $this->relationList;
179
    }
180
181
    /**
182
     * Setter for relationList
183
     *
184
     * @param Relation[] $relationList
185
     * @return ClassMetadata
186
     */
187
    public function setRelationList($relationList)
188
    {
189
        $this->relationList = $relationList;
190
        return $this;
191
    }
192
193
    /**
194
     * getRelation
195
     *
196
     * @param string $key
197
     * @access public
198
     * @return Relation|null
199
     */
200
    public function getRelation($key)
201
    {
202
        if (!empty($this->relationList)) {
203
            foreach ($this->relationList as $relation) {
204
                if ($relation->getSerializedKey() == $key) {
205
                    return $relation;
206
                }
207
            }
208
        }
209
    }
210
211
    /**
212
     * Getter for repositoryName
213
     *
214
     * @return string
215
     */
216
    public function getRepositoryName()
217
    {
218
        return $this->repositoryName;
219
    }
220
221
    /**
222
     * Setter for repositoryName
223
     *
224
     * @param string $repositoryName
225
     * @return ClassMetadata
226
     */
227
    public function setRepositoryName($repositoryName)
228
    {
229
        $this->repositoryName = $repositoryName;
230
        return $this;
231
    }
232
233 View Code Duplication
    private function getClassMetadata()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
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...
234
    {
235
        if (!isset($this->classMetadata)) {
236
            $this->classMetadataCache = $this->sdk
0 ignored issues
show
Bug introduced by
The property classMetadataCache does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property sdk does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
237
                ->getMapping()
238
                ->getClassMetadata($this->entityName);
0 ignored issues
show
Bug introduced by
The property entityName does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
239
        }
240
241
        return $this->classMetadataCache;
242
    }
243
244
    public function getIdGetter()
245
    {
246
        return 'get' . ucfirst($this->getIdKey());
247
    }
248
249
    public function getIdKey()
250
    {
251
        if ($this->getIdentifierAttribute()) {
252
            $idAttr = $this->getIdentifierAttribute()
253
                ->getAttributeName();
254
            return $idAttr;
255
        } else {
256
            return 'id';
257
        }
258
    }
259
260
    public function getIdSerializeKey()
261
    {
262
        if ($this->getIdentifierAttribute()) {
263
            $idAttr = $this->getIdentifierAttribute()
264
                ->getSerializedKey();
265
            return $idAttr;
266
        } else {
267
            return '@id';
268
        }
269
    }
270
}
271