FileConfig   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 50
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getValue() 0 4 1
A get() 0 4 1
A getContent() 0 6 2
A getFileName() 0 4 1
A setValue() 0 6 1
A set() 0 4 1
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
}