Completed
Push — d64 ( d3edb0...620cfc )
by Welling
02:34
created

ClientLocal::getTableColumns()   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\RelationalTableGateway;
16
use Directus\Database\TableSchema;
17
use Directus\SDK\Response\EntryCollection;
18
use Directus\SDK\Response\Entry;
19
use Zend\Db\Sql\Select;
20
21
/**
22
 * Client Local
23
 *
24
 * Client to Interact with the database directly using Directus Database Component
25
 *
26
 * @author Welling Guzmán <[email protected]>
27
 */
28
class ClientLocal implements RequestsInterface
29
{
30
    /**
31
     * @var BaseTableGateway[]
32
     */
33
    protected $tableGateways = [];
34
35
    /**
36
     * @var Connection
37
     */
38
    protected $connection = null;
39
40
    /**
41
     * ClientLocal constructor.
42
     *
43
     * @param $connection
44
     */
45
    public function __construct($connection)
46
    {
47
        $this->connection = $connection;
48
    }
49
50
    /**
51
     * Gets the list of tables name in the database
52
     *
53
     * @param array $params
54
     *
55
     * @return array
56
     */
57
    public function getTables(array $params = [])
58
    {
59
        return TableSchema::getTablesSchema($params);
60
    }
61
62
    /**
63
     * Gets all the columns in the database
64
     *
65
     * @param array $params
66
     *
67
     * @return array
68
     */
69
    public function getColumns(array $params = [])
70
    {
71
        return TableSchema::getColumnsSchema($params);
72
    }
73
74
    /**
75
     * Gets table columns
76
     *
77
     * @param $tableName
78
     * @param array $params
79
     *
80
     * @return \Directus\Database\Object\Column[]
81
     */
82
    public function getTableColumns($tableName, array $params = [])
83
    {
84
        $tables = TableSchema::getTableColumnsSchema($tableName, $params);
0 ignored issues
show
Bug introduced by
The method getTableColumnsSchema() does not exist on Directus\Database\TableSchema. Did you maybe mean getTable()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
85
86
        return $tables;
87
    }
88
89
    /**
90
     * Gets all the entries in the given table name
91
     *
92
     * @param string $tableName
93
     * @param array $params
94
     *
95
     * @return Entry|EntryCollection
96
     */
97
    public function getEntries($tableName, array $params = [])
98
    {
99
        $tableGateway = $this->getTableGateway($tableName);
100
101
        return $this->createResponseFromData($tableGateway->getEntries($params));
102
    }
103
104
    /**
105
     * Gets an entry in the given table name with the given id
106
     *
107
     * @param string $tableName
108
     * @param mixed $id
109
     * @param array $params
110
     *
111
     * @return array|mixed
112
     */
113
    public function getEntry($tableName, $id, array $params = [])
114
    {
115
        // @TODO: Dynamic ID
116
        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\En...esponse\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getEntry of type array.

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...
117
            'id' => $id
118
        ]));
119
    }
120
121
    /**
122
     * Gets the list of users
123
     *
124
     * @param array $params
125
     *
126
     * @return array|mixed
127
     */
128
    public function getUsers(array $params = [])
129
    {
130
        // @TODO: store the directus tables somewhere (SchemaManager?)
131
        return $this->getEntries('directus_users', $params);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getEntries...ectus_users', $params); (Directus\SDK\Response\En...esponse\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getUsers of type array.

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
     * Gets an user by the given id
136
     *
137
     * @param $id
138
     * @param array $params
139
     *
140
     * @return array|mixed
141
     */
142
    public function getUser($id, array $params = [])
143
    {
144
        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\En...esponse\EntryCollection) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getUser of type array.

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...
145
    }
146
147
    /**
148
     * @inheritDoc
149
     */
150
    public function fetchTables()
151
    {
152
        // TODO: Implement fetchTables() method.
153
    }
154
155
    /**
156
     * @inheritDoc
157
     */
158
    public function fetchTableInfo($tableName)
159
    {
160
        // TODO: Implement fetchTableInfo() method.
161
    }
162
163
    /**
164
     * @inheritDoc
165
     */
166
    public function fetchColumns($tableName)
0 ignored issues
show
Unused Code introduced by
The parameter $tableName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
167
    {
168
        // TODO: Implement fetchColumns() method.
169
    }
170
171
    /**
172
     * @inheritDoc
173
     */
174
    public function fetchColumnInfo($tableName, $columnName)
175
    {
176
        // TODO: Implement fetchColumnInfo() method.
177
    }
178
179
    /**
180
     * @inheritDoc
181
     */
182
    public function fetchItems($tableName = null, $conditions = [])
183
    {
184
        if ($tableName == null) {
185
            $tableName = $this->getTable();
0 ignored issues
show
Bug introduced by
The method getTable() does not exist on Directus\SDK\ClientLocal. Did you maybe mean getTables()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
186
        }
187
188
        $select = new Select($tableName);
189
190
        // Conditional to honor the active column, (does not check if column exists)
191
        if (isset($conditions['active'])) {
192
            $select->where->equalTo('active', $conditions['active']);
193
        }
194
195
        // Order by "id desc" by default or by a parameter value
196
        if (isset($conditions['sort'])) {
197
            $select->order($conditions['sort']);
198
        }
199
200
        return $this->selectWith($select);
0 ignored issues
show
Bug introduced by
The method selectWith() does not seem to exist on object<Directus\SDK\ClientLocal>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
201
    }
202
203
    /**
204
     * @inheritDoc
205
     */
206
    public function fetchItem($tableName, $itemID)
0 ignored issues
show
Unused Code introduced by
The parameter $tableName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $itemID is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
207
    {
208
        // TODO: Implement fetchItem() method.
209
    }
210
211
    /**
212
     * @inheritDoc
213
     */
214
    public function fetchGroups()
215
    {
216
        // TODO: Implement fetchGroups() method.
217
    }
218
219
    /**
220
     * @inheritDoc
221
     */
222
    public function fetchGroupInfo($groupID)
223
    {
224
        // TODO: Implement fetchGroupInfo() method.
225
    }
226
227
    /**
228
     * @inheritDoc
229
     */
230
    public function fetchGroupPrivileges($groupID)
231
    {
232
        // TODO: Implement fetchGroupPrivileges() method.
233
    }
234
235
    /**
236
     * @inheritDoc
237
     */
238
    public function getFiles(array $params = [])
239
    {
240
        return $this->getEntries('directus_files', $params);
241
    }
242
243
    /**
244
     * @inheritDoc
245
     */
246
    public function getFile($id, array $params = [])
247
    {
248
        return $this->getEntry('directus_files', $id, $params);
249
    }
250
251
    /**
252
     * @inheritDoc
253
     */
254
    public function fetchSettings()
255
    {
256
        // TODO: Implement fetchSettings() method.
257
    }
258
259
    /**
260
     * @inheritDoc
261
     */
262
    public function fetchSettingCollection($collectionName)
263
    {
264
        // TODO: Implement fetchSettingCollection() method.
265
    }
266
267
    /**
268
     * Get a table gateway for the given table name
269
     *
270
     * @param $tableName
271
     *
272
     * @return RelationalTableGateway
273
     */
274
    protected function getTableGateway($tableName)
275
    {
276
        if (!array_key_exists($tableName, $this->tableGateways)) {
277
            $this->tableGateways[$tableName] = new RelationalTableGateway($tableName, $this->connection);
278
        }
279
280
        return $this->tableGateways[$tableName];
281
    }
282
283
    // @TODO: move to a builder class
284
    protected function createResponseFromData($data)
285
    {
286
        if (isset($data['rows'])) {
287
            $response = new EntryCollection($data);
288
        } else {
289
            $response = new Entry($data);
290
        }
291
292
        return $response;
293
    }
294
}
295