Completed
Push — d64 ( 4f8de5...da115c )
by Welling
02:13
created

ClientLocal::getBookmark()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
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
13
use Directus\Database\Connection;
14
use Directus\Database\TableGateway\BaseTableGateway;
15
use Directus\Database\TableGateway\DirectusActivityTableGateway;
16
use Directus\Database\TableGateway\DirectusMessagesTableGateway;
17
use Directus\Database\TableGateway\DirectusPreferencesTableGateway;
18
use Directus\Database\TableGateway\DirectusPrivilegesTableGateway;
19
use Directus\Database\TableGateway\DirectusUiTableGateway;
20
use Directus\Database\TableGateway\DirectusUsersTableGateway;
21
use Directus\Database\TableGateway\RelationalTableGateway;
22
use Directus\Database\TableSchema;
23
use Directus\Util\ArrayUtils;
24
use Directus\Util\SchemaUtils;
25
26
/**
27
 * Client Local
28
 *
29
 * Client to Interact with the database directly using Directus Database Component
30
 *
31
 * @author Welling Guzmán <[email protected]>
32
 */
33
class ClientLocal extends AbstractClient
34
{
35
    /**
36
     * @var BaseTableGateway[]
37
     */
38
    protected $tableGateways = [];
39
40
    /**
41
     * @var Connection
42
     */
43
    protected $connection = null;
44
45
    /**
46
     * ClientLocal constructor.
47
     *
48
     * @param $connection
49
     */
50
    public function __construct($connection)
51
    {
52
        $this->connection = $connection;
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function getTables(array $params = [])
59
    {
60
        return $this->createResponseFromData(TableSchema::getTablesSchema($params));
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->createResponseFro...TablesSchema($params)); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\Entry to the return on line 60 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getTables of type Directus\SDK\Response\EntryCollection.
Loading history...
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66
    public function getTable($tableName)
67
    {
68
        return $this->createResponseFromData(TableSchema::getSchemaArray($tableName));
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->createResponseFro...hemaArray($tableName)); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 68 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getTable of type Directus\SDK\Response\Entry.
Loading history...
69
    }
70
71
    /**
72
     * @inheritDoc
73
     */
74
    public function getColumns($tableName, array $params = [])
75
    {
76
        return $this->createResponseFromData(TableSchema::getColumnSchemaArray($tableName, $params));
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->createResponseFro...($tableName, $params)); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\Entry to the return on line 76 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getColumns of type Directus\SDK\Response\EntryCollection.
Loading history...
77
    }
78
79
    /**
80
     * @inheritDoc
81
     */
82
    public function getColumn($tableName, $columnName)
83
    {
84
        return $this->createResponseFromData(TableSchema::getColumnSchema($tableName, $columnName)->toArray());
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->createResponseFro...olumnName)->toArray()); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 84 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getColumn of type Directus\SDK\Response\Entry.
Loading history...
85
    }
86
87
    /**
88
     * @inheritDoc
89
     */
90
    public function getItems($tableName, array $params = [])
91
    {
92
        $tableGateway = $this->getTableGateway($tableName);
93
94
        return $this->createResponseFromData($tableGateway->getItems($params));
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->createResponseFro...ay->getItems($params)); 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::getItems of type Directus\SDK\Response\EntryCollection.
Loading history...
95
    }
96
97
    /**
98
     * @inheritDoc
99
     */
100
    public function getItem($tableName, $id, array $params = [])
101
    {
102
        // @TODO: Dynamic ID
103
        return $this->getItems($tableName, array_merge($params, [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getItems($..., array('id' => $id))); (Directus\SDK\Response\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getItem of type Directus\SDK\Response\Entry.

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...
104
            'id' => $id
105
        ]));
106
    }
107
108
    /**
109
     * @inheritDoc
110
     */
111
    public function getUsers(array $params = [])
112
    {
113
        // @TODO: store the directus tables somewhere (SchemaManager?)
114
        return $this->getItems('directus_users', $params);
115
    }
116
117
    /**
118
     * @inheritDoc
119
     */
120
    public function getUser($id, array $params = [])
121
    {
122
        return $this->getItem('directus_users', $id, $params);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getItem('d..._users', $id, $params); (Directus\SDK\Response\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getUser of type Directus\SDK\Response\Entry.

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...
123
    }
124
125
    /**
126
     * @inheritDoc
127
     */
128
    public function getGroups(array $params = [])
129
    {
130
        return $this->getItems('directus_groups', $params);
131
    }
132
133
    /**
134
     * @inheritDoc
135
     */
136
    public function getGroup($id, array $params = [])
137
    {
138
        return $this->getItem('directus_groups', $id, $params);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getItem('d...groups', $id, $params); (Directus\SDK\Response\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getGroup of type Directus\SDK\Response\Entry.

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...
139
    }
140
141
    /**
142
     * @inheritDoc
143
     */
144
    public function getGroupPrivileges($groupID)
145
    {
146
        $this->getItems('directus_privileges', [
147
            'filter' => [
148
                'group_id' => ['eq' => $groupID]
149
            ]
150
        ]);
151
    }
152
153
    /**
154
     * @inheritDoc
155
     */
156
    public function getFiles(array $params = [])
157
    {
158
        return $this->getItems('directus_files', $params);
159
    }
160
161
    /**
162
     * @inheritDoc
163
     */
164
    public function getFile($id, array $params = [])
165
    {
166
        return $this->getItem('directus_files', $id, $params);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getItem('d..._files', $id, $params); (Directus\SDK\Response\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getFile of type Directus\SDK\Response\Entry.

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...
167
    }
168
169
    /**
170
     * @inheritDoc
171
     */
172
    public function getSettings()
173
    {
174
        return $this->getItems('directus_settings');
175
    }
176
177
    /**
178
     * @inheritDoc
179
     */
180
    public function getSettingsByCollection($collectionName)
181
    {
182
        return $this->getItems('directus_settings', [
183
            'filter' => [
184
                'collection' => ['eq' => $collectionName]
185
            ]
186
        ]);
187
    }
188
189
    /**
190
     * @inheritDoc
191
     */
192
    public function getMessages($userId)
193
    {
194
        $connection = $this->container->get('connection');
195
        $acl = $this->container->get('acl');
196
        $messagesTableGateway = new DirectusMessagesTableGateway($connection, $acl);
197
        $result = $messagesTableGateway->fetchMessagesInboxWithHeaders($userId);
198
199
        return $this->createResponseFromData($result);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->createResponseFromData($result); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\Entry to the return on line 199 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getMessages of type Directus\SDK\Response\EntryCollection.
Loading history...
200
    }
201
202
    /**
203
     * @inheritDoc
204
     */
205 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...
206
    {
207
        $tableGateway = $this->getTableGateway($tableName);
208
        $data = $this->processData($tableName, $data);
209
210
        foreach($data as $key => $value) {
211
            if ($value instanceof File) {
212
                $data[$key] = $this->processFile($value);
213
            }
214
        }
215
216
        $newRecord = $tableGateway->manageRecordUpdate($tableName, $data);
217
218
        return $this->getItem($tableName, $newRecord[$tableGateway->primaryKeyFieldName]);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getItem($t...>primaryKeyFieldName]); (Directus\SDK\Response\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createItem of type Directus\SDK\Response\Entry.

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...
219
    }
220
221
    /**
222
     * @inheritDoc
223
     */
224 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...
225
    {
226
        $tableGateway = $this->getTableGateway($tableName);
227
        $data = $this->processData($tableName, $data);
228
229
        foreach($data as $key => $value) {
230
            if ($value instanceof File) {
231
                $data[$key] = $this->processFile($value);
232
            }
233
        }
234
235
        $updatedRecord = $tableGateway->manageRecordUpdate($tableName, array_merge($data, ['id' => $id]));
236
237
        return $this->getItem($tableName, $updatedRecord[$tableGateway->primaryKeyFieldName]);
238
    }
239
240
    /**
241
     * @inheritDoc
242
     */
243
    public function deleteItem($tableName, $ids)
244
    {
245
        // @TODO: Accept EntryCollection and Entry
246
        $tableGateway = $this->getTableGateway($tableName);
247
248
        if (!is_array($ids)) {
249
            $ids = [$ids];
250
        }
251
252
        return $tableGateway->delete(function($delete) use ($ids) {
253
            return $delete->where->in('id', $ids);
254
        });
255
    }
256
257
    /**
258
     * @inheritDoc
259
     */
260
    public function createUser(array $data)
261
    {
262
        return $this->createItem('directus_users', $data);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->createItem...irectus_users', $data); (Directus\SDK\Response\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createUser of type Directus\SDK\Response\Entry.

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...
263
    }
264
265
    /**
266
     * @inheritDoc
267
     */
268
    public function updateUser($id, array $data)
269
    {
270
        return $this->updateItem('directus_users', $id, $data);
271
    }
272
273
    /**
274
     * @inheritDoc
275
     */
276
    public function deleteUser($ids)
277
    {
278
        return $this->deleteItem('directus_users', $ids);
279
    }
280
281
    /**
282
     * @inheritDoc
283
     */
284
    public function createFile(File $file)
285
    {
286
        $data = $this->processFile($file);
287
288
        return $this->createItem('directus_files', $data);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->createItem...irectus_files', $data); (Directus\SDK\Response\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createFile of type Directus\SDK\Response\Entry.

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...
289
    }
290
291
    /**
292
     * @inheritDoc
293
     */
294
    public function updateFile($id, $data)
295
    {
296
        if ($data instanceof File) {
297
            $data = $this->processFile($data);
298
        }
299
300
        return $this->updateItem('directus_files', $id, $data);
301
    }
302
303
    /**
304
     * @inheritDoc
305
     */
306
    public function deleteFile($ids)
307
    {
308
        return $this->deleteItem('directus_files', $ids);
309
    }
310
311
    public function createPreferences($data)
312
    {
313
        if (!ArrayUtils::contains($data, ['title', 'table_name'])) {
314
            throw new \Exception('title and table_name are required');
315
        }
316
317
        $acl = $this->container->get('acl');
318
        $data['user'] = $acl->getUserId();
319
320
        return $this->createItem('directus_preferences', $data);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->createItem...s_preferences', $data); (Directus\SDK\Response\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createPreferences of type Directus\SDK\Response\Entry.

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...
321
    }
322
323
    /**
324
     * @inheritdoc
325
     */
326
    public function createBookmark($data)
327
    {
328
        $acl = $this->container->get('acl');
329
        $data['user'] = $acl->getUserId();
330
331
        $preferences = $this->createPreferences(ArrayUtils::pick($data, [
332
            'title', 'table_name', 'sort', 'status', 'search_string', 'sort_order', 'columns_visible', 'user'
333
        ]));
334
335
        $title = $preferences->title;
0 ignored issues
show
Bug introduced by
The property title does not seem to exist in Directus\SDK\Response\EntryCollection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
336
        $tableName = $preferences->table_name;
0 ignored issues
show
Bug introduced by
The property table_name does not seem to exist in Directus\SDK\Response\EntryCollection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
337
        $bookmarkData = [
338
            'section' => 'search',
339
            'title' => $title,
340
            'url' => 'tables/' . $tableName . '/pref/' . $title,
341
            'user' => $data['user']
342
        ];
343
344
        return $this->createItem('directus_bookmarks', $bookmarkData);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->createItem...marks', $bookmarkData); (Directus\SDK\Response\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createBookmark of type Directus\SDK\Response\Entry.

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...
345
    }
346
347
    /**
348
     * @inheritdoc
349
     */
350
    public function getBookmark($id)
351
    {
352
        return $this->getItem('directus_bookmarks', $id);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getItem('directus_bookmarks', $id); (Directus\SDK\Response\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getBookmark of type Directus\SDK\Response\Entry.

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...
353
    }
354
355
    /**
356
     * @inheritdoc
357
     */
358
    public function getBookmarks($userId = null)
359
    {
360
        $filters = [];
361
        if ($userId !== null) {
362
            $filters = [
363
                'filters' => ['user' => ['eq' => $userId]]
364
            ];
365
        }
366
367
        return $this->getItems('directus_bookmarks', $filters);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getItems('..._bookmarks', $filters); (Directus\SDK\Response\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getBookmarks of type Directus\SDK\Response\Entry.

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...
368
    }
369
370
    /**
371
     * @inheritdoc
372
     */
373
    public function createColumn($data)
374
    {
375
        $data = $this->parseColumnData($data);
376
377
        $tableGateway = $this->getTableGateway($data['table_name']);
378
379
        $tableGateway->addColumn($data['table_name'], ArrayUtils::omit($data, ['table_name']));
380
381
        return $this->getColumn($data['table_name'], $data['column_name']);
382
    }
383
384
    /**
385
     * @inheritdoc
386
     */
387
    public function createGroup(array $data)
388
    {
389
        return $this->createItem('directus_groups', $data);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->createItem...rectus_groups', $data); (Directus\SDK\Response\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createGroup of type Directus\SDK\Response\Entry.

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...
390
    }
391
392
    /**
393
     * @inheritdoc
394
     */
395
    public function createMessage(array $data)
396
    {
397
        $this->requiredAttributes(['from', 'message', 'subject'], $data);
398
        $this->requiredOneAttribute(['to', 'toGroup'], $data);
399
400
        $recipients = $this->getMessagesTo($data);
401
        $recipients = explode(',', $recipients);
402
        ArrayUtils::remove($data, ['to', 'toGroup']);
403
404
        $groupRecipients = [];
405
        $userRecipients = [];
406
        foreach ($recipients as $recipient) {
407
            $typeAndId = explode('_', $recipient);
408
            if ($typeAndId[0] == 0) {
409
                $userRecipients[] = $typeAndId[1];
410
            } else {
411
                $groupRecipients[] = $typeAndId[1];
412
            }
413
        }
414
415
        $ZendDb = $this->container->get('connection');
416
        $acl = $this->container->get('acl');
417
        if (count($groupRecipients) > 0) {
418
            $usersTableGateway = new DirectusUsersTableGateway($ZendDb, $acl);
419
            $result = $usersTableGateway->findActiveUserIdsByGroupIds($groupRecipients);
420
            foreach ($result as $item) {
421
                $userRecipients[] = $item['id'];
422
            }
423
        }
424
425
        $userRecipients[] = $acl->getUserId();
426
427
        $messagesTableGateway = new DirectusMessagesTableGateway($ZendDb, $acl);
428
        $id = $messagesTableGateway->sendMessage($data, array_unique($userRecipients), $acl->getUserId());
429
430
        if ($id) {
431
            $Activity = new DirectusActivityTableGateway($ZendDb, $acl);
432
            $data['id'] = $id;
433
            $Activity->recordMessage($data, $acl->getUserId());
434
        }
435
436
        $message = $messagesTableGateway->fetchMessageWithRecipients($id, $acl->getUserId());
437
        $response = [
438
            'meta' => [
439
                'type' => 'item',
440
                'table' => 'directus_messages'
441
            ],
442
            'data' => $message
443
        ];
444
445
        return $this->createResponseFromData($response);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->createResponseFromData($response); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 445 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createMessage of type Directus\SDK\Response\Entry.
Loading history...
446
    }
447
448
    /**
449
     * @inheritdoc
450
     */
451
    public function sendMessage(array $data)
452
    {
453
        return $this->createMessage($data);
454
    }
455
456 View Code Duplication
    public function createPrivileges(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...
457
    {
458
        $connection = $this->container->get('connection');
459
        $acl = $this->container->get('acl');
460
        $privileges = new DirectusPrivilegesTableGateway($connection, $acl);
461
462
        $response = [
463
            'meta' => [
464
                'type' => 'item',
465
                'table' => 'directus_privileges'
466
            ],
467
            'data' => $privileges->insertPrivilege($data)
468
        ];
469
470
        return $this->createResponseFromData($response);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->createResponseFromData($response); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 470 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createPrivileges of type Directus\SDK\Response\Entry.
Loading history...
471
    }
472
473
    public function createTable($name, array $data = [])
474
    {
475
        $isTableNameAlphanumeric = preg_match("/[a-z0-9]+/i", $name);
476
        $zeroOrMoreUnderscoresDashes = preg_match("/[_-]*/i", $name);
477
478
        if (!($isTableNameAlphanumeric && $zeroOrMoreUnderscoresDashes)) {
479
            return $this->createResponseFromData(['error' => ['message' => 'invalid_table_name']]);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->createResponseFro...invalid_table_name'))); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 479 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createTable of type Directus\SDK\Response\Entry.
Loading history...
480
        }
481
482
        $schema = $this->container->get('schemaManager');
483
        $emitter = $this->container->get('emitter');
484
        if (!$schema->tableExists($name)) {
485
            $emitter->run('table.create:before', $name);
486
            // Through API:
487
            // Remove spaces and symbols from table name
488
            // And in lowercase
489
            $name = SchemaUtils::cleanTableName($name);
490
            $schema->createTable($name);
491
            $emitter->run('table.create', $name);
492
            $emitter->run('table.create:after', $name);
493
        }
494
495
        $connection = $this->container->get('connection');
496
        $acl = $this->container->get('acl');
497
        $privileges = new DirectusPrivilegesTableGateway($connection, $acl);
498
499
        return $this->createResponseFromData($privileges->insertPrivilege([
1 ignored issue
show
Bug Compatibility introduced by
The expression $this->createResponseFro...able_name' => $name))); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 499 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createTable of type Directus\SDK\Response\Entry.
Loading history...
500
            'group_id' => 1,
501
            'table_name' => $name
502
        ]));
503
    }
504
505
    /**
506
     * @inheritdoc
507
     */
508
    public function createColumnUIOptions(array $data)
509
    {
510
        $this->requiredAttributes(['table', 'column', 'ui', 'options'], $data);
511
        $tableGateway = $this->getTableGateway('directus_ui');
512
513
        $table = $data['table'];
514
        $column = $data['column'];
515
        $ui = $data['ui'];
516
517
        $data = $data['options'];
518
        $keys = ['table_name' => $table, 'column_name' => $column, 'ui_name' => $ui];
519
        $uis = to_name_value($data, $keys);
520
521
        $column_settings = [];
522
        foreach ($uis as $col) {
523
            $existing = $tableGateway->select(['table_name' => $table, 'column_name' => $column, 'ui_name' => $ui, 'name' => $col['name']])->toArray();
524
            if (count($existing) > 0) {
525
                $col['id'] = $existing[0]['id'];
526
            }
527
            array_push($column_settings, $col);
528
        }
529
        $tableGateway->updateCollection($column_settings);
530
531
        $connection = $this->container->get('connection');
532
        $acl = $this->container->get('acl');
533
        $UiOptions = new DirectusUiTableGateway($connection, $acl);
534
        $response = $UiOptions->fetchOptions($table, $column, $ui);
535
536
        if (!$response) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $response of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
537
            $response = [
538
                'error' => [
539
                    'message' => sprintf('unable_to_find_column_%s_options_for_%s', ['column' => $column, 'ui' => $ui])
540
                ],
541
                'success' => false
542
            ];
543
        } else {
544
            $response = [
545
                'meta' => [
546
                    'type' => 'item',
547
                    'table' => 'directus_ui'
548
                ],
549
                'data' => $response
550
            ];
551
        }
552
553
        return $this->createResponseFromData($response);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->createResponseFromData($response); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 553 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInt...::createColumnUIOptions of type Directus\SDK\Response\Entry.
Loading history...
554
    }
555
556 View Code Duplication
    public function getPreferences($table, $user)
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...
557
    {
558
        $acl = $this->container->get('acl');
559
        $connection = $this->container->get('connection');
560
        $preferencesTableGateway = new DirectusPreferencesTableGateway($connection, $acl);
561
562
        $response = [
563
            'meta' => [
564
                'type' => 'item',
565
                'table' => 'directus_preferences'
566
            ],
567
            'data' => $preferencesTableGateway->fetchByUserAndTableAndTitle($user, $table)
568
        ];
569
570
        return $this->createResponseFromData($response);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->createResponseFromData($response); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 570 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getPreferences of type Directus\SDK\Response\Entry.
Loading history...
571
    }
572
573
    /**
574
     * @inheritdoc
575
     */
576
    public function deleteBookmark($id)
577
    {
578
        return $this->deleteItem('directus_bookmarks', $id);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->deleteItem...ectus_bookmarks', $id); (integer) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::deleteBookmark of type Directus\SDK\Response\Entry.

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...
579
    }
580
581
    /**
582
     * @inheritdoc
583
     */
584 View Code Duplication
    public function deleteColumn($name, $table)
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...
585
    {
586
        $tableGateway = $this->getTableGateway($table);
587
        $success = $tableGateway->dropColumn($name);
588
589
        $response = [
590
            'success' => (bool) $success
591
        ];
592
593
        if (!$success) {
594
            $response['error'] = [
595
                'message' => sprintf('unable_to_remove_column_%s', ['column_name' => $name])
596
            ];
597
        }
598
599
        return $this->createResponseFromData($response);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->createResponseFromData($response); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 599 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::deleteColumn of type Directus\SDK\Response\Entry.
Loading history...
600
    }
601
602
    /**
603
     * @inheritdoc
604
     */
605
    public function deleteGroup($id)
606
    {
607
        return $this->deleteItem('directus_groups', $id);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->deleteItem('directus_groups', $id); (integer) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::deleteGroup of type Directus\SDK\Response\Entry.

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...
608
    }
609
610
    /**
611
     * @inheritdoc
612
     */
613 View Code Duplication
    public function deleteTable($name)
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...
614
    {
615
        $tableGateway = $this->getTableGateway($name);
616
        $success = $tableGateway->drop();
617
618
        $response = [
619
            'success' => (bool) $success
620
        ];
621
622
        if (!$success) {
623
            $response['error'] = [
624
                'message' => sprintf('unable_to_remove_table_%s', ['table_name' => $name])
625
            ];
626
        }
627
628
        return $this->createResponseFromData($response);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->createResponseFromData($response); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 628 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::deleteTable of type Directus\SDK\Response\Entry.
Loading history...
629
    }
630
631
    /**
632
     * Get a table gateway for the given table name
633
     *
634
     * @param $tableName
635
     *
636
     * @return RelationalTableGateway
637
     */
638
    protected function getTableGateway($tableName)
639
    {
640
        if (!array_key_exists($tableName, $this->tableGateways)) {
641
            $acl = TableSchema::getAclInstance();
642
            $this->tableGateways[$tableName] = new RelationalTableGateway($tableName, $this->connection, $acl);
643
        }
644
645
        return $this->tableGateways[$tableName];
646
    }
647
}
648