Completed
Push — master ( 495e06...161604 )
by Welling
02:05
created

ClientLocal   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 3
dl 0
loc 168
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getTableGateway() 0 8 2
A getEntries() 0 6 1
A getEntry() 0 6 1
A fetchTables() 0 4 1
A fetchTableInfo() 0 4 1
A fetchColumns() 0 4 1
A fetchColumnInfo() 0 4 1
A fetchItems() 0 20 4
A fetchItem() 0 4 1
A fetchGroups() 0 4 1
A fetchGroupInfo() 0 4 1
A fetchGroupPrivileges() 0 4 1
A fetchFiles() 0 4 1
A fetchFileInfo() 0 4 1
A fetchSettings() 0 4 1
A fetchSettingCollection() 0 4 1
1
<?php
2
3
namespace Directus\SDK;
4
5
use Directus\Database\Connection;
6
use Directus\Database\TableGateway\BaseTableGateway;
7
use Directus\Database\TableGateway\RelationalTableGatewayWithConditions;
8
use Zend\Db\Sql\Select;
9
10
class ClientLocal implements RequestsInterface
11
{
12
    /**
13
     * @var BaseTableGateway[]
14
     */
15
    protected $tableGateways = [];
16
17
    /**
18
     * @var Connection
19
     */
20
    protected $connection = null;
21
22
    public function __construct($connection)
23
    {
24
        $this->connection = $connection;
25
    }
26
27
    /**
28
     * Get a table gateway for the given table name
29
     *
30
     * @param $tableName
31
     *
32
     * @return RelationalTableGatewayWithConditions
33
     */
34
    protected function getTableGateway($tableName)
35
    {
36
        if (!array_key_exists($tableName, $this->tableGateways)) {
37
            $this->tableGateways[$tableName] = new RelationalTableGatewayWithConditions($tableName, $this->connection);
38
        }
39
40
        return $this->tableGateways[$tableName];
41
    }
42
43
    public function getEntries($tableName, array $options = [])
44
    {
45
        $tableGateway = $this->getTableGateway($tableName);
46
47
        return $tableGateway->getEntries($options);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $tableGateway->getEntries($options); (array) is incompatible with the return type declared by the interface Directus\SDK\RequestsInterface::getEntries of type object.

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...
48
    }
49
50
    public function getEntry($id, $tableName, array $options = [])
51
    {
52
        $tableGateway = $this->getTableGateway($tableName);
53
54
        return $tableGateway->getEntries(array_merge(['id' => $id], $options));
55
    }
56
57
    /**
58
     * @inheritDoc
59
     */
60
    public function fetchTables()
61
    {
62
        // TODO: Implement fetchTables() method.
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68
    public function fetchTableInfo($tableName)
69
    {
70
        // TODO: Implement fetchTableInfo() method.
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76
    public function fetchColumns($tableName)
77
    {
78
        // TODO: Implement fetchColumns() method.
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84
    public function fetchColumnInfo($tableName, $columnName)
85
    {
86
        // TODO: Implement fetchColumnInfo() method.
87
    }
88
89
    /**
90
     * @inheritDoc
91
     */
92
    public function fetchItems($tableName = null, $conditions = [])
93
    {
94
        if ($tableName == null) {
95
            $tableName = $this->getTable();
0 ignored issues
show
Bug introduced by
The method getTable() does not exist on Directus\SDK\ClientLocal. Did you maybe mean getTableGateway()?

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...
96
        }
97
98
        $select = new Select($tableName);
99
100
        // Conditional to honor the active column, (does not check if column exists)
101
        if (isset($conditions['active'])) {
102
            $select->where->equalTo('active', $conditions['active']);
103
        }
104
105
        // Order by "id desc" by default or by a parameter value
106
        if (isset($conditions['sort'])) {
107
            $select->order($conditions['sort']);
108
        }
109
110
        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...
111
    }
112
113
    /**
114
     * @inheritDoc
115
     */
116
    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...
117
    {
118
        // TODO: Implement fetchItem() method.
119
    }
120
121
    /**
122
     * @inheritDoc
123
     */
124
    public function fetchGroups()
125
    {
126
        // TODO: Implement fetchGroups() method.
127
    }
128
129
    /**
130
     * @inheritDoc
131
     */
132
    public function fetchGroupInfo($groupID)
133
    {
134
        // TODO: Implement fetchGroupInfo() method.
135
    }
136
137
    /**
138
     * @inheritDoc
139
     */
140
    public function fetchGroupPrivileges($groupID)
141
    {
142
        // TODO: Implement fetchGroupPrivileges() method.
143
    }
144
145
    /**
146
     * @inheritDoc
147
     */
148
    public function fetchFiles()
149
    {
150
        // TODO: Implement fetchFiles() method.
151
    }
152
153
    /**
154
     * @inheritDoc
155
     */
156
    public function fetchFileInfo($fileID)
157
    {
158
        // TODO: Implement fetchFileInfo() method.
159
    }
160
161
    /**
162
     * @inheritDoc
163
     */
164
    public function fetchSettings()
165
    {
166
        // TODO: Implement fetchSettings() method.
167
    }
168
169
    /**
170
     * @inheritDoc
171
     */
172
    public function fetchSettingCollection($collectionName)
173
    {
174
        // TODO: Implement fetchSettingCollection() method.
175
    }
176
177
}
178