Completed
Push — master ( 9fadf4...ec31af )
by Welling
04:48
created

AbstractClient   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 156
Duplicated Lines 3.85 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 7.94%

Importance

Changes 0
Metric Value
dl 6
loc 156
ccs 5
cts 63
cp 0.0794
rs 10
c 0
b 0
f 0
wmc 28
lcom 0
cbo 6

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setContainer() 0 4 1
A getContainer() 0 4 1
A processOnDirectusFiles() 0 7 1
A processDataOnDirectusPreferences() 0 7 1
A processDataOnDirectusBookmarks() 0 7 1
A requiredAttributes() 3 6 2
A requiredOneAttribute() 3 6 2
A createResponseFromData() 0 10 4
A processData() 0 9 2
A processFile() 0 18 4
A processDataOnDirectusUsers() 0 14 4
A parseColumnData() 0 16 2
A getMessagesTo() 0 15 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Directus\SDK;
4
5
use Directus\SDK\Response\EntryCollection;
6
use Directus\SDK\Response\Entry;
7
use Directus\Util\ArrayUtils;
8
use Directus\Util\StringUtils;
9
10
abstract class AbstractClient implements RequestsInterface
11
{
12
    /**
13
     * @var Container
14
     */
15
    protected $container;
16
17
    /**
18
     * @param Container $container
19
     */
20
    public function setContainer(Container $container)
21
    {
22
        $this->container = $container;
23
    }
24
25
    /**
26
     * @return Container
27
     */
28
    public function getContainer()
29
    {
30
        return $this->container;
31
    }
32
33
    /**
34
     * Creates a response object
35
     *
36
     * @param $data
37
     *
38
     * @return \Directus\SDK\Response\EntryCollection|\Directus\SDK\Response\Entry
39
     */
40 28
    protected function createResponseFromData($data)
41
    {
42 28
        if (isset($data['rows']) || (isset($data['data']) && ArrayUtils::isNumericKeys($data['data']))) {
43 6
            $response = new EntryCollection($data);
44
        } else {
45 22
            $response = new Entry($data);
46
        }
47
48 28
        return $response;
49
    }
50
51
    protected function processData($tableName, array $data)
52
    {
53
        $method = 'processDataOn' . StringUtils::underscoreToCamelCase($tableName, true);
54
        if (method_exists($this, $method)) {
55
            $data = call_user_func_array([$this, $method], [$data]);
56
        }
57
58
        return $data;
59
    }
60
61
    protected function processFile(File $file)
62
    {
63
        $data = $file->toArray();
64
        // Not container, we are using remote :)
65
        if (!$this->container) {
66
            return $data;
67
        }
68
69
        $Files = $this->container->get('files');
0 ignored issues
show
Coding Style introduced by
$Files does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
70
71
        if (!array_key_exists('type', $data) || strpos($data['type'], 'embed/') === 0) {
72
            $recordData = $Files->saveEmbedData($data);
0 ignored issues
show
Coding Style introduced by
$Files does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
73
        } else {
74
            $recordData = $Files->saveData($data['data'], $data['name']);
0 ignored issues
show
Coding Style introduced by
$Files does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
75
        }
76
77
        return array_merge($recordData, ArrayUtils::omit($data, ['data', 'name']));
78
    }
79
80
    protected function processDataOnDirectusUsers($data)
81
    {
82
        $data = ArrayUtils::omit($data, ['id', 'user', 'access_token', 'last_login', 'last_access', 'last_page']);
83
        if (ArrayUtils::has($data, 'password')) {
84
            // @TODO: use Auth hash password
85
            $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT, ['cost' => 12]);
86
        }
87
88
        if (ArrayUtils::has($data, 'avatar_file_id') && $data['avatar_file_id'] instanceof File) {
89
            $data['avatar_file_id'] = $this->processFile($data['avatar_file_id']);
90
        }
91
92
        return $data;
93
    }
94
95
    protected function processOnDirectusFiles($data)
96
    {
97
        // @NOTE: omit columns such id or user.
98
        $data = ArrayUtils::omit($data, ['id', 'user']);
99
100
        return $data;
101
    }
102
103
    protected function processDataOnDirectusPreferences($data)
104
    {
105
        // @NOTE: omit columns such id or user.
106
        $data = ArrayUtils::omit($data, ['id']);
107
108
        return $data;
109
    }
110
111
    protected function processDataOnDirectusBookmarks($data)
112
    {
113
        // @NOTE: omit columns such id or user.
114
        $data = ArrayUtils::omit($data, ['id']);
115
116
        return $data;
117
    }
118
119
    protected function parseColumnData($data)
120
    {
121
        $requiredAttributes = ['name', 'table', 'type', 'ui'];
122
        if (!ArrayUtils::contains($data, $requiredAttributes)) {
123
            throw new \Exception(sprintf('%s are required', implode(',', $requiredAttributes)));
124
        }
125
126
        $data = ArrayUtils::aliasKeys($data, [
127
            'table_name' => 'table',
128
            'column_name' => 'name',
129
            'data_type' => 'type',
130
            'char_length' => 'length'
131
        ]);
132
133
        return $data;
134
    }
135
136
    protected function requiredAttributes(array $attributes, array $data)
137
    {
138 View Code Duplication
        if (!ArrayUtils::contains($data, $attributes)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
139
            throw new \Exception(sprintf('These attributes are required: %s', implode(',', $attributes)));
140
        }
141
    }
142
143
    protected function requiredOneAttribute(array $attributes, array $data)
144
    {
145 View Code Duplication
        if (!ArrayUtils::containsSome($data, $attributes)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
146
            throw new \Exception(sprintf('These attributes are required: %s', implode(',', $attributes)));
147
        }
148
    }
149
150
    protected function getMessagesTo(array $data)
151
    {
152
        $isGroup = ArrayUtils::has($data, 'toGroup');
153
        $to = ArrayUtils::get($data, 'to', ArrayUtils::get($data, 'toGroup', []));
154
155
        if (!is_array($to)) {
156
            $to = explode(',', $to);
157
        }
158
159
        $toIds = array_map(function($id) use ($isGroup) {
160
            return sprintf('%s_%s', ($isGroup ? 1 : 0), $id);
161
        }, $to);
162
163
        return implode(',', $toIds);
164
    }
165
}
166