Completed
Push — d64 ( b460e4...2ffd1a )
by Welling
03:33
created

ClientLocal::getTables()   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 1
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\RelationalTableGatewayWithConditions;
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
        $tableGateway = $this->getTableGateway($tableName);
116
117
        // @TODO: Dynamic ID
118
        return $tableGateway->getEntries(array_merge($params, ['id' => $id]));
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
        $tableGateway = $this->getTableGateway('directus_users');
131
132
        return $tableGateway->getEntries($params);
133
    }
134
135
    /**
136
     * Gets an user by the given id
137
     *
138
     * @param $id
139
     * @param array $params
140
     *
141
     * @return array|mixed
142
     */
143
    public function getUser($id, array $params = [])
144
    {
145
        return $this->getEntry('directus_users', $id, $params);
146
    }
147
148
    /**
149
     * @inheritDoc
150
     */
151
    public function fetchTables()
152
    {
153
        // TODO: Implement fetchTables() method.
154
    }
155
156
    /**
157
     * @inheritDoc
158
     */
159
    public function fetchTableInfo($tableName)
160
    {
161
        // TODO: Implement fetchTableInfo() method.
162
    }
163
164
    /**
165
     * @inheritDoc
166
     */
167
    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...
168
    {
169
        // TODO: Implement fetchColumns() method.
170
    }
171
172
    /**
173
     * @inheritDoc
174
     */
175
    public function fetchColumnInfo($tableName, $columnName)
176
    {
177
        // TODO: Implement fetchColumnInfo() method.
178
    }
179
180
    /**
181
     * @inheritDoc
182
     */
183
    public function fetchItems($tableName = null, $conditions = [])
184
    {
185
        if ($tableName == null) {
186
            $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...
187
        }
188
189
        $select = new Select($tableName);
190
191
        // Conditional to honor the active column, (does not check if column exists)
192
        if (isset($conditions['active'])) {
193
            $select->where->equalTo('active', $conditions['active']);
194
        }
195
196
        // Order by "id desc" by default or by a parameter value
197
        if (isset($conditions['sort'])) {
198
            $select->order($conditions['sort']);
199
        }
200
201
        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...
202
    }
203
204
    /**
205
     * @inheritDoc
206
     */
207
    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...
208
    {
209
        // TODO: Implement fetchItem() method.
210
    }
211
212
    /**
213
     * @inheritDoc
214
     */
215
    public function fetchGroups()
216
    {
217
        // TODO: Implement fetchGroups() method.
218
    }
219
220
    /**
221
     * @inheritDoc
222
     */
223
    public function fetchGroupInfo($groupID)
224
    {
225
        // TODO: Implement fetchGroupInfo() method.
226
    }
227
228
    /**
229
     * @inheritDoc
230
     */
231
    public function fetchGroupPrivileges($groupID)
232
    {
233
        // TODO: Implement fetchGroupPrivileges() method.
234
    }
235
236
    /**
237
     * @inheritDoc
238
     */
239
    public function fetchFiles()
240
    {
241
        // TODO: Implement fetchFiles() method.
242
    }
243
244
    /**
245
     * @inheritDoc
246
     */
247
    public function fetchFileInfo($fileID)
248
    {
249
        // TODO: Implement fetchFileInfo() method.
250
    }
251
252
    /**
253
     * @inheritDoc
254
     */
255
    public function fetchSettings()
256
    {
257
        // TODO: Implement fetchSettings() method.
258
    }
259
260
    /**
261
     * @inheritDoc
262
     */
263
    public function fetchSettingCollection($collectionName)
264
    {
265
        // TODO: Implement fetchSettingCollection() method.
266
    }
267
268
    /**
269
     * Get a table gateway for the given table name
270
     *
271
     * @param $tableName
272
     *
273
     * @return RelationalTableGatewayWithConditions
274
     */
275
    protected function getTableGateway($tableName)
276
    {
277
        if (!array_key_exists($tableName, $this->tableGateways)) {
278
            $this->tableGateways[$tableName] = new RelationalTableGatewayWithConditions($tableName, $this->connection);
279
        }
280
281
        return $this->tableGateways[$tableName];
282
    }
283
284
    // @TODO: move to a builder class
285
    protected function createResponseFromData($data)
286
    {
287
        if (isset($data['rows'])) {
288
            $response = new EntryCollection($data);
289
        } else {
290
            $response = new Entry($data);
291
        }
292
293
        return $response;
294
    }
295
}
296