Completed
Push — d64 ( 97323e...2997b8 )
by Welling
06:27
created

AbstractClient::getMessagesTo()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 2
nop 1
dl 0
loc 15
ccs 0
cts 9
cp 0
crap 12
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 View Code Duplication
        if (!ArrayUtils::contains($data, $requiredAttributes)) {
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...
111
            throw new \Exception(sprintf('%s are required', implode(',', $requiredAttributes)));
112
        }
113
114
        $data = ArrayUtils::aliasKeys($data, [
115
            'table_name' => 'table',
116
            'column_name' => 'name',
117
            'data_type' => 'type',
118
            'char_length' => 'length'
119
        ]);
120
121
        return $data;
122
    }
123
124
    protected function requiredAttributes(array $attributes, array $data)
125
    {
126 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...
127
            throw new \Exception(sprintf('These attributes are required: %s', implode(',', $attributes)));
128
        }
129
    }
130
131
    protected function requiredOneAttribute(array $attributes, array $data)
132
    {
133 View Code Duplication
        if (!ArrayUtils::containsSome($data, $attributes)) {
0 ignored issues
show
Bug introduced by
The method containsSome() does not exist on Directus\Util\ArrayUtils. Did you maybe mean contains()?

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...
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...
134
            throw new \Exception(sprintf('These attributes are required: %s', implode(',', $attributes)));
135
        }
136
    }
137
138
    protected function getMessagesTo(array $data)
139
    {
140
        $isGroup = ArrayUtils::has($data, 'toGroup');
141
        $to = ArrayUtils::get($data, 'to', ArrayUtils::get($data, 'toGroup', []));
142
143
        if (!is_array($to)) {
144
            $to = explode(',', $to);
145
        }
146
147
        $toIds = array_map(function($id) use ($isGroup) {
148
            return sprintf('%s_%s', ($isGroup ? 1 : 0), $id);
149
        }, $to);
150
151
        return implode(',', $toIds);
152
    }
153
}