FileConfig::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: akeinhell
5
 * Date: 23.09.16
6
 * Time: 18:26
7
 */
8
9
namespace Telegram\Config;
10
11
12
class FileConfig extends BaseConfig
13
{
14
    /**
15
     * @var
16
     */
17
    private $path;
18
19
    /**
20
     * FileConfig constructor.
21
     * @param $path
22
     */
23
    public function __construct($path)
24
    {
25
        $this->path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
26
    }
27
28
    public function getValue($chatId, $value, $default)
29
    {
30
        return array_get($this->get($chatId), $value, $default);
31
    }
32
33
    public function get($chatId)
34
    {
35
        return $this->getContent($chatId);
36
    }
37
38
    private function getContent($chatId)
39
    {
40
        $file = $this->getFileName($chatId);
41
42
        return file_exists($file) ? \GuzzleHttp\json_decode(file_get_contents($file)) : [];
43
    }
44
45
    private function getFileName($chatId)
46
    {
47
        return $this->path . 'c' . $chatId;
48
    }
49
50
    public function setValue($chatId, $key, $value)
51
    {
52
        $data       = $this->get($chatId);
53
        $data[$key] = $value;
54
        $this->set($chatId, $data);
55
    }
56
57
    public function set($chatId, array $params)
58
    {
59
        file_put_contents($this->getFileName($chatId), \GuzzleHttp\json_encode($params));
60
    }
61
}