Completed
Push — d64 ( 715811...47f128 )
by Welling
03:02
created

ClientLocal::updateFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Directus – <http://getdirectus.com>
5
 *
6
 * @link      The canonical repository – <https://github.com/directus/directus>
7
 * @copyright Copyright 2006-2016 RANGER Studio, LLC – <http://rangerstudio.com>
8
 * @license   GNU General Public License (v3) – <http://www.gnu.org/copyleft/gpl.html>
9
 */
10
11
namespace Directus\SDK;
12
13
use Directus\Database\Connection;
14
use Directus\Database\TableGateway\BaseTableGateway;
15
use Directus\Database\TableGateway\DirectusMessagesTableGateway;
16
use Directus\Database\TableGateway\RelationalTableGateway;
17
use Directus\Database\TableSchema;
18
19
/**
20
 * Client Local
21
 *
22
 * Client to Interact with the database directly using Directus Database Component
23
 *
24
 * @author Welling Guzmán <[email protected]>
25
 */
26
class ClientLocal extends AbstractClient
27
{
28
    /**
29
     * @var BaseTableGateway[]
30
     */
31
    protected $tableGateways = [];
32
33
    /**
34
     * @var Connection
35
     */
36
    protected $connection = null;
37
38
    /**
39
     * ClientLocal constructor.
40
     *
41
     * @param $connection
42
     */
43
    public function __construct($connection)
44
    {
45
        $this->connection = $connection;
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51
    public function getTables(array $params = [])
52
    {
53
        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 53 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getTables of type Directus\SDK\Response\EntryCollection.
Loading history...
54
    }
55
56
    /**
57
     * @inheritDoc
58
     */
59
    public function getTable($tableName)
60
    {
61
        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 61 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getTable of type Directus\SDK\Response\Entry.
Loading history...
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67
    public function getColumns($tableName, array $params = [])
68
    {
69
        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 69 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getColumns of type Directus\SDK\Response\EntryCollection.
Loading history...
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75
    public function getColumn($tableName, $columnName)
76
    {
77
        return $this->createResponseFromData(TableSchema::getColumnSchemaArray($tableName, $columnName));
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->createResponseFro...bleName, $columnName)); of type Directus\SDK\Response\En...ctus\SDK\Response\Entry adds the type Directus\SDK\Response\EntryCollection to the return on line 77 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getColumn of type Directus\SDK\Response\Entry.
Loading history...
78
    }
79
80
    /**
81
     * @inheritDoc
82
     */
83
    public function getEntries($tableName, array $params = [])
84
    {
85
        $tableGateway = $this->getTableGateway($tableName);
86
87
        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 87 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getEntries of type Directus\SDK\Response\EntryCollection.
Loading history...
88
    }
89
90
    /**
91
     * @inheritDoc
92
     */
93
    public function getEntry($tableName, $id, array $params = [])
94
    {
95
        // @TODO: Dynamic ID
96
        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...
97
            'id' => $id
98
        ]));
99
    }
100
101
    /**
102
     * @inheritDoc
103
     */
104
    public function getUsers(array $params = [])
105
    {
106
        // @TODO: store the directus tables somewhere (SchemaManager?)
107
        return $this->getEntries('directus_users', $params);
108
    }
109
110
    /**
111
     * @inheritDoc
112
     */
113
    public function getUser($id, array $params = [])
114
    {
115
        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...
116
    }
117
118
    /**
119
     * @inheritDoc
120
     */
121
    public function getGroups(array $params = [])
122
    {
123
        return $this->getEntries('directus_groups', $params);
124
    }
125
126
    /**
127
     * @inheritDoc
128
     */
129
    public function getGroup($id, array $params = [])
130
    {
131
        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...
132
    }
133
134
    /**
135
     * @inheritDoc
136
     */
137
    public function getGroupPrivileges($groupID)
138
    {
139
        $this->getEntries('directus_privileges', [
140
            'filter' => [
141
                'group_id' => ['eq' => $groupID]
142
            ]
143
        ]);
144
    }
145
146
    /**
147
     * @inheritDoc
148
     */
149
    public function getFiles(array $params = [])
150
    {
151
        return $this->getEntries('directus_files', $params);
152
    }
153
154
    /**
155
     * @inheritDoc
156
     */
157
    public function getFile($id, array $params = [])
158
    {
159
        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...
160
    }
161
162
    /**
163
     * @inheritDoc
164
     */
165
    public function getSettings()
166
    {
167
        return $this->getEntries('directus_settings');
168
    }
169
170
    /**
171
     * @inheritDoc
172
     */
173
    public function getSettingsByCollection($collectionName)
174
    {
175
        return $this->getEntries('directus_settings', [
176
            'filter' => [
177
                'collection' => ['eq' => $collectionName]
178
            ]
179
        ]);
180
    }
181
182
    /**
183
     * @inheritDoc
184
     */
185
    public function getMessages($userId)
186
    {
187
        $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...
188
        $result = $messagesTableGateway->fetchMessagesInboxWithHeaders($userId);
189
190
        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 190 which is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getMessages of type Directus\SDK\Response\EntryCollection.
Loading history...
191
    }
192
193
    /**
194
     * @inheritDoc
195
     */
196 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...
197
    {
198
        $tableGateway = $this->getTableGateway($tableName);
199
        $data = $this->processData($tableName, $data);
200
201
        foreach($data as $key => $value) {
202
            if ($value instanceof File) {
203
                $data[$key] = $this->processFile($value);
204
            }
205
        }
206
207
        $newRecord = $tableGateway->manageRecordUpdate($tableName, $data);
208
209
        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...
210
    }
211
212
    /**
213
     * @inheritDoc
214
     */
215 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...
216
    {
217
        $tableGateway = $this->getTableGateway($tableName);
218
        $data = $this->processData($tableName, $data);
219
220
        foreach($data as $key => $value) {
221
            if ($value instanceof File) {
222
                $data[$key] = $this->processFile($value);
223
            }
224
        }
225
226
        $updatedRecord = $tableGateway->manageRecordUpdate($tableName, array_merge($data, ['id' => $id]));
227
228
        return $this->getEntry($tableName, $updatedRecord[$tableGateway->primaryKeyFieldName]);
229
    }
230
231
    /**
232
     * @inheritDoc
233
     */
234
    public function deleteEntry($tableName, $ids)
235
    {
236
        // @TODO: Accept EntryCollection and Entry
237
        $tableGateway = $this->getTableGateway($tableName);
238
239
        if (!is_array($ids)) {
240
            $ids = [$ids];
241
        }
242
243
        return $tableGateway->delete(function($delete) use ($ids) {
244
            return $delete->where->in('id', $ids);
245
        });
246
    }
247
248
    /**
249
     * @inheritDoc
250
     */
251
    public function createUser(array $data)
252
    {
253
        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...
254
    }
255
256
    /**
257
     * @inheritDoc
258
     */
259
    public function updateUser($id, array $data)
260
    {
261
        return $this->updateEntry('directus_users', $id, $data);
262
    }
263
264
    /**
265
     * @inheritDoc
266
     */
267
    public function deleteUser($ids)
268
    {
269
        return $this->deleteEntry('directus_users', $ids);
270
    }
271
272
    /**
273
     * @inheritDoc
274
     */
275
    public function createFile(File $file)
276
    {
277
        $data = $this->processFile($file);
278
279
        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...
280
    }
281
282
    /**
283
     * @inheritDoc
284
     */
285
    public function updateFile($id, array $data)
286
    {
287
        return $this->updateEntry('directus_files', $id, $data);
288
    }
289
290
    /**
291
     * @inheritDoc
292
     */
293
    public function deleteFile($ids)
294
    {
295
        return $this->deleteEntry('directus_files', $ids);
296
    }
297
298
    /**
299
     * Get a table gateway for the given table name
300
     *
301
     * @param $tableName
302
     *
303
     * @return RelationalTableGateway
304
     */
305
    protected function getTableGateway($tableName)
306
    {
307
        if (!array_key_exists($tableName, $this->tableGateways)) {
308
            $acl = TableSchema::getAclInstance();
309
            $this->tableGateways[$tableName] = new RelationalTableGateway($tableName, $this->connection, $acl);
310
        }
311
312
        return $this->tableGateways[$tableName];
313
    }
314
}
315