AbstractClient::parseColumnData()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 16
ccs 0
cts 10
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
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 3
        } 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
84
        if (ArrayUtils::has($data, 'avatar_file_id') && $data['avatar_file_id'] instanceof File) {
85
            $data['avatar_file_id'] = $this->processFile($data['avatar_file_id']);
86
        }
87
88
        return $data;
89
    }
90
91
    protected function processOnDirectusFiles($data)
92
    {
93
        // @NOTE: omit columns such id or user.
94
        $data = ArrayUtils::omit($data, ['id', 'user']);
95
96
        return $data;
97
    }
98
99
    protected function processDataOnDirectusPreferences($data)
100
    {
101
        // @NOTE: omit columns such id or user.
102
        $data = ArrayUtils::omit($data, ['id']);
103
104
        return $data;
105
    }
106
107
    protected function processDataOnDirectusBookmarks($data)
108
    {
109
        // @NOTE: omit columns such id or user.
110
        $data = ArrayUtils::omit($data, ['id']);
111
112
        return $data;
113
    }
114
115
    protected function parseColumnData($data)
116
    {
117
        $requiredAttributes = ['name', 'table', 'type', 'ui'];
118
        if (!ArrayUtils::contains($data, $requiredAttributes)) {
119
            throw new \Exception(sprintf('%s are required', implode(',', $requiredAttributes)));
120
        }
121
122
        $data = ArrayUtils::aliasKeys($data, [
123
            'table_name' => 'table',
124
            'column_name' => 'name',
125
            'data_type' => 'type',
126
            'char_length' => 'length'
127
        ]);
128
129
        return $data;
130
    }
131
132
    protected function requiredAttributes(array $attributes, array $data)
133
    {
134 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...
135
            throw new \Exception(sprintf('These attributes are required: %s', implode(',', $attributes)));
136
        }
137
    }
138
139
    protected function requiredOneAttribute(array $attributes, array $data)
140
    {
141 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...
142
            throw new \Exception(sprintf('These attributes are required: %s', implode(',', $attributes)));
143
        }
144
    }
145
146
    protected function getMessagesTo(array $data)
147
    {
148
        $isGroup = ArrayUtils::has($data, 'toGroup');
149
        $to = ArrayUtils::get($data, 'to', ArrayUtils::get($data, 'toGroup', []));
150
151
        if (!is_array($to)) {
152
            $to = explode(',', $to);
153
        }
154
155
        $toIds = array_map(function($id) use ($isGroup) {
156
            return sprintf('%s_%s', ($isGroup ? 1 : 0), $id);
157
        }, $to);
158
159
        return implode(',', $toIds);
160
    }
161
}
162