Json::InsertData()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 7
c 1
b 0
f 1
nc 2
nop 3
dl 0
loc 9
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace DyarWeb\DB;
5
6
/**
7
 * Class Json
8
 * @package DyarWeb
9
 */
10
class Json
11
{
12
    private $DBDir;
13
14
    public function __construct()
15
    {
16
        $this->DBDir = getenv('jsonDBDir');
17
    }
18
19
    /**
20
     * @param $DBName
21
     * @param $TableName
22
     * @return bool
23
     */
24
    public function DeleteTable($DBName, $TableName)
25
    {
26
        return unlink($this->DBDir . '/' . $DBName . '/' . $TableName . '.json');
27
    }
28
29
    /**
30
     * @param $DBName
31
     * @return bool
32
     */
33
    public function DeleteDB($DBName)
34
    {
35
        $files = array_diff(scandir($this->DBDir . '/' . $DBName), ['.', '..']);
0 ignored issues
show
Bug introduced by
It seems like scandir($this->DBDir . '/' . $DBName) can also be of type false; however, parameter $array1 of array_diff() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
        $files = array_diff(/** @scrutinizer ignore-type */ scandir($this->DBDir . '/' . $DBName), ['.', '..']);
Loading history...
36
        foreach ($files as $file) {
37
            (is_dir($this->DBDir . '/' . $DBName . '/' . $file)) ? $this->DeleteDB($this->DBDir . '/' . $DBName . '/' . $file) : unlink($this->DBDir . '/' . $DBName . '/' . $file);
38
        }
39
        return rmdir($this->DBDir . '/' . $DBName);
40
    }
41
42
    /**
43
     * @param string $DBName
44
     * @param string $TableName
45
     * @param array $data
46
     * @return false|int
47
     */
48
    public function InsertData($DBName, $TableName, array $data)
49
    {
50
        $fileName = $this->DBDir . '/' . $DBName . '/' . $TableName . '.json';
51
        if (!is_file($fileName)) {
52
            return $this->CreateTable($DBName, $TableName, $data);
53
        } else {
54
            $out = json_decode(file_get_contents($fileName));
55
            $out[] = $data;
56
            return file_put_contents($fileName, json_encode($out));
57
        }
58
    }
59
60
    /**
61
     * @param string $DBName
62
     * @param string $TableName
63
     * @param array $columns
64
     * @param int|string $mode
65
     * @return false|int
66
     */
67
    public function CreateTable($DBName, $TableName, array $columns, $mode = 0600)
68
    {
69
        if (!is_dir($DBName)) {
70
            $this->CreateDB($DBName, 0700);
71
        }
72
        $json = json_encode([$columns]);
73
        $fileName = $this->DBDir . '/' . $DBName . '/' . $TableName . '.json';
74
        $res = file_put_contents($fileName, $json);
75
        $res .= chmod($fileName, $mode);
0 ignored issues
show
Bug introduced by
It seems like $mode can also be of type string; however, parameter $mode of chmod() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
        $res .= chmod($fileName, /** @scrutinizer ignore-type */ $mode);
Loading history...
76
        return $res;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $res returns the type string which is incompatible with the documented return type false|integer.
Loading history...
77
    }
78
79
    /**
80
     * @param string $DBName
81
     * @param int $mode
82
     * @return bool
83
     */
84
    public function CreateDB($DBName, int $mode = 0700)
85
    {
86
        if (!is_dir($this->DBDir)) {
87
            mkdir($this->DBDir, 0700);
88
        }
89
        $Folders = explode('/', $DBName);
90
        $dir = '';
91
        $return = '';
92
        foreach ($Folders as $folder) {
93
            $dir .= '/' . $folder;
94
            if (!is_dir($this->DBDir . $dir)) {
95
                $return .= mkdir($this->DBDir . $dir, $mode);
96
            }
97
        }
98
        return $return;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $return returns the type string which is incompatible with the documented return type boolean.
Loading history...
99
    }
100
101
    /**
102
     * @param string $DBName
103
     * @param string $TableName
104
     * @param array $data
105
     * @param array $where
106
     * @return false|int
107
     */
108
    public function UpdateData($DBName, $TableName, array $data, array $where)
109
    {
110
        $fileName = $this->DBDir . '/' . $DBName . '/' . $TableName . '.json';
111
        $out = json_decode(file_get_contents($fileName));
112
        foreach ($out as $item) {
113
            $res = true;
114
            foreach ($where as $key => $value) {
115
                if ($item->$key != $value || !$item->$key) {
116
                    $res = false;
117
                    break;
118
                }
119
            }
120
            if ($res) {
121
                foreach ($data as $key => $value) {
122
                    $item->$key = $value;
123
                }
124
            }
125
        }
126
        return file_put_contents($fileName, json_encode($out));
127
    }
128
129
    /**
130
     * @param string $DBName
131
     * @param string $TableName
132
     * @param array|null $where
133
     * @return false|int
134
     */
135
    public function DeleteData($DBName, $TableName, array $where)
136
    {
137
        $fileName = $this->DBDir . '/' . $DBName . '/' . $TableName . '.json';
138
        if (is_file($fileName)) {
139
            $out = json_decode(file_get_contents($fileName));
140
            $i=-1;
141
            foreach ($out as $item) {
142
                $i++;
143
                $res = true;
144
                foreach ($where as $key => $value) {
145
                    if ($item->$key != $value || !$item->$key) {
146
                        $res = false;
147
                        break;
148
                    }
149
                }
150
                if ($res) {
151
                    unset($out[$i]);
152
                }
153
            }
154
            $out = array_values($out);
155
            return file_put_contents($fileName, json_encode($out));
156
        } else {
157
            return false;
158
        }
159
    }
160
161
    /**
162
     * @param string $DBName
163
     * @param string $TableName
164
     * @param null $where
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $where is correct as it would always require null to be passed?
Loading history...
165
     * @param null $limit
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $limit is correct as it would always require null to be passed?
Loading history...
166
     * @return array|mixed
167
     */
168
    public function SelectData($DBName, $TableName, array $where = null, int $limit = null)
169
    {
170
        $fileName = $this->DBDir . '/' . $DBName . '/' . $TableName . '.json';
171
        if (is_file($fileName)) {
172
            $out = json_decode(file_get_contents($fileName));
173
            if ($where == null) {
174
                return $out;
175
            } else {
176
                $items = [];
177
                foreach ($out as $item) {
178
                    $res = true;
179
                    foreach ($where as $key => $value) {
180
                        if ($item->$key != $value || !$item->$key) {
181
                            $res = false;
182
                            break;
183
                        }
184
                    }
185
                    if ($res) {
186
                        $items[] = $item;
187
                    }
188
                    if ($limit != null && count($items) >= $limit) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $limit of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
189
                        break;
190
                    }
191
                }
192
                if (count($items) == 1) {
193
                    return (object)$items[0];
194
                } elseif (empty($items)) {
195
                    return false;
196
                } else {
197
                    return (object)$items;
198
                }
199
            }
200
        } else {
201
            return false;
202
        }
203
    }
204
}
205