Completed
Push — d64 ( 2002c2...cb45b2 )
by Welling
02:09
created

ClientLocal::getEntries()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
ccs 0
cts 5
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\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->getEntries($params));
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->createResponseFro...->getEntries($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 createColumn($data)
351
    {
352
        $data = $this->parseColumnData($data);
353
354
        $tableGateway = $this->getTableGateway($data['table_name']);
355
356
        $tableGateway->addColumn($data['table_name'], ArrayUtils::omit($data, ['table_name']));
357
358
        return $this->getColumn($data['table_name'], $data['column_name']);
359
    }
360
361
    /**
362
     * @inheritdoc
363
     */
364
    public function createGroup(array $data)
365
    {
366
        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...
367
    }
368
369
    /**
370
     * @inheritdoc
371
     */
372
    public function createMessage(array $data)
373
    {
374
        $this->requiredAttributes(['from', 'message', 'subject'], $data);
375
        $this->requiredOneAttribute(['to', 'toGroup'], $data);
376
377
        $recipients = $this->getMessagesTo($data);
378
        $recipients = explode(',', $recipients);
379
        ArrayUtils::remove($data, ['to', 'toGroup']);
380
381
        $groupRecipients = [];
382
        $userRecipients = [];
383
        foreach ($recipients as $recipient) {
384
            $typeAndId = explode('_', $recipient);
385
            if ($typeAndId[0] == 0) {
386
                $userRecipients[] = $typeAndId[1];
387
            } else {
388
                $groupRecipients[] = $typeAndId[1];
389
            }
390
        }
391
392
        $ZendDb = $this->container->get('connection');
393
        $acl = $this->container->get('acl');
394
        if (count($groupRecipients) > 0) {
395
            $usersTableGateway = new DirectusUsersTableGateway($ZendDb, $acl);
396
            $result = $usersTableGateway->findActiveUserIdsByGroupIds($groupRecipients);
397
            foreach ($result as $item) {
398
                $userRecipients[] = $item['id'];
399
            }
400
        }
401
402
        $userRecipients[] = $acl->getUserId();
403
404
        $messagesTableGateway = new DirectusMessagesTableGateway($ZendDb, $acl);
405
        $id = $messagesTableGateway->sendMessage($data, array_unique($userRecipients), $acl->getUserId());
406
407
        if ($id) {
408
            $Activity = new DirectusActivityTableGateway($ZendDb, $acl);
409
            $data['id'] = $id;
410
            $Activity->recordMessage($data, $acl->getUserId());
411
        }
412
413
        $message = $messagesTableGateway->fetchMessageWithRecipients($id, $acl->getUserId());
414
        $response = [
415
            'meta' => [
416
                'type' => 'item',
417
                'table' => 'directus_messages'
418
            ],
419
            'data' => $message
420
        ];
421
422
        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 422 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createMessage of type Directus\SDK\Response\Entry.
Loading history...
423
    }
424
425
    /**
426
     * @inheritdoc
427
     */
428
    public function sendMessage(array $data)
429
    {
430
        return $this->createMessage($data);
431
    }
432
433 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...
434
    {
435
        $connection = $this->container->get('connection');
436
        $acl = $this->container->get('acl');
437
        $privileges = new DirectusPrivilegesTableGateway($connection, $acl);
438
439
        $response = [
440
            'meta' => [
441
                'type' => 'item',
442
                'table' => 'directus_privileges'
443
            ],
444
            'data' => $privileges->insertPrivilege($data)
445
        ];
446
447
        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 447 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createPrivileges of type Directus\SDK\Response\Entry.
Loading history...
448
    }
449
450
    public function createTable($name, array $data = [])
451
    {
452
        $isTableNameAlphanumeric = preg_match("/[a-z0-9]+/i", $name);
453
        $zeroOrMoreUnderscoresDashes = preg_match("/[_-]*/i", $name);
454
455
        if (!($isTableNameAlphanumeric && $zeroOrMoreUnderscoresDashes)) {
456
            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 456 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createTable of type Directus\SDK\Response\Entry.
Loading history...
457
        }
458
459
        $schema = $this->container->get('schemaManager');
460
        $emitter = $this->container->get('emitter');
461
        if (!$schema->tableExists($name)) {
462
            $emitter->run('table.create:before', $name);
463
            // Through API:
464
            // Remove spaces and symbols from table name
465
            // And in lowercase
466
            $name = SchemaUtils::cleanTableName($name);
467
            $schema->createTable($name);
468
            $emitter->run('table.create', $name);
469
            $emitter->run('table.create:after', $name);
470
        }
471
472
        $connection = $this->container->get('connection');
473
        $acl = $this->container->get('acl');
474
        $privileges = new DirectusPrivilegesTableGateway($connection, $acl);
475
476
        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 476 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createTable of type Directus\SDK\Response\Entry.
Loading history...
477
            'group_id' => 1,
478
            'table_name' => $name
479
        ]));
480
    }
481
482
    /**
483
     * @inheritdoc
484
     */
485
    public function createColumnUIOptions(array $data)
486
    {
487
        $this->requiredAttributes(['table', 'column', 'ui', 'options'], $data);
488
        $tableGateway = $this->getTableGateway('directus_ui');
489
490
        $table = $data['table'];
491
        $column = $data['column'];
492
        $ui = $data['ui'];
493
494
        $data = $data['options'];
495
        $keys = ['table_name' => $table, 'column_name' => $column, 'ui_name' => $ui];
496
        $uis = to_name_value($data, $keys);
497
498
        $column_settings = [];
499
        foreach ($uis as $col) {
500
            $existing = $tableGateway->select(['table_name' => $table, 'column_name' => $column, 'ui_name' => $ui, 'name' => $col['name']])->toArray();
501
            if (count($existing) > 0) {
502
                $col['id'] = $existing[0]['id'];
503
            }
504
            array_push($column_settings, $col);
505
        }
506
        $tableGateway->updateCollection($column_settings);
507
508
        $connection = $this->container->get('connection');
509
        $acl = $this->container->get('acl');
510
        $UiOptions = new DirectusUiTableGateway($connection, $acl);
511
        $response = $UiOptions->fetchOptions($table, $column, $ui);
512
513
        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...
514
            $response = [
515
                'error' => [
516
                    'message' => sprintf('unable_to_find_column_%s_options_for_%s', ['column' => $column, 'ui' => $ui])
517
                ],
518
                'success' => false
519
            ];
520
        } else {
521
            $response = [
522
                'meta' => [
523
                    'type' => 'item',
524
                    'table' => 'directus_ui'
525
                ],
526
                'data' => $response
527
            ];
528
        }
529
530
        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 530 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInt...::createColumnUIOptions of type Directus\SDK\Response\Entry.
Loading history...
531
    }
532
533 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...
534
    {
535
        $acl = $this->container->get('acl');
536
        $connection = $this->container->get('connection');
537
        $preferencesTableGateway = new DirectusPreferencesTableGateway($connection, $acl);
538
539
        $response = [
540
            'meta' => [
541
                'type' => 'item',
542
                'table' => 'directus_preferences'
543
            ],
544
            'data' => $preferencesTableGateway->fetchByUserAndTableAndTitle($user, $table)
545
        ];
546
547
        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 547 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getPreferences of type Directus\SDK\Response\Entry.
Loading history...
548
    }
549
550
    /**
551
     * @inheritdoc
552
     */
553
    public function deleteBookmark($id)
554
    {
555
        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...
556
    }
557
558
    /**
559
     * @inheritdoc
560
     */
561 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...
562
    {
563
        $tableGateway = $this->getTableGateway($table);
564
        $success = $tableGateway->dropColumn($name);
565
566
        $response = [
567
            'success' => (bool) $success
568
        ];
569
570
        if (!$success) {
571
            $response['error'] = [
572
                'message' => sprintf('unable_to_remove_column_%s', ['column_name' => $name])
573
            ];
574
        }
575
576
        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 576 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::deleteColumn of type Directus\SDK\Response\Entry.
Loading history...
577
    }
578
579
    /**
580
     * @inheritdoc
581
     */
582
    public function deleteGroup($id)
583
    {
584
        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...
585
    }
586
587
    /**
588
     * @inheritdoc
589
     */
590 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...
591
    {
592
        $tableGateway = $this->getTableGateway($name);
593
        $success = $tableGateway->drop();
594
595
        $response = [
596
            'success' => (bool) $success
597
        ];
598
599
        if (!$success) {
600
            $response['error'] = [
601
                'message' => sprintf('unable_to_remove_table_%s', ['table_name' => $name])
602
            ];
603
        }
604
605
        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 605 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::deleteTable of type Directus\SDK\Response\Entry.
Loading history...
606
    }
607
608
    /**
609
     * Get a table gateway for the given table name
610
     *
611
     * @param $tableName
612
     *
613
     * @return RelationalTableGateway
614
     */
615
    protected function getTableGateway($tableName)
616
    {
617
        if (!array_key_exists($tableName, $this->tableGateways)) {
618
            $acl = TableSchema::getAclInstance();
619
            $this->tableGateways[$tableName] = new RelationalTableGateway($tableName, $this->connection, $acl);
620
        }
621
622
        return $this->tableGateways[$tableName];
623
    }
624
}
625