Completed
Push — master ( 495e06...161604 )
by Welling
02:05
created

Client::createLocalClient()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 42
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 8.439
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 6
eloc 25
nc 32
nop 1
crap 42
1
<?php
2
3
namespace Directus\SDK;
4
5
use Directus\Database\Connection;
6
use Directus\Util\ArrayUtils;
7
use Directus\Database\TableSchema;
8
9
class Client
10 2
{
11
    /**
12 2
     * @var Client
13
     */
14
    protected static $instance = null;
15 4
16
    /**
17 4
     * @var array
18
     */
19
    protected $defaultConfig = [
20 2
        'environment' => 'development',
21
        'database' => [
22 2
            'driver' => 'pdo_mysql'
23
        ],
24
        'status' => [
25 2
            'column_name' => 'active',
26
            'deleted_value' => 0,
27 2
            'active_value' => 1,
28
            'draft_value' => 2,
29
            'mapping' => [
30 2
                0 => [
31
                    'name' => 'Delete',
32 2
                    'color' => '#C1272D',
33
                    'sort' => 3
34
                ],
35 2
                1 => [
36
                    'name' => 'Active',
37 2
                    'color' => '#5B5B5B',
38
                    'sort' => 1
39
                ],
40 2
                2 => [
41
                    'name' => 'Draft',
42 2
                    'color' => '#BBBBBB',
43
                    'sort' => 2
44
                ]
45 2
            ]
46
        ]
47 2
    ];
48
49
    /**
50 2
     * @param $userToken
51
     * @param array $options
52 2
     *
53
     * @return RequestsInterface
54
     */
55 2
    public static function create($userToken, $options = [])
56
    {
57 2
        if (static::$instance == null) {
58
            static::$instance = new static;
59
        }
60 2
61
        if (!is_array($userToken)) {
62 2
            $newClient = static::$instance->createRemoteClient($userToken, $options);
63
        } else {
64
            $options = $userToken;
65 2
            $newClient = static::$instance->createLocalClient($options);
66
        }
67 2
68
        return $newClient;
69
    }
70 2
71
    /**
72 2
     * Creates a new local client instance
73
     *
74
     * @param array $options
75 2
     *
76
     * @return ClientLocal
77
     */
78
    public function createLocalClient(array $options)
79
    {
80
        $options = ArrayUtils::defaults($this->defaultConfig, $options);
0 ignored issues
show
Bug introduced by
The method defaults() 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...
81
        $dbConfig = ArrayUtils::get($options, 'database', []);
82
83
        $config = ArrayUtils::omit($options, 'database');
84
        // match the actual directus status mapping config key
85
        $config['statusMapping'] = ArrayUtils::get($config, 'status.mapping');
86
        unset($config['status']['mapping']);
87
88
        if (!defined('STATUS_DELETED_NUM')) {
89
            define('STATUS_DELETED_NUM', ArrayUtils::get($config, 'status.deleted_value', 0));
90
        }
91
92
        if (!defined('STATUS_ACTIVE_NUM')) {
93
            define('STATUS_ACTIVE_NUM', ArrayUtils::get($config, 'status.active_value', 1));
94
        }
95
96
        if (!defined('STATUS_DRAFT_NUM')) {
97
            define('STATUS_DRAFT_NUM', ArrayUtils::get($config, 'status.draft_value', 2));
98
        }
99
100
        if (!defined('STATUS_COLUMN_NAME')) {
101
            define('STATUS_COLUMN_NAME', ArrayUtils::get($config, 'status.column_name', 'active'));
102
        }
103
104
        if (!defined('DIRECTUS_ENV')) {
105
            define('DIRECTUS_ENV', ArrayUtils::get($config, 'environment', 'development'));
106
        }
107
108
        $connection = new Connection($dbConfig);
109
        $connection->connect();
110
111
        $schema = new \Directus\Database\Schemas\Sources\MySQLSchema($connection);
112
        $schema = new \Directus\Database\SchemaManager($schema);
113
        TableSchema::setSchemaManagerInstance($schema);
114
        TableSchema::setAclInstance(false);
115
        TableSchema::setConnectionInstance($connection);
116
        TableSchema::setConfig($config);
117
118
        return new ClientLocal($connection);
119
    }
120
121
    /**
122
     * Create a new remote client instance
123
     *
124
     * @param $userToken
125
     * @param array $options
126
     *
127
     * @return ClientRemote
128
     */
129
    public function createRemoteClient($userToken, array $options = [])
130
    {
131
        return new ClientRemote($userToken, $options);
132
    }
133
}