JSON::init()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * Copyright (c) 2018 VectorNetworkProject. All rights reserved. MIT license.
4
 *
5
 * GitHub: https://github.com/VectorNetworkProject/TheMix
6
 * Website: https://www.vector-network.tk
7
 */
8
9
namespace VectorNetworkProject\TheMix\provider;
10
11
use pocketmine\utils\Config;
12
use pocketmine\utils\MainLogger;
13
use VectorNetworkProject\TheMix\lib\database\Provider;
14
15
class JSON extends Provider
16
{
17
    /* @var string $path */
18
    private $path;
19
20
    /* @var string $file */
21
    private $file;
22
23
    /**
24
     * JSON constructor.
25
     *
26
     * @param string $xuid
27
     * @param string $file
28
     */
29
    public function __construct(string $xuid, string $file)
30
    {
31
        $this->path = self::getPath('datas', 'json').$xuid.'/';
32
        $this->file = $file.'.json';
33
    }
34
35
    /**
36
     * @param array $data
37
     */
38
    public function init(array $data = []): void
39
    {
40
        if (!$this->hasTable()) {
41
            $this->createTable($data);
42
        }
43
    }
44
45
    /**
46
     * @param array $table
47
     *
48
     * @return void
49
     */
50
    public function createTable(array $table = []): void
51
    {
52
        @mkdir($this->path, 0755, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
53
        $config = new Config($this->path.$this->file, Config::JSON, $table);
54
        $config->save();
55
        MainLogger::getLogger()->debug('[PROVIDER] Create '.$this->file);
56
    }
57
58
    /**
59
     * @return bool
60
     */
61
    public function hasTable(): bool
62
    {
63
        return file_exists($this->path.$this->file)
64
            ? true
65
            : false;
66
    }
67
68
    /**
69
     * @return void
70
     */
71
    public function deleteTable(): void
72
    {
73
        unlink($this->path.$this->file);
74
    }
75
76
    /**
77
     * @param string     $key
78
     * @param bool|mixed $data
79
     *
80
     * @return void
81
     */
82
    public function set(string $key, $data): void
83
    {
84
        $config = new Config($this->path.$this->file, Config::JSON);
85
        $config->set($key, $data);
86
        $config->save();
87
    }
88
89
    /**
90
     * @param string $key
91
     *
92
     * @return bool|mixed
93
     */
94
    public function get(string $key)
95
    {
96
        $config = new Config($this->path.$this->file, Config::JSON);
97
98
        return $config->get($key);
99
    }
100
101
    /**
102
     * @return array
103
     */
104
    public function getAll(): array
105
    {
106
        $config = new Config($this->path.$this->file, Config::JSON);
107
108
        return $config->getAll();
109
    }
110
111
    /**
112
     * @return array
113
     */
114
    public function getKeys(): array
115
    {
116
        $config = new Config($this->path.$this->file, Config::JSON);
117
118
        return $config->getAll(true);
119
    }
120
121
    /**
122
     * @param string $key
123
     *
124
     * @return bool
125
     */
126 View Code Duplication
    public function exists(string $key): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
Coding Style introduced by
function exists() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
127
    {
128
        $config = new Config($this->path.$this->file, Config::JSON);
129
130
        return $config->exists($key)
131
            ? true
132
            : false;
133
    }
134
135
    /**
136
     * @param string $key
137
     *
138
     * @return void
139
     */
140
    public function remove(string $key): void
141
    {
142
        $config = new Config($this->path.$this->file, Config::JSON);
143
        $config->remove($key);
144
    }
145
}
146