Setting::setSettingFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace CSlant\TelegramGitNotifier\Models;
4
5
use CSlant\TelegramGitNotifier\Constants\SettingConstant;
6
7
class Setting
8
{
9
    private array $settings = [];
10
11
    private string $settingFile = '';
12
13
    /**
14
     * @return array
15
     */
16
    public function getSettings(): array
17
    {
18
        return $this->settings;
19
    }
20
21
    /**
22
     * @param string $settingFile
23
     *
24
     * @return void
25
     */
26
    public function setSettingFile(string $settingFile): void
27
    {
28
        $this->settingFile = $settingFile;
29
    }
30
31
    /**
32
     * @return string
33
     */
34
    public function getSettingFile(): string
35
    {
36
        return $this->settingFile;
37
    }
38
39
    /**
40
     * Set settings
41
     *
42
     * @return void
43
     */
44
    public function setSettingConfig(): void
45
    {
46
        $json = file_get_contents($this->settingFile);
47
48
        if (!empty($json)) {
49
            $this->settings = json_decode($json, true);
50
        }
51
    }
52
53
    /**
54
     * @return bool
55
     */
56
    public function isAllEventsNotification(): bool
57
    {
58
        if (!empty($this->settings)
59
            && $this->settings[SettingConstant::T_ALL_EVENTS_NOTIFICATION] === true
60
        ) {
61
            return true;
62
        }
63
64
        return false;
65
    }
66
67
    /**
68
     * Condition for checking the notification status
69
     *
70
     * @return bool
71
     */
72
    public function isNotified(): bool
73
    {
74
        if (!empty($this->settings)
75
            && $this->settings[SettingConstant::T_IS_NOTIFIED] === true
76
        ) {
77
            return true;
78
        }
79
80
        return false;
81
    }
82
83
    /**
84
     * Update setting item value and save to file
85
     *
86
     * @param string $settingName
87
     * @param array|string|bool|int|null $settingValue
88
     *
89
     * @return bool
90
     */
91
    public function updateSetting(
92
        string $settingName,
93
        mixed $settingValue = null
94
    ): bool {
95
        $settingKeys = explode('.', $settingName);
96
        $lastKey = array_pop($settingKeys);
97
        $nestedSettings = &$this->settings;
98
99
        foreach ($settingKeys as $key) {
100
            if (!isset($nestedSettings[$key])
101
                || !is_array($nestedSettings[$key])
102
            ) {
103
                return false;
104
            }
105
            $nestedSettings = &$nestedSettings[$key];
106
        }
107
108
        if (isset($nestedSettings[$lastKey])) {
109
            $newValue = $settingValue ?? !$nestedSettings[$lastKey];
110
            $nestedSettings[$lastKey] = $newValue;
111
112
            return $this->saveSettingsToFile();
113
        }
114
115
        return false;
116
    }
117
118
    /**
119
     * Save settings to json file
120
     *
121
     * @return bool
122
     */
123
    private function saveSettingsToFile(): bool
124
    {
125
        if (file_exists($this->settingFile)) {
126
            $json = json_encode($this->settings, JSON_PRETTY_PRINT);
127
            file_put_contents($this->settingFile, $json, LOCK_EX);
128
129
            return true;
130
        }
131
132
        return false;
133
    }
134
}
135