Completed
Push — d64 ( 2997b8...ca4f4f )
by Welling
02:50
created

ClientLocal::createPrivileges()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 16
ccs 0
cts 14
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\DirectusPrivilegesTableGateway;
18
use Directus\Database\TableGateway\DirectusUsersTableGateway;
19
use Directus\Database\TableGateway\RelationalTableGateway;
20
use Directus\Database\TableSchema;
21
use Directus\Util\ArrayUtils;
22
23
/**
24
 * Client Local
25
 *
26
 * Client to Interact with the database directly using Directus Database Component
27
 *
28
 * @author Welling Guzmán <[email protected]>
29
 */
30
class ClientLocal extends AbstractClient
31
{
32
    /**
33
     * @var BaseTableGateway[]
34
     */
35
    protected $tableGateways = [];
36
37
    /**
38
     * @var Connection
39
     */
40
    protected $connection = null;
41
42
    /**
43
     * ClientLocal constructor.
44
     *
45
     * @param $connection
46
     */
47
    public function __construct($connection)
48
    {
49
        $this->connection = $connection;
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55
    public function getTables(array $params = [])
56
    {
57
        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 57 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getTables of type Directus\SDK\Response\EntryCollection.
Loading history...
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63
    public function getTable($tableName)
64
    {
65
        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 65 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getTable of type Directus\SDK\Response\Entry.
Loading history...
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71
    public function getColumns($tableName, array $params = [])
72
    {
73
        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 73 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getColumns of type Directus\SDK\Response\EntryCollection.
Loading history...
74
    }
75
76
    /**
77
     * @inheritDoc
78
     */
79
    public function getColumn($tableName, $columnName)
80
    {
81
        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 81 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getColumn of type Directus\SDK\Response\Entry.
Loading history...
82
    }
83
84
    /**
85
     * @inheritDoc
86
     */
87
    public function getEntries($tableName, array $params = [])
88
    {
89
        $tableGateway = $this->getTableGateway($tableName);
90
91
        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 91 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getEntries of type Directus\SDK\Response\EntryCollection.
Loading history...
92
    }
93
94
    /**
95
     * @inheritDoc
96
     */
97
    public function getEntry($tableName, $id, array $params = [])
98
    {
99
        // @TODO: Dynamic ID
100
        return $this->getEntries($tableName, array_merge($params, [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getEntries..., array('id' => $id))); (Directus\SDK\Response\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getEntry 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...
101
            'id' => $id
102
        ]));
103
    }
104
105
    /**
106
     * @inheritDoc
107
     */
108
    public function getUsers(array $params = [])
109
    {
110
        // @TODO: store the directus tables somewhere (SchemaManager?)
111
        return $this->getEntries('directus_users', $params);
112
    }
113
114
    /**
115
     * @inheritDoc
116
     */
117
    public function getUser($id, array $params = [])
118
    {
119
        return $this->getEntry('directus_users', $id, $params);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getEntry('..._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...
120
    }
121
122
    /**
123
     * @inheritDoc
124
     */
125
    public function getGroups(array $params = [])
126
    {
127
        return $this->getEntries('directus_groups', $params);
128
    }
129
130
    /**
131
     * @inheritDoc
132
     */
133
    public function getGroup($id, array $params = [])
134
    {
135
        return $this->getEntry('directus_groups', $id, $params);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getEntry('...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...
136
    }
137
138
    /**
139
     * @inheritDoc
140
     */
141
    public function getGroupPrivileges($groupID)
142
    {
143
        $this->getEntries('directus_privileges', [
144
            'filter' => [
145
                'group_id' => ['eq' => $groupID]
146
            ]
147
        ]);
148
    }
149
150
    /**
151
     * @inheritDoc
152
     */
153
    public function getFiles(array $params = [])
154
    {
155
        return $this->getEntries('directus_files', $params);
156
    }
157
158
    /**
159
     * @inheritDoc
160
     */
161
    public function getFile($id, array $params = [])
162
    {
163
        return $this->getEntry('directus_files', $id, $params);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getEntry('..._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...
164
    }
165
166
    /**
167
     * @inheritDoc
168
     */
169
    public function getSettings()
170
    {
171
        return $this->getEntries('directus_settings');
172
    }
173
174
    /**
175
     * @inheritDoc
176
     */
177
    public function getSettingsByCollection($collectionName)
178
    {
179
        return $this->getEntries('directus_settings', [
180
            'filter' => [
181
                'collection' => ['eq' => $collectionName]
182
            ]
183
        ]);
184
    }
185
186
    /**
187
     * @inheritDoc
188
     */
189
    public function getMessages($userId)
190
    {
191
        $messagesTableGateway = new DirectusMessagesTableGateway($this->connection, null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<Directus\Permissions\Acl>.

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

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

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

function acceptsInteger($int) { }

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

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
192
        $result = $messagesTableGateway->fetchMessagesInboxWithHeaders($userId);
193
194
        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 194 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getMessages of type Directus\SDK\Response\EntryCollection.
Loading history...
195
    }
196
197
    /**
198
     * @inheritDoc
199
     */
200 View Code Duplication
    public function createEntry($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...
201
    {
202
        $tableGateway = $this->getTableGateway($tableName);
203
        $data = $this->processData($tableName, $data);
204
205
        foreach($data as $key => $value) {
206
            if ($value instanceof File) {
207
                $data[$key] = $this->processFile($value);
208
            }
209
        }
210
211
        $newRecord = $tableGateway->manageRecordUpdate($tableName, $data);
212
213
        return $this->getEntry($tableName, $newRecord[$tableGateway->primaryKeyFieldName]);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getEntry($...>primaryKeyFieldName]); (Directus\SDK\Response\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createEntry 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...
214
    }
215
216
    /**
217
     * @inheritDoc
218
     */
219 View Code Duplication
    public function updateEntry($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...
220
    {
221
        $tableGateway = $this->getTableGateway($tableName);
222
        $data = $this->processData($tableName, $data);
223
224
        foreach($data as $key => $value) {
225
            if ($value instanceof File) {
226
                $data[$key] = $this->processFile($value);
227
            }
228
        }
229
230
        $updatedRecord = $tableGateway->manageRecordUpdate($tableName, array_merge($data, ['id' => $id]));
231
232
        return $this->getEntry($tableName, $updatedRecord[$tableGateway->primaryKeyFieldName]);
233
    }
234
235
    /**
236
     * @inheritDoc
237
     */
238
    public function deleteEntry($tableName, $ids)
239
    {
240
        // @TODO: Accept EntryCollection and Entry
241
        $tableGateway = $this->getTableGateway($tableName);
242
243
        if (!is_array($ids)) {
244
            $ids = [$ids];
245
        }
246
247
        return $tableGateway->delete(function($delete) use ($ids) {
248
            return $delete->where->in('id', $ids);
249
        });
250
    }
251
252
    /**
253
     * @inheritDoc
254
     */
255
    public function createUser(array $data)
256
    {
257
        return $this->createEntry('directus_users', $data);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->createEntr...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...
258
    }
259
260
    /**
261
     * @inheritDoc
262
     */
263
    public function updateUser($id, array $data)
264
    {
265
        return $this->updateEntry('directus_users', $id, $data);
266
    }
267
268
    /**
269
     * @inheritDoc
270
     */
271
    public function deleteUser($ids)
272
    {
273
        return $this->deleteEntry('directus_users', $ids);
274
    }
275
276
    /**
277
     * @inheritDoc
278
     */
279
    public function createFile(File $file)
280
    {
281
        $data = $this->processFile($file);
282
283
        return $this->createEntry('directus_files', $data);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->createEntr...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...
284
    }
285
286
    /**
287
     * @inheritDoc
288
     */
289
    public function updateFile($id, array $data)
290
    {
291
        return $this->updateEntry('directus_files', $id, $data);
292
    }
293
294
    /**
295
     * @inheritDoc
296
     */
297
    public function deleteFile($ids)
298
    {
299
        return $this->deleteEntry('directus_files', $ids);
300
    }
301
302
    public function createPreferences($data)
303
    {
304
        if (!ArrayUtils::contains($data, ['title', 'table_name'])) {
305
            throw new \Exception('title and table_name are required');
306
        }
307
308
        $acl = $this->container->get('acl');
309
        $data['user'] = $acl->getUserId();
310
311
        return $this->createEntry('directus_preferences', $data);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->createEntr...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...
312
    }
313
314
    /**
315
     * @inheritdoc
316
     */
317
    public function createBookmark($data)
318
    {
319
        $acl = $this->container->get('acl');
320
        $data['user'] = $acl->getUserId();
321
322
        $preferences = $this->createPreferences(ArrayUtils::pick($data, [
323
            'title', 'table_name', 'sort', 'status', 'search_string', 'sort_order', 'columns_visible', 'user'
324
        ]));
325
326
        $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...
327
        $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...
328
        $bookmarkData = [
329
            'section' => 'search',
330
            'title' => $title,
331
            'url' => 'tables/' . $tableName . '/pref/' . $title,
332
            'user' => $data['user']
333
        ];
334
335
        return $this->createEntry('directus_bookmarks', $bookmarkData);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->createEntr...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...
336
    }
337
338
    /**
339
     * @inheritdoc
340
     */
341
    public function createColumn($data)
342
    {
343
        $data = $this->parseColumnData($data);
344
345
        $tableGateway = $this->getTableGateway($data['table_name']);
346
347
        $tableGateway->addColumn($data['table_name'], ArrayUtils::omit($data, ['table_name']));
348
349
        return $this->getColumn($data['table_name'], $data['column_name']);
350
    }
351
352
    /**
353
     * @inheritdoc
354
     */
355
    public function createGroup(array $data)
356
    {
357
        return $this->createEntry('directus_groups', $data);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->createEntr...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...
358
    }
359
360
    /**
361
     * @inheritdoc
362
     */
363
    public function createMessage(array $data)
364
    {
365
        $this->requiredAttributes(['from', 'message', 'subject'], $data);
366
        $this->requiredOneAttribute(['to', 'toGroup'], $data);
367
368
        $recipients = $this->getMessagesTo($data);
369
        $recipients = explode(',', $recipients);
370
        ArrayUtils::remove($data, ['to', 'toGroup']);
371
372
        $groupRecipients = [];
373
        $userRecipients = [];
374
        foreach ($recipients as $recipient) {
375
            $typeAndId = explode('_', $recipient);
376
            if ($typeAndId[0] == 0) {
377
                $userRecipients[] = $typeAndId[1];
378
            } else {
379
                $groupRecipients[] = $typeAndId[1];
380
            }
381
        }
382
383
        $ZendDb = $this->container->get('connection');
384
        $acl = $this->container->get('acl');
385
        if (count($groupRecipients) > 0) {
386
            $usersTableGateway = new DirectusUsersTableGateway($ZendDb, $acl);
387
            $result = $usersTableGateway->findActiveUserIdsByGroupIds($groupRecipients);
388
            foreach ($result as $item) {
389
                $userRecipients[] = $item['id'];
390
            }
391
        }
392
393
        $userRecipients[] = $acl->getUserId();
394
395
        $messagesTableGateway = new DirectusMessagesTableGateway($ZendDb, $acl);
396
        $id = $messagesTableGateway->sendMessage($data, array_unique($userRecipients), $acl->getUserId());
397
398
        if ($id) {
399
            $Activity = new DirectusActivityTableGateway($ZendDb, $acl);
400
            $data['id'] = $id;
401
            $Activity->recordMessage($data, $acl->getUserId());
402
        }
403
404
        $message = $messagesTableGateway->fetchMessageWithRecipients($id, $acl->getUserId());
405
        $response = [
406
            'meta' => [
407
                'type' => 'entry',
408
                'table' => 'directus_messages'
409
            ],
410
            'data' => $message
411
        ];
412
413
        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 413 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createMessage of type Directus\SDK\Response\Entry.
Loading history...
414
    }
415
416
    /**
417
     * @inheritdoc
418
     */
419
    public function sendMessage(array $data)
420
    {
421
        return $this->createMessage($data);
422
    }
423
424
    public function createPrivileges(array $data)
425
    {
426
        $connection = $this->container->get('connection');
427
        $acl = $this->container->get('acl');
428
        $privileges = new DirectusPrivilegesTableGateway($connection, $acl);
429
430
        $response = [
431
            'meta' => [
432
                'type' => 'entry',
433
                'table' => 'directus_privileges'
434
            ],
435
            'data' => $privileges->insertPrivilege($data)
436
        ];
437
438
        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 438 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createPrivileges of type Directus\SDK\Response\Entry.
Loading history...
439
    }
440
441
    /**
442
     * Get a table gateway for the given table name
443
     *
444
     * @param $tableName
445
     *
446
     * @return RelationalTableGateway
447
     */
448
    protected function getTableGateway($tableName)
449
    {
450
        if (!array_key_exists($tableName, $this->tableGateways)) {
451
            $acl = TableSchema::getAclInstance();
452
            $this->tableGateways[$tableName] = new RelationalTableGateway($tableName, $this->connection, $acl);
453
        }
454
455
        return $this->tableGateways[$tableName];
456
    }
457
}
458