Completed
Push — master ( 9df0a4...b657f4 )
by Welling
02:10
created

ClientRemote::updateItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 7
loc 7
ccs 0
cts 4
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Directus – <http://getdirectus.com>
5
 *
6
 * @link      The canonical repository – <https://github.com/directus/directus>
7
 * @copyright Copyright 2006-2016 RANGER Studio, LLC – <http://rangerstudio.com>
8
 * @license   GNU General Public License (v3) – <http://www.gnu.org/copyleft/gpl.html>
9
 */
10
11
namespace Directus\SDK;
12
13
use Directus\Util\ArrayUtils;
14
15
/**
16
 * Client Remote
17
 *
18
 * @author Welling Guzmán <[email protected]>
19
 */
20
class ClientRemote extends BaseClientRemote
21
{
22
    /**
23
     * @inheritdoc
24
     */
25 2
    public function getTables(array $params = [])
26
    {
27 2
        return $this->performRequest('GET', static::TABLE_LIST_ENDPOINT);
28
    }
29
30
    /**
31
     * @inheritdoc
32
     */
33 4
    public function getTable($tableName)
34
    {
35 4
        $path = $this->buildPath(static::TABLE_INFORMATION_ENDPOINT, $tableName);
36
37 4
        return $this->performRequest('GET', $path);
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43 2
    public function getColumns($tableName, array $params = [])
44
    {
45 2
        $path = $this->buildPath(static::COLUMN_LIST_ENDPOINT, $tableName);
46
47 2
        return $this->performRequest('GET', $path);
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53 2
    public function getColumn($tableName, $columnName)
54
    {
55 2
        $path = $this->buildPath(static::COLUMN_INFORMATION_ENDPOINT, [$tableName, $columnName]);
56
57 2
        return $this->performRequest('GET', $path);
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63 2
    public function getItems($tableName, array $options = [])
64
    {
65 2
        $path = $this->buildPath(static::TABLE_ENTRIES_ENDPOINT, $tableName);
66
67 2
        return $this->performRequest('GET', $path, ['query' => $options]);
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73 2
    public function getItem($tableName, $id, array $options = [])
74
    {
75 2
        $path = $this->buildPath(static::TABLE_ENTRY_ENDPOINT, [$tableName, $id]);
76
77 2
        return $this->performRequest('GET', $path, ['query' => $options]);
78
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83 2
    public function getUsers(array $params = [])
84
    {
85 2
        return $this->getItems('directus_users', $params);
86
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91 2
    public function getUser($id, array $params = [])
92
    {
93 2
        return $this->getItem($id, 'directus_users', $params);
94
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99 2
    public function getGroups()
100
    {
101 2
        return $this->performRequest('GET', static::GROUP_LIST_ENDPOINT);
102
    }
103
104
    /**
105
     * @inheritdoc
106
     */
107 2
    public function getGroup($groupID)
108
    {
109 2
        $path = $this->buildPath(static::GROUP_INFORMATION_ENDPOINT, $groupID);
110
111 2
        return $this->performRequest('GET', $path);
112
    }
113
114
    /**
115
     * @inheritdoc
116
     */
117 2
    public function getGroupPrivileges($groupID)
118
    {
119 2
        $path = $this->buildPath(static::GROUP_PRIVILEGES_ENDPOINT, $groupID);
120
121 2
        return $this->performRequest('GET', $path);
122
    }
123
124
    /**
125
     * @inheritdoc
126
     */
127 2
    public function getFiles(array $params = [])
128
    {
129 2
        return $this->performRequest('GET', static::FILE_LIST_ENDPOINT, ['query' => $params]);
130
    }
131
132
    /**
133
     * @inheritdoc
134
     */
135 2
    public function getFile($fileID)
136
    {
137 2
        $path = $this->buildPath(static::FILE_INFORMATION_ENDPOINT, $fileID);
138
139 2
        return $this->performRequest('GET', $path);
140
    }
141
142
    /**
143
     * @inheritdoc
144
     */
145 2
    public function getSettings()
146
    {
147 2
        return $this->performRequest('GET', static::SETTING_LIST_ENDPOINT);
148
    }
149
150
    /**
151
     * @inheritdoc
152
     */
153 2
    public function getSettingsByCollection($collectionName)
154
    {
155 2
        $path = $this->buildPath(static::SETTING_COLLECTION_GET_ENDPOINT, $collectionName);
156
157 2
        return $this->performRequest('GET', $path);
158
    }
159
160
    /**
161
     * @inheritdoc
162
     */
163
    public function updateSettings($collection, array $data)
164
    {
165
        $path = $this->buildPath(static::SETTING_COLLECTION_UPDATE_ENDPOINT, $collection);
166
167
        return $this->performRequest('PUT', $path, [
168
            'body' => $data
169
        ]);
170
    }
171
172
    /**
173
     * @inheritdoc
174
     */
175 View Code Duplication
    public function getMessages($userId = null)
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...
176
    {
177
        if ($userId !== null) {
178
            $path = $this->buildPath(static::MESSAGES_USER_LIST_ENDPOINT, $userId);
179
        } else {
180
            $path = $this->buildPath(static::MESSAGES_LIST_ENDPOINT);
181
        }
182
183
        return $this->performRequest('GET', $path);
184
    }
185
186
    /**
187
     * @inheritdoc
188
     */
189
    public function getMessage($id)
190
    {
191
        $path = $this->buildPath(static::MESSAGES_GET_ENDPOINT, $id);
192
193
        return $this->performRequest('GET', $path);
194
    }
195
196
    /**
197
     * @inheritdoc
198
     */
199 View Code Duplication
    public function createItem($tableName, array $data)
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...
200
    {
201
        $path = $this->buildPath(static::TABLE_ENTRY_CREATE_ENDPOINT, $tableName);
202
        $data = $this->processData($tableName, $data);
203
204
        return $this->performRequest('POST', $path, ['body' => $data]);
205
    }
206
207
    /**
208
     * @inheritdoc
209
     */
210 View Code Duplication
    public function updateItem($tableName, $id, array $data)
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...
211
    {
212
        $path = $this->buildPath(static::TABLE_ENTRY_UPDATE_ENDPOINT, [$tableName, $id]);
213
        $data = $this->processData($tableName, $data);
214
215
        return $this->performRequest('PUT', $path, ['body' => $data]);
216
    }
217
218
    /**
219
     * @inheritdoc
220
     */
221
    public function deleteItem($tableName, $id, $hard = false)
222
    {
223
        $path = $this->buildPath(static::TABLE_ENTRY_DELETE_ENDPOINT, [$tableName, $id]);
224
        $options = [];
225
226
        if ($hard !== true) {
227
            $options = [
228
                'query' => ['soft' => true]
229
            ];
230
        }
231
232
        return $this->performRequest('DELETE', $path, $options);
233
    }
234
235
    /**
236
     * @inheritdoc
237
     */
238
    public function createUser(array $data)
239
    {
240
        return $this->createItem('directus_users', $data);
241
    }
242
243
    /**
244
     * @inheritdoc
245
     */
246
    public function updateUser($id, array $data)
247
    {
248
        return $this->updateItem('directus_users', $id, $data);
249
    }
250
251
    /**
252
     * @inheritdoc
253
     */
254
    public function deleteUser($ids, $hard = false)
255
    {
256
        return $this->deleteItem('directus_users', $ids, $hard);
257
    }
258
259
    /**
260
     * @inheritdoc
261
     */
262
    public function createFile(File $file)
263
    {
264
        $data = $this->processFile($file);
265
266
        return $this->performRequest('POST', static::FILE_CREATE_ENDPOINT, ['body' => $data]);
267
    }
268
269
    /**
270
     * @inheritdoc
271
     */
272
    public function updateFile($id, $data)
273
    {
274
        if ($data instanceof File) {
275
            $data = $data->toArray();
276
        }
277
278
        $data['id'] = $id;
279
        $path = $this->buildPath(static::FILE_UPDATE_ENDPOINT, $id);
280
        $data = $this->processData('directus_files', $data);
281
282
        return $this->performRequest('POST', $path, ['body' => $data]);
283
    }
284
285
    /**
286
     * @inheritdoc
287
     */
288
    public function deleteFile($id, $hard = false)
289
    {
290
        return $this->deleteItem('directus_files', $id, $hard);
291
    }
292
293
    public function createPreferences($data)
294
    {
295
        $this->requiredAttributes(['title', 'table_name'], $data);
296
297
        $tableName = ArrayUtils::get($data, 'table_name');
298
        $path = $this->buildPath(static::TABLE_PREFERENCES_ENDPOINT, $tableName);
299
        $data = $this->processData($tableName, $data);
300
301
        return $this->performRequest('POST', $path, ['body' => $data]);
302
    }
303
304
    /**
305
     * @inheritdoc
306
     */
307
    public function createBookmark($data)
308
    {
309
        $preferences = $this->createPreferences(ArrayUtils::pick($data, [
310
            'title', 'table_name', 'sort', 'status', 'search_string', 'sort_order', 'columns_visible'
311
        ]));
312
313
        $title = $preferences->title;
0 ignored issues
show
Documentation introduced by
The property title does not exist on object<Directus\SDK\Response\Entry>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
314
        $tableName = $preferences->table_name;
0 ignored issues
show
Documentation introduced by
The property table_name does not exist on object<Directus\SDK\Response\Entry>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
315
        $bookmarkData = [
316
            'section' => 'search',
317
            'title' => $title,
318
            'url' => 'tables/' . $tableName . '/pref/' . $title
319
        ];
320
321
        $path = $this->buildPath(static::BOOKMARKS_CREATE_ENDPOINT);
322
        $bookmarkData = $this->processData($tableName, $bookmarkData);
323
324
        return $this->performRequest('POST', $path, ['body' => $bookmarkData]);
325
    }
326
327
    /**
328
     * @inheritdoc
329
     */
330
    public function getBookmark($id)
331
    {
332
        $path = $this->buildPath(static::BOOKMARKS_READ_ENDPOINT, $id);
333
334
        return $this->performRequest('GET', $path);
335
    }
336
337
    /**
338
     * @inheritdoc
339
     */
340 View Code Duplication
    public function getBookmarks($userId = null)
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...
341
    {
342
        if ($userId !== null) {
343
            $path = $this->buildPath(static::BOOKMARKS_USER_ENDPOINT, $userId);
344
        } else {
345
            $path = $this->buildPath(static::BOOKMARKS_ALL_ENDPOINT);
346
        }
347
348
        return $this->performRequest('GET', $path);
349
    }
350
351
    /**
352
     * @inheritdoc
353
     */
354
    public function createColumn($data)
355
    {
356
        $data = $this->parseColumnData($data);
357
358
        return $this->performRequest('POST', $this->buildPath(static::COLUMN_CREATE_ENDPOINT, $data['table_name']), [
359
            'body' => $data
360
        ]);
361
    }
362
363
    /**
364
     * @inheritdoc
365
     */
366
    public function createGroup(array $data)
367
    {
368
        return $this->performRequest('POST', static::GROUP_CREATE_ENDPOINT, [
369
            'body' => $data
370
        ]);
371
    }
372
373
    /**
374
     * @inheritdoc
375
     */
376
    public function createMessage(array $data)
377
    {
378
        $this->requiredAttributes(['from', 'message', 'subject'], $data);
379
        $this->requiredOneAttribute(['to', 'toGroup'], $data);
380
381
        $data['recipients'] = $this->getMessagesTo($data);
382
        ArrayUtils::remove($data, ['to', 'toGroup']);
383
384
        return $this->performRequest('POST', static::MESSAGES_CREATE_ENDPOINT, [
385
            'body' => $data
386
        ]);
387
    }
388
389
    /**
390
     * @inheritdoc
391
     */
392
    public function sendMessage(array $data)
393
    {
394
        return $this->createMessage($data);
395
    }
396
397
    /**
398
     * @inheritdoc
399
     */
400
    public function createPrivileges(array $data)
401
    {
402
        $this->requiredAttributes(['group_id', 'table_name'], $data);
403
404
        return $this->performRequest('POST', static::GROUP_PRIVILEGES_CREATE_ENDPOINT, [
405
            'body' => $data
406
        ]);
407
    }
408
409
    public function createTable($name, array $params = [])
410
    {
411
        $data = [
412
            'addTable' => true,
413
            'table_name' => $name
414
        ];
415
416
        return $this->performRequest('POST', static::TABLE_CREATE_ENDPOINT, [
417
            'body' => $data
418
        ]);
419
    }
420
421
    /**
422
     * @inheritdoc
423
     */
424
    public function createColumnUIOptions(array $data)
425
    {
426
        $this->requiredAttributes(['table', 'column', 'ui', 'options'], $data);
427
428
        $path = $this->buildPath(static::COLUMN_OPTIONS_CREATE_ENDPOINT, [
429
            $data['table'],
430
            $data['column'],
431
            $data['ui']
432
        ]);
433
434
        $data = ArrayUtils::get($data, 'options');
435
436
        return $this->performRequest('POST', $path, [
437
            'body' => $data
438
        ]);
439
    }
440
441
    /**
442
     * @inheritdoc
443
     */
444
    public function getPreferences($table, $user = null)
445
    {
446
        return $this->performRequest('POST', $this->buildPath(static::TABLE_PREFERENCES_ENDPOINT, $table));
447
    }
448
449
    /**
450
     * @inheritdoc
451
     */
452
    public function deleteBookmark($id, $hard = false)
453
    {
454
        return $this->deleteItem('directus_bookmarks', $id, $hard);
455
    }
456
457
    /**
458
     * @inheritdoc
459
     */
460
    public function deleteColumn($name, $table)
461
    {
462
        $path = $this->buildPath(static::COLUMN_DELETE_ENDPOINT, [$name, $table]);
463
464
        return $this->performRequest('DELETE', $path);
465
    }
466
467
    /**
468
     * @inheritdoc
469
     */
470
    public function deleteGroup($id, $hard = false)
471
    {
472
        return $this->deleteItem('directus_groups', $id, $hard);
473
    }
474
475
    /**
476
     * @inheritdoc
477
     */
478
    public function deleteTable($name)
479
    {
480
        $path = $this->buildPath(static::TABLE_DELETE_ENDPOINT, $name);
481
482
        return $this->performRequest('DELETE', $path);
483
    }
484
485
    /**
486
     * @inheritdoc
487
     */
488
    public function getActivity(array $params = [])
489
    {
490
        $path = $this->buildPath(static::ACTIVITY_GET_ENDPOINT);
491
492
        return $this->performRequest('GET', $path, [
493
            'query' => $params
494
        ]);
495
    }
496
497
    /**
498
     * @inheritdoc
499
     */
500
    public function getRandom(array $options = [])
501
    {
502
        $path = $this->buildPath(static::UTILS_RANDOM_ENDPOINT);
503
504
        return $this->performRequest('POST', $path, ['body' => $options]);
505
    }
506
507
    /**
508
     * @inheritdoc
509
     */
510
    public function getHash($string, array $options = [])
511
    {
512
        $path = $this->buildPath(static::UTILS_HASH_ENDPOINT);
513
514
        $data = [
515
            'string' => $string
516
        ];
517
518
        if (ArrayUtils::has($options, 'hasher')) {
519
            $data['hasher'] = ArrayUtils::pull($options, 'hasher');
520
        }
521
522
        $data['options'] = $options;
523
524
        return $this->performRequest('POST', $path, ['body' => $data]);
525
    }
526
}
527