YAML::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

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 1
nc 1
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 VectorNetworkProject\TheMix\lib\database\Provider;
13
14
class YAML extends Provider
15
{
16
    /* @var string $path */
17
    private $path;
18
19
    /* @var string $file */
20
    private $file;
21
22
    /**
23
     * JSON constructor.
24
     *
25
     * @param string $file
26
     */
27
    public function __construct(string $file = 'config')
28
    {
29
        $this->path = self::getPath('configs', 'yaml');
30
        $this->file = $file.'.yml';
31
    }
32
33
    /**
34
     * @param array $data
35
     */
36
    public function init(array $data = []): void
37
    {
38
        if (!$this->hasTable()) {
39
            $this->createTable($data);
40
        }
41
    }
42
43
    /**
44
     * @param array $table
45
     *
46
     * @return void
47
     */
48
    public function createTable(array $table = []): void
49
    {
50
        @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...
51
        $config = new Config($this->path.$this->file, Config::YAML, $table);
52
        $config->save();
53
    }
54
55
    /**
56
     * @return bool
57
     */
58
    public function hasTable(): bool
59
    {
60
        return file_exists($this->path.$this->file)
61
            ? true
62
            : false;
63
    }
64
65
    /**
66
     * @return bool
67
     */
68
    public function deleteTable(): bool
0 ignored issues
show
Coding Style introduced by
function deleteTable() 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...
69
    {
70
        return unlink($this->path.$this->file)
71
            ? true
72
            : false;
73
    }
74
75
    /**
76
     * @param string     $key
77
     * @param bool|mixed $data
78
     *
79
     * @return void
80
     */
81
    public function set(string $key, $data): void
82
    {
83
        $config = new Config($this->path.$this->file, Config::YAML);
84
        $config->set($key, $data);
85
        $config->save();
86
    }
87
88
    /**
89
     * @param string $key
90
     *
91
     * @return mixed
92
     */
93
    public function get(string $key)
94
    {
95
        $config = new Config($this->path.$this->file, Config::YAML);
96
97
        return $config->get($key);
98
    }
99
100
    /**
101
     * @return array
102
     */
103
    public function getAll(): array
104
    {
105
        $config = new Config($this->path.$this->file, Config::YAML);
106
107
        return $config->getAll();
108
    }
109
110
    /**
111
     * @return array
112
     */
113
    public function getKeys(): array
114
    {
115
        $config = new Config($this->path.$this->file, Config::YAML);
116
117
        return $config->getAll(true);
118
    }
119
120
    /**
121
     * @param string $key
122
     *
123
     * @return bool
124
     */
125 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...
126
    {
127
        $config = new Config($this->path.$this->file, Config::YAML);
128
129
        return $config->exists($key)
130
            ? true
131
            : false;
132
    }
133
134
    /**
135
     * @param string $key
136
     *
137
     * @return void
138
     */
139
    public function remove(string $key): void
140
    {
141
        $config = new Config($this->path.$this->file, Config::YAML);
142
        $config->remove($key);
143
    }
144
}
145