Completed
Push — d64 ( f52daa...138eb5 )
by Welling
02:03
created

ClientRemote::getUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
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
use Directus\Util\ArrayUtils;
13
use Directus\Util\StringUtils;
14
15
/**
16
 * Client Remote
17
 *
18
 * @author Welling Guzmán <[email protected]>
19
 */
20
class ClientRemote extends BaseClientRemote
21
{
22
    protected $baseEndpoint = 'http://localhost/api';
23 2
    protected $hostedBaseEndpointFormat = 'https://%s.directus.io/api';
24
25 2
    /**
26
     * @inheritdoc
27
     */
28 4
    public function getTables(array $params = [])
29
    {
30 4
        return $this->performRequest('GET', static::TABLE_LIST_ENDPOINT);
1 ignored issue
show
Bug Compatibility introduced by
The expression $this->performRequest('G...::TABLE_LIST_ENDPOINT); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\Entry to the return on line 30 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getTables of type Directus\SDK\Response\EntryCollection.
Loading history...
31
    }
32
33 2
    /**
34
     * @inheritdoc
35 2
     */
36
    public function getTable($tableName)
37
    {
38 2
        return $this->performRequest('GET', static::TABLE_INFORMATION_ENDPOINT, $tableName);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('G..._ENDPOINT, $tableName); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 38 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getTable of type Directus\SDK\Response\Entry.
Loading history...
39
    }
40 2
41
    /**
42
     * @inheritdoc
43 2
     */
44
    public function getColumns($tableName, array $params = [])
45 2
    {
46
        return $this->performRequest('GET', static::COLUMN_LIST_ENDPOINT, $tableName);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('G..._ENDPOINT, $tableName); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\Entry to the return on line 46 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getColumns of type Directus\SDK\Response\EntryCollection.
Loading history...
47
    }
48 2
49
    /**
50 2
     * @inheritdoc
51
     */
52
    public function getColumn($tableName, $columnName)
53 2
    {
54
        return $this->performRequest('GET', static::COLUMN_INFORMATION_ENDPOINT, [$tableName, $columnName]);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('G...bleName, $columnName)); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 54 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getColumn of type Directus\SDK\Response\Entry.
Loading history...
55 2
    }
56
57
    /**
58 2
     * @inheritdoc
59
     */
60 2
    public function getEntries($tableName, array $options = [])
61
    {
62
        return $this->performRequest('GET', static::TABLE_ENTRIES_ENDPOINT, $tableName, null, $options);
0 ignored issues
show
Documentation introduced by
$tableName is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug Compatibility introduced by
The expression $this->performRequest('G...eName, null, $options); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\Entry to the return on line 62 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getEntries of type Directus\SDK\Response\EntryCollection.
Loading history...
63 2
    }
64
65 2
    /**
66
     * @inheritdoc
67
     */
68 2
    public function getEntry($tableName, $id, array $options = [])
69
    {
70 2
        return $this->performRequest('GET', static::TABLE_ENTRY_ENDPOINT, [$tableName, $id]);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('G...rray($tableName, $id)); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 70 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getEntry of type Directus\SDK\Response\Entry.
Loading history...
71
    }
72
73 2
    /**
74
     * @inheritdoc
75 2
     */
76
    public function getUsers(array $params = [])
77
    {
78 2
        return $this->getEntries('directus_users', $params);
79
    }
80 2
81
    /**
82
     * @inheritdoc
83 2
     */
84
    public function getUser($id, array $params = [])
85 2
    {
86
        return $this->getEntry($id, 'directus_users', $params);
87
    }
88 2
89
    /**
90 2
     * @inheritdoc
91
     */
92
    public function getGroups()
93 2
    {
94
        return $this->performRequest('GET', static::GROUP_LIST_ENDPOINT);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('G...::GROUP_LIST_ENDPOINT); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\Entry to the return on line 94 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getGroups of type Directus\SDK\Response\EntryCollection.
Loading history...
95 2
    }
96
97
    /**
98 2
     * @inheritdoc
99
     */
100 2
    public function getGroup($groupID)
101
    {
102
        return $this->performRequest('GET', static::GROUP_INFORMATION_ENDPOINT, $groupID);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('G...ON_ENDPOINT, $groupID); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 102 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getGroup of type Directus\SDK\Response\Entry.
Loading history...
103 2
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108
    public function getGroupPrivileges($groupID)
109
    {
110
        return $this->performRequest('GET', static::GROUP_PRIVILEGES_ENDPOINT, $groupID);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('G...ES_ENDPOINT, $groupID); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\Entry to the return on line 110 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getGroupPrivileges of type Directus\SDK\Response\EntryCollection.
Loading history...
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116
    public function getFiles()
117
    {
118
        return $this->performRequest('GET', static::FILE_LIST_ENDPOINT);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('G...c::FILE_LIST_ENDPOINT); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\Entry to the return on line 118 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getFiles of type Directus\SDK\Response\EntryCollection.
Loading history...
119
    }
120
121
    /**
122
     * @inheritdoc
123
     */
124
    public function getFile($fileID)
125
    {
126
        return $this->performRequest('GET', static::FILE_INFORMATION_ENDPOINT, $fileID);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('G...ION_ENDPOINT, $fileID); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 126 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getFile of type Directus\SDK\Response\Entry.
Loading history...
127
    }
128
129
    /**
130
     * @inheritdoc
131
     */
132
    public function getSettings()
133
    {
134
        return $this->performRequest('GET', static::SETTING_LIST_ENDPOINT);
135
    }
136
137
    /**
138
     * @inheritdoc
139
     */
140
    public function getSettingsByCollection($collectionName)
141
    {
142
        return $this->performRequest('GET', static::SETTING_COLLECTION_ENDPOINT, $collectionName);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('G...OINT, $collectionName); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\Entry to the return on line 142 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInt...getSettingsByCollection of type Directus\SDK\Response\EntryCollection.
Loading history...
143
    }
144
145
    /**
146
     * @inheritdoc
147
     */
148
    public function getMessages($userId)
149
    {
150
        return $this->performRequest('GET', static::MESSAGES_USER_ENDPOINT, $userId);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('G...SER_ENDPOINT, $userId); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\Entry to the return on line 150 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getMessages of type Directus\SDK\Response\EntryCollection.
Loading history...
151
    }
152
153
    /**
154
     * @inheritdoc
155
     */
156
    public function createEntry($tableName, array $data)
157
    {
158
        return $this->performRequest('POST', static::TABLE_ENTRY_CREATE_ENDPOINT, $tableName, $data);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->performRequest('P...NT, $tableName, $data); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 158 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createEntry of type Directus\SDK\Response\Entry.
Loading history...
159
    }
160
161
    /**
162
     * @inheritdoc
163
     */
164
    public function updateEntry($tableName, $id, array $data)
165
    {
166
        return $this->performRequest('PUT', static::TABLE_ENTRY_UPDATE_ENDPOINT, [$tableName, $id], $data);
167
    }
168
169
    /**
170
     * @inheritdoc
171
     */
172
    public function deleteEntry($tableName, $ids)
173
    {
174
        return $this->performRequest('DELETE', static::TABLE_ENTRY_DELETE_ENDPOINT, [$tableName, $ids]);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->performReq...ray($tableName, $ids)); (Directus\SDK\Response\En...ctus\SDK\Response\Entry) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::deleteEntry of type integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
175
    }
176
177
    /**
178
     * @inheritdoc
179
     */
180 View Code Duplication
    public function createUser(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...
181
    {
182
        // @TODO: Add hooks
183
        if (ArrayUtils::has($data, 'password')) {
184
            // @NOTE: Use Directus password hash
185
            $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT, ['cost' => 12]);
186
        }
187
188
        return $this->createEntry('directus_users', $data);
189
    }
190
191
    /**
192
     * @inheritdoc
193
     */
194 View Code Duplication
    public function updateUser($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...
195
    {
196
        // @TODO: Add hooks
197
        if (ArrayUtils::has($data, 'password')) {
198
            // @NOTE: Use Directus password hash
199
            $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT, ['cost' => 12]);
200
        }
201
202
        return $this->updateEntry('directus_users', $id, $data);
203
    }
204
205
    /**
206
     * @inheritdoc
207
     */
208
    public function deleteUser($ids)
209
    {
210
        return $this->deleteEntry('directus_users', $ids);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->deleteEntr...directus_users', $ids); (Directus\SDK\Response\En...ctus\SDK\Response\Entry) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::deleteUser of type integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
211
    }
212
213
    /**
214
     * @inheritdoc
215
     */
216
    public function createFile(array $data)
217
    {
218
        $attributes = [];
219
        if (ArrayUtils::has($data, 'file')) {
220
            $path = $data['file'];
221
            $ext = pathinfo($path, PATHINFO_EXTENSION);
222
            $mimeType = mime_content_type($path);
223
            $attributes['name'] = pathinfo($path, PATHINFO_FILENAME) . '.' . $ext;
224
            $attributes['type'] = $mimeType;
225
            $content = file_get_contents($path);
226
            $base64 = 'data:' . $mimeType . ';base64,' . base64_encode($content);
227
            $attributes['data'] = $base64;
228
            unset($data['file']);
229
        } else if (ArrayUtils::has($data, 'data')) {
230
            $finfo = new \finfo(FILEINFO_MIME);
231
            list($mimeType, $charset) = explode('; charset=', $finfo->buffer($data['data']));
0 ignored issues
show
Unused Code introduced by
The assignment to $charset is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
232
            $base64 = 'data:' . $mimeType . ';base64,' . base64_encode($data['data']);
233
            list($type, $subtype) = explode('/', $mimeType);
0 ignored issues
show
Unused Code introduced by
The assignment to $type is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
234
            $attributes['data'] = $base64;
235
            $attributes['type'] = $mimeType;
236
            $attributes['name'] = StringUtils::randomString() . '.' . $subtype;
237
            unset($data['data']);
238
        } else {
239
            throw new \Exception('Missing "file" or "data" attribute.');
240
        }
241
242
        $data = array_merge($data, $attributes);
243
244
        return $this->performRequest('POST', static::FILE_CREATE_ENDPOINT, null, $data);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug Compatibility introduced by
The expression $this->performRequest('P...ENDPOINT, null, $data); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 244 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createFile of type Directus\SDK\Response\Entry.
Loading history...
245
    }
246
247
    /**
248
     * @inheritdoc
249
     */
250
    public function updateFile($id, array $data)
251
    {
252
        return $this->updateEntry('directus_files', $id, $data);
253
    }
254
255
    /**
256
     * @inheritdoc
257
     */
258
    public function deleteFile($ids)
259
    {
260
        return $this->deleteEntry('directus_files', $ids);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->deleteEntr...directus_files', $ids); (Directus\SDK\Response\En...ctus\SDK\Response\Entry) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::deleteFile of type integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
261
    }
262
}
263