Completed
Push — master ( 66aaa5...b237f3 )
by Colin
01:09
created

Model::getAttributeFromArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace MacsiDigital\Xero\Support;
4
5
use Exception;
6
use Illuminate\Support\Collection;
7
8
abstract class Model
9
{
10
    protected $attributes = [];
11
    protected $query_attributes = [];
12
    protected $relationships = [];
13
    protected $queries = [];
14
    protected $methods = [];
15
16
    public $response;
17
18
    const ENDPOINT = '';
19
    const NODE_NAME = '';
20
    const KEY_FIELD = '';
21
22
    protected $client;
23
24
    public function __construct()
25
    {
26
        $this->client = app()->xero->client;
27
    }
28
29
    /**
30
     * Get the resource uri of the class (Contacts) etc.
31
     *
32
     * @return string
33
     */
34
    public static function getEndpoint()
35
    {
36
        return static::ENDPOINT;
37
    }
38
39
    /**
40
     * Get the root node name.  Just the unqualified classname.
41
     *
42
     * @return string
43
     */
44
    public static function getRootNodeName()
45
    {
46
        return static::NODE_NAME;
47
    }
48
49
    /**
50
     * Get the unique key field.
51
     *
52
     * @return string
53
     */
54
    public static function getKey()
55
    {
56
        return static::KEY_FIELD;
57
    }
58
59
    /**
60
     * Get the object unique ID.
61
     *
62
     * @return string
63
     */
64
    public function getID()
65
    {
66
        $index = $this->getKey();
67
68
        return $this->$index;
69
    }
70
71
    public function hasID()
72
    {
73
        $index = $this->getKey();
74
        if ($this->$index != '') {
75
            return true;
76
        }
77
78
        return false;
79
    }
80
81
    public function getAttributes()
82
    {
83
        return $this->attributes;
84
    }
85
86
    /**
87
     * Get an attribute from the model.
88
     *
89
     * @param  string  $key
90
     * @return mixed
91
     */
92
    public function getAttribute($key)
93
    {
94
        if (! $key) {
95
            return;
96
        }
97
        if ($this->attributeExists($key)) {
98
            return $this->getAttributeValue($key);
99
        }
100
    }
101
102
    /**
103
     * Get a plain attribute (not a relationship).
104
     *
105
     * @param  string  $key
106
     * @return mixed
107
     */
108
    public function getAttributeValue($key)
109
    {
110
        return $this->getAttributeFromArray($key);
111
    }
112
113
    /**
114
     * Get an attribute from the $attributes array.
115
     *
116
     * @param  string  $key
117
     * @return mixed
118
     */
119
    protected function getAttributeFromArray($key)
120
    {
121
        if (isset($this->attributes[$key])) {
122
            return $this->attributes[$key];
123
        }
124
    }
125
126
    /**
127
     * Set a given attribute on the model.
128
     *
129
     * @param  string  $key
130
     * @param  mixed  $value
131
     * @return mixed
132
     */
133
    public function setAttribute($key, $value)
134
    {
135
        if ($this->attributeExists($key)) {
136
            $this->attributes[$key] = $value;
137
        }
138
139
        return $this;
140
    }
141
142
    public function processRelationships()
143
    {
144
        foreach ($this->relationships as $key => $class) {
145
            if (is_array($this->$key) && in_array($this->getKey(), $this->$key)) {
146
                if (! is_object($this->$key)) {
147
                    $this->attributes[$key] = ($class)::make($this->$key);
148
                }
149
            } else {
150
                foreach ($this->$key as $index => $item) {
151
                    if (! is_object($item)) {
152
                        $this->attributes[$key][$index] = ($class)::make($item);
153
                    }
154
                }
155
            }
156
        }
157
158
        return $this;
159
    }
160
161
    public function attributeExists($key)
162
    {
163
        return array_key_exists($key, $this->attributes);
164
    }
165
166
    public function unsetAttribute($key)
167
    {
168
        $this->setAttribute($key, '');
169
    }
170
171
    /**
172
     * Dynamically retrieve attributes on the model.
173
     *
174
     * @param  string  $key
175
     * @return mixed
176
     */
177
    public function __get($key)
178
    {
179
        return $this->getAttribute($key);
180
    }
181
182
    /**
183
     * Dynamically set attributes on the model.
184
     *
185
     * @param  string  $key
186
     * @param  mixed  $value
187
     * @return void
188
     */
189
    public function __set($key, $value)
190
    {
191
        $this->setAttribute($key, $value);
192
    }
193
194
    /**
195
     * Determine if an attribute or relation exists on the model.
196
     *
197
     * @param  string  $key
198
     * @return bool
199
     */
200
    public function __isset($key)
201
    {
202
        return $this->attributeExists($key);
203
    }
204
205
    /**
206
     * Unset an attribute on the model.
207
     *
208
     * @param  string  $key
209
     * @return void
210
     */
211
    public function __unset($key)
212
    {
213
        $this->unsetAttribute($key);
214
    }
215
216
    public static function make($attributes)
217
    {
218
        $model = new static;
219
        foreach ($attributes as $attribute => $value) {
220
            $model->$attribute = $value;
221
        }
222
223
        return $model;
224
    }
225
226
    public static function create($attributes)
227
    {
228
        $model = static::make($attributes);
229
        $model->save();
230
231
        return $model;
232
    }
233
234
    public function fill($attributes)
235
    {
236
        foreach ($attributes as $attribute => $value) {
237
            $this->$attribute = $value;
238
        }
239
240
        return $this;
241
    }
242
243
    public function update($attributes)
244
    {
245
        $this->fill($attributes)->save();
246
247
        return $this;
248
    }
249
250
    public function save()
251
    {
252
        if ($this->hasID()) {
253
            if (in_array('put', $this->methods)) {
254
                $this->response = $this->client->post($this->getEndpoint().'/'.$this->getID(), $this->attributes);
255
                if ($this->response->getStatusCode() == '200') {
256
                    return $this->response->getContents();
257
                } else {
258
                    throw new Exception('Status Code '.$this->response->getStatusCode());
259
                }
260
            }
261
        } else {
262
            if (in_array('post', $this->methods)) {
263
                $this->response = $this->client->post($this->getEndpoint(), $this->attributes);
264
                if ($this->response->getStatusCode() == '200') {
265
                    $saved_item = $this->collect($this->response->getContents())->first();
266
                    $index = $this->GetKey();
267
                    $this->$index = $saved_item->$index;
268
269
                    return $this->response->getContents();
270
                } else {
271
                    throw new Exception('Status Code '.$this->response->getStatusCode());
272
                }
273
            }
274
        }
275
    }
276
277
    public function where($key, $operator, $value="")
278
    {
279
        if(in_array($key, $this->query_attributes)){
280
            if($value == ""){
281
                $value = $operator;
282
                $operator = '=';
283
            }
284
            $this->queries[$key] = ['key' => $key, 'operator' => $operator, 'value' => $value];
285
        }
286
287
        return $this;
288
    }
289
290
    public function getQueryString() 
291
    {
292
        $query_string = '';
293
        if($this->queries != []){
294
            $query_string .= '?';
295
            $i = 1;
296
            foreach($this->queries as $query){
297
                if($i>1){
298
                    $query_string .= '&';
299
                }
300
                $query_string  .= $query['key'].$query['operator'].$query['value'];
301
                $i++;
302
            }
303
        }
304
        
305
        return $query_String;
0 ignored issues
show
Bug introduced by
The variable $query_String does not exist. Did you mean $query_string?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
306
    }
307
308
    public function first()
309
    {
310
        return $this->get()->first();
311
    }
312
313 View Code Duplication
    public function get()
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...
314
    {
315
        if (in_array('get', $this->methods)) {
316
            $this->response = $this->client->get($this->getEndpoint().$this->getQueryString());
317
            if ($this->response->getStatusCode() == '200') {
318
                return $this->collect($this->response->getContents());
319
            } else {
320
                throw new Exception('Status Code '.$this->response->getStatusCode());
321
            }
322
        }
323
    }
324
325 View Code Duplication
    public function all()
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...
326
    {
327
        if (in_array('get', $this->methods)) {
328
            $this->response = $this->client->get($this->getEndpoint());
329
            if ($this->response->getStatusCode() == '200') {
330
                return $this->collect($this->response->getContents());
331
            } else {
332
                throw new Exception('Status Code '.$this->response->getStatusCode());
333
            }
334
        }
335
    }
336
337 View Code Duplication
    public function find($id)
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...
338
    {
339
        if (in_array('get', $this->methods)) {
340
            $this->response = $this->client->get($this->getEndpoint().'/'.$id);
341
            if ($this->response->getStatusCode() == '200') {
342
                return $this->collect($this->response->getContents())->first();
343
            } else {
344
                throw new Exception('Status Code '.$this->response->getStatusCode());
345
            }
346
        }
347
    }
348
349
    public function delete($id = "")
350
    {
351
        if ($id == '') {
352
            $id = $this->getID();
353
        }
354
        if (in_array('delete', $this->methods)) {
355
            $this->response = $this->client->delete($this->getEndpoint().'/'.$id);
356
            if ($this->response->getStatusCode() == '200') {
357
                return $this->response->getStatusCode();
358
            } else {
359
                throw new Exception('Status Code '.$this->response->getStatusCode());
360
            }
361
        }
362
    }
363
364
    protected function collect($response)
365
    {
366
        $items = [];
367
        foreach ($response[$this->getEndpoint()] as $item) {
368
            $items[] = static::make($item);
369
        }
370
371
        return new Collection($items);
372
    }
373
}
374