Completed
Push — d64 ( aa2c4e...97323e )
by Welling
01:57
created

AbstractClient::processDataOnDirectusBookmarks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
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 1
dl 0
loc 7
ccs 0
cts 3
cp 0
crap 2
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
    // @TODO: move to a builder class
34 28
    protected function createResponseFromData($data)
35
    {
36 28
        if (isset($data['rows']) || (isset($data['data']) && ArrayUtils::isNumericKeys($data['data']))) {
37 6
            $response = new EntryCollection($data);
38 6
        } else {
39 22
            $response = new Entry($data);
40
        }
41
42 28
        return $response;
43
    }
44
45
    protected function processData($tableName, array $data)
46
    {
47
        $method = 'processDataOn' . StringUtils::underscoreToCamelCase($tableName, true);
48
        if (method_exists($this, $method)) {
49
            $data = call_user_func_array([$this, $method], [$data]);
50
        }
51
52
        return $data;
53
    }
54
55
    protected function processFile(File $file)
56
    {
57
        $Files = $this->container->get('files');
58
        $data = $file->toArray();
59
        if (!array_key_exists('type', $data) || strpos($data['type'], 'embed/') === 0) {
60
            $recordData = $Files->saveEmbedData($data);
61
        } else {
62
            $recordData = $Files->saveData($data['data'], $data['name']);
63
        }
64
65
        return array_merge($recordData, ArrayUtils::omit($data, ['data', 'name']));
66
    }
67
68
    protected function processDataOnDirectusUsers($data)
69
    {
70
        $data = ArrayUtils::omit($data, ['id', 'user', 'access_token', 'last_login', 'last_access', 'last_page']);
71
        if (ArrayUtils::has($data, 'password')) {
72
            // @TODO: use Auth hash password
73
            $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT, ['cost' => 12]);
74
        }
75
76
        if (ArrayUtils::has($data, 'avatar_file_id')) {
77
            $data['avatar_file_id'] = $this->processFile($data['avatar_file_id']);
78
        }
79
80
        return $data;
81
    }
82
83
    protected function processOnDirectusFiles($data)
84
    {
85
        // @NOTE: omit columns such id or user.
86
        $data = ArrayUtils::omit($data, ['id', 'user']);
87
88
        return $data;
89
    }
90
91
    protected function processDataOnDirectusPreferences($data)
92
    {
93
        // @NOTE: omit columns such id or user.
94
        $data = ArrayUtils::omit($data, ['id']);
95
96
        return $data;
97
    }
98
99
    protected function processDataOnDirectusBookmarks($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 parseColumnData($data)
108
    {
109
        $requiredAttributes = ['name', 'table', 'type', 'ui'];
110
        if (!ArrayUtils::contains($data, $requiredAttributes)) {
111
            throw new \Exception(sprintf('%s are required', implode(',', $requiredAttributes)));
112
        }
113
114
        $data = ArrayUtils::aliasKeys($data, [
0 ignored issues
show
Bug introduced by
The method aliasKeys() does not seem to exist on object<Directus\Util\ArrayUtils>.

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...
115
            'table_name' => 'table',
116
            'column_name' => 'name',
117
            'data_type' => 'type',
118
            'char_length' => 'length'
119
        ]);
120
121
        return $data;
122
    }
123
}