Completed
Push — d64 ( 715811...47f128 )
by Welling
03:02
created

AbstractClient::parseFile()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 2
nop 1
dl 0
loc 12
ccs 0
cts 8
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 parseOnDirectusFiles($data)
84
    {
85
        // @TODO: omit columns such id or user.
86
        $data = ArrayUtils::omit($data, ['id', 'user']);
87
88
        return $data;
89
    }
90
}