Completed
Push — d64 ( 97323e...2997b8 )
by Welling
06:27
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\DirectusUsersTableGateway;
18
use Directus\Database\TableGateway\RelationalTableGateway;
19
use Directus\Database\TableSchema;
20
use Directus\Util\ArrayUtils;
21
22
/**
23
 * Client Local
24
 *
25
 * Client to Interact with the database directly using Directus Database Component
26
 *
27
 * @author Welling Guzmán <[email protected]>
28
 */
29
class ClientLocal extends AbstractClient
30
{
31
    /**
32
     * @var BaseTableGateway[]
33
     */
34
    protected $tableGateways = [];
35
36
    /**
37
     * @var Connection
38
     */
39
    protected $connection = null;
40
41
    /**
42
     * ClientLocal constructor.
43
     *
44
     * @param $connection
45
     */
46
    public function __construct($connection)
47
    {
48
        $this->connection = $connection;
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54
    public function getTables(array $params = [])
55
    {
56
        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 56 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getTables of type Directus\SDK\Response\EntryCollection.
Loading history...
57
    }
58
59
    /**
60
     * @inheritDoc
61
     */
62
    public function getTable($tableName)
63
    {
64
        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 64 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getTable of type Directus\SDK\Response\Entry.
Loading history...
65
    }
66
67
    /**
68
     * @inheritDoc
69
     */
70
    public function getColumns($tableName, array $params = [])
71
    {
72
        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 72 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getColumns of type Directus\SDK\Response\EntryCollection.
Loading history...
73
    }
74
75
    /**
76
     * @inheritDoc
77
     */
78
    public function getColumn($tableName, $columnName)
79
    {
80
        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 80 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getColumn of type Directus\SDK\Response\Entry.
Loading history...
81
    }
82
83
    /**
84
     * @inheritDoc
85
     */
86
    public function getEntries($tableName, array $params = [])
87
    {
88
        $tableGateway = $this->getTableGateway($tableName);
89
90
        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 90 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getEntries of type Directus\SDK\Response\EntryCollection.
Loading history...
91
    }
92
93
    /**
94
     * @inheritDoc
95
     */
96
    public function getEntry($tableName, $id, array $params = [])
97
    {
98
        // @TODO: Dynamic ID
99
        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...
100
            'id' => $id
101
        ]));
102
    }
103
104
    /**
105
     * @inheritDoc
106
     */
107
    public function getUsers(array $params = [])
108
    {
109
        // @TODO: store the directus tables somewhere (SchemaManager?)
110
        return $this->getEntries('directus_users', $params);
111
    }
112
113
    /**
114
     * @inheritDoc
115
     */
116
    public function getUser($id, array $params = [])
117
    {
118
        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...
119
    }
120
121
    /**
122
     * @inheritDoc
123
     */
124
    public function getGroups(array $params = [])
125
    {
126
        return $this->getEntries('directus_groups', $params);
127
    }
128
129
    /**
130
     * @inheritDoc
131
     */
132
    public function getGroup($id, array $params = [])
133
    {
134
        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...
135
    }
136
137
    /**
138
     * @inheritDoc
139
     */
140
    public function getGroupPrivileges($groupID)
141
    {
142
        $this->getEntries('directus_privileges', [
143
            'filter' => [
144
                'group_id' => ['eq' => $groupID]
145
            ]
146
        ]);
147
    }
148
149
    /**
150
     * @inheritDoc
151
     */
152
    public function getFiles(array $params = [])
153
    {
154
        return $this->getEntries('directus_files', $params);
155
    }
156
157
    /**
158
     * @inheritDoc
159
     */
160
    public function getFile($id, array $params = [])
161
    {
162
        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...
163
    }
164
165
    /**
166
     * @inheritDoc
167
     */
168
    public function getSettings()
169
    {
170
        return $this->getEntries('directus_settings');
171
    }
172
173
    /**
174
     * @inheritDoc
175
     */
176
    public function getSettingsByCollection($collectionName)
177
    {
178
        return $this->getEntries('directus_settings', [
179
            'filter' => [
180
                'collection' => ['eq' => $collectionName]
181
            ]
182
        ]);
183
    }
184
185
    /**
186
     * @inheritDoc
187
     */
188
    public function getMessages($userId)
189
    {
190
        $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...
191
        $result = $messagesTableGateway->fetchMessagesInboxWithHeaders($userId);
192
193
        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 193 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getMessages of type Directus\SDK\Response\EntryCollection.
Loading history...
194
    }
195
196
    /**
197
     * @inheritDoc
198
     */
199 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...
200
    {
201
        $tableGateway = $this->getTableGateway($tableName);
202
        $data = $this->processData($tableName, $data);
203
204
        foreach($data as $key => $value) {
205
            if ($value instanceof File) {
206
                $data[$key] = $this->processFile($value);
207
            }
208
        }
209
210
        $newRecord = $tableGateway->manageRecordUpdate($tableName, $data);
211
212
        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...
213
    }
214
215
    /**
216
     * @inheritDoc
217
     */
218 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...
219
    {
220
        $tableGateway = $this->getTableGateway($tableName);
221
        $data = $this->processData($tableName, $data);
222
223
        foreach($data as $key => $value) {
224
            if ($value instanceof File) {
225
                $data[$key] = $this->processFile($value);
226
            }
227
        }
228
229
        $updatedRecord = $tableGateway->manageRecordUpdate($tableName, array_merge($data, ['id' => $id]));
230
231
        return $this->getEntry($tableName, $updatedRecord[$tableGateway->primaryKeyFieldName]);
232
    }
233
234
    /**
235
     * @inheritDoc
236
     */
237
    public function deleteEntry($tableName, $ids)
238
    {
239
        // @TODO: Accept EntryCollection and Entry
240
        $tableGateway = $this->getTableGateway($tableName);
241
242
        if (!is_array($ids)) {
243
            $ids = [$ids];
244
        }
245
246
        return $tableGateway->delete(function($delete) use ($ids) {
247
            return $delete->where->in('id', $ids);
248
        });
249
    }
250
251
    /**
252
     * @inheritDoc
253
     */
254
    public function createUser(array $data)
255
    {
256
        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...
257
    }
258
259
    /**
260
     * @inheritDoc
261
     */
262
    public function updateUser($id, array $data)
263
    {
264
        return $this->updateEntry('directus_users', $id, $data);
265
    }
266
267
    /**
268
     * @inheritDoc
269
     */
270
    public function deleteUser($ids)
271
    {
272
        return $this->deleteEntry('directus_users', $ids);
273
    }
274
275
    /**
276
     * @inheritDoc
277
     */
278
    public function createFile(File $file)
279
    {
280
        $data = $this->processFile($file);
281
282
        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...
283
    }
284
285
    /**
286
     * @inheritDoc
287
     */
288
    public function updateFile($id, array $data)
289
    {
290
        return $this->updateEntry('directus_files', $id, $data);
291
    }
292
293
    /**
294
     * @inheritDoc
295
     */
296
    public function deleteFile($ids)
297
    {
298
        return $this->deleteEntry('directus_files', $ids);
299
    }
300
301
    public function createPreferences($data)
302
    {
303
        if (!ArrayUtils::contains($data, ['title', 'table_name'])) {
304
            throw new \Exception('title and table_name are required');
305
        }
306
307
        $acl = $this->container->get('acl');
308
        $data['user'] = $acl->getUserId();
309
310
        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...
311
    }
312
313
    /**
314
     * @inheritdoc
315
     */
316
    public function createBookmark($data)
317
    {
318
        $acl = $this->container->get('acl');
319
        $data['user'] = $acl->getUserId();
320
321
        $preferences = $this->createPreferences(ArrayUtils::pick($data, [
322
            'title', 'table_name', 'sort', 'status', 'search_string', 'sort_order', 'columns_visible', 'user'
323
        ]));
324
325
        $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...
326
        $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...
327
        $bookmarkData = [
328
            'section' => 'search',
329
            'title' => $title,
330
            'url' => 'tables/' . $tableName . '/pref/' . $title,
331
            'user' => $data['user']
332
        ];
333
334
        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...
335
    }
336
337
    /**
338
     * @inheritdoc
339
     */
340
    public function createColumn($data)
341
    {
342
        $data = $this->parseColumnData($data);
343
344
        $tableGateway = $this->getTableGateway($data['table_name']);
345
346
        $tableGateway->addColumn($data['table_name'], ArrayUtils::omit($data, ['table_name']));
347
348
        return $this->getColumn($data['table_name'], $data['column_name']);
349
    }
350
351
    /**
352
     * @inheritdoc
353
     */
354
    public function createGroup(array $data)
355
    {
356
        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...
357
    }
358
359
    /**
360
     * @inheritdoc
361
     */
362
    public function createMessage(array $data)
363
    {
364
        $this->requiredAttributes(['from', 'message', 'subject'], $data);
365
        $this->requiredOneAttribute(['to', 'toGroup'], $data);
366
367
        $recipients = $this->getMessagesTo($data);
368
        $recipients = explode(',', $recipients);
369
        ArrayUtils::remove($data, ['to', 'toGroup']);
370
371
        $groupRecipients = [];
372
        $userRecipients = [];
373
        foreach ($recipients as $recipient) {
374
            $typeAndId = explode('_', $recipient);
375
            if ($typeAndId[0] == 0) {
376
                $userRecipients[] = $typeAndId[1];
377
            } else {
378
                $groupRecipients[] = $typeAndId[1];
379
            }
380
        }
381
382
        $ZendDb = $this->container->get('connection');
383
        $acl = $this->container->get('acl');
384
        if (count($groupRecipients) > 0) {
385
            $usersTableGateway = new DirectusUsersTableGateway($ZendDb, $acl);
386
            $result = $usersTableGateway->findActiveUserIdsByGroupIds($groupRecipients);
387
            foreach ($result as $item) {
388
                $userRecipients[] = $item['id'];
389
            }
390
        }
391
392
        $userRecipients[] = $acl->getUserId();
393
394
        $messagesTableGateway = new DirectusMessagesTableGateway($ZendDb, $acl);
395
        $id = $messagesTableGateway->sendMessage($data, array_unique($userRecipients), $acl->getUserId());
396
397
        if ($id) {
398
            $Activity = new DirectusActivityTableGateway($ZendDb, $acl);
399
            $data['id'] = $id;
400
            $Activity->recordMessage($data, $acl->getUserId());
401
        }
402
403
        $message = $messagesTableGateway->fetchMessageWithRecipients($id, $acl->getUserId());
404
        $response = [
405
            'meta' => [
406
                'type' => 'entry',
407
                'table' => 'directus_messages'
408
            ],
409
            'data' => $message
410
        ];
411
412
        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 412 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::createMessage of type Directus\SDK\Response\Entry.
Loading history...
413
    }
414
415
    /**
416
     * @inheritdoc
417
     */
418
    public function sendMessage(array $data)
419
    {
420
        return $this->createMessage($data);
421
    }
422
423
    /**
424
     * Get a table gateway for the given table name
425
     *
426
     * @param $tableName
427
     *
428
     * @return RelationalTableGateway
429
     */
430
    protected function getTableGateway($tableName)
431
    {
432
        if (!array_key_exists($tableName, $this->tableGateways)) {
433
            $acl = TableSchema::getAclInstance();
434
            $this->tableGateways[$tableName] = new RelationalTableGateway($tableName, $this->connection, $acl);
435
        }
436
437
        return $this->tableGateways[$tableName];
438
    }
439
}
440