Passed
Push — master ( 262b3f...86674d )
by Yannick
09:44
created

OnlyofficeAppsettings::setSetting()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 13
rs 10
1
<?php
2
/**
3
 * (c) Copyright Ascensio System SIA 2025.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
use Onlyoffice\DocsIntegrationSdk\Manager\Settings\SettingsManager;
18
19
class OnlyofficeAppsettings extends SettingsManager
20
{
21
    /**
22
     * Link to Docs Cloud.
23
     *
24
     * @var string
25
     */
26
    public const LINK_TO_DOCS = 'https://www.onlyoffice.com/docs-registration.aspx?referer=chamilo';
27
    /**
28
     * The settings key for the document server address.
29
     *
30
     * @var string
31
     */
32
    public $documentServerUrl = 'document_server_url';
33
34
    /**
35
     * The config key for the jwt header.
36
     *
37
     * @var string
38
     */
39
    public $jwtHeader = 'onlyoffice_jwt_header';
40
41
    /**
42
     * The config key for the internal url.
43
     *
44
     * @var string
45
     */
46
    public $documentServerInternalUrl = 'onlyoffice_internal_url';
47
48
    /**
49
     * The config key for the storage url.
50
     *
51
     * @var string
52
     */
53
    public $storageUrl = 'onlyoffice_storage_url';
54
55
    /**
56
     * The config key for the demo data.
57
     *
58
     * @var string
59
     */
60
    public $useDemoName = 'onlyoffice_connect_demo_data';
61
62
    /**
63
     * Chamilo plugin.
64
     */
65
    public $plugin;
66
67
    public $newSettings;
68
69
    /**
70
     * The config key for JWT secret key.
71
     *
72
     * @var string
73
     */
74
    protected $jwtKey = 'jwt_secret';
75
76
    public function __construct(Plugin $plugin, ?array $newSettings = null)
77
    {
78
        parent::__construct();
79
        $this->plugin = $plugin;
80
        $this->newSettings = $newSettings;
81
    }
82
83
    public function getSetting($settingName)
84
    {
85
        $value = null;
86
        if (null !== $this->newSettings) {
87
            if (isset($this->newSettings[$settingName])) {
88
                $value = $this->newSettings[$settingName];
89
            }
90
91
            if (empty($value)) {
92
                $prefix = $this->plugin->getPluginName();
93
94
                if (substr($settingName, 0, strlen($prefix)) == $prefix) {
95
                    $settingNameWithoutPrefix = substr($settingName, strlen($prefix) + 1);
96
                }
97
98
                if (isset($this->newSettings[$settingNameWithoutPrefix])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $settingNameWithoutPrefix does not seem to be defined for all execution paths leading up to this point.
Loading history...
99
                    $value = $this->newSettings[$settingNameWithoutPrefix];
100
                }
101
            }
102
            if ($this->isSettingUrl($value)) {
103
                $value = $this->processUrl($value);
104
            }
105
            if (!empty($value)) {
106
                return $value;
107
            }
108
        }
109
        switch ($settingName) {
110
            case $this->jwtHeader:
111
                $settings = api_get_setting($settingName);
112
                $value = is_array($settings) && array_key_exists($this->plugin->getPluginName(), $settings)
113
                    ? $settings[$this->plugin->getPluginName()]
114
                    : null;
115
116
                if (empty($value)) {
117
                    $value = 'Authorization';
118
                }
119
                break;
120
            case $this->documentServerInternalUrl:
121
                $settings = api_get_setting($settingName);
122
                $value = is_array($settings) ? ($settings[$this->plugin->getPluginName()] ?? null) : null;
123
                break;
124
            case $this->useDemoName:
125
                $settings = api_get_setting($settingName);
126
                $value = is_array($settings) ? ($settings[0] ?? null) : null;
127
                break;
128
            case $this->jwtPrefix:
129
                $value = 'Bearer ';
130
                break;
131
            default:
132
                if (!empty($this->plugin) && method_exists($this->plugin, 'get')) {
133
                    $value = $this->plugin->get($settingName);
134
                }
135
        }
136
        if (empty($value)) {
137
            $value = api_get_configuration_value($settingName);
138
        }
139
140
        return $value;
141
    }
142
143
    public function setSetting($settingName, $value, $createSetting = false)
144
    {
145
        if (($settingName === $this->useDemoName) && $createSetting) {
146
            api_add_setting($value, $settingName, null, 'setting', 'Plugins');
147
148
            return;
149
        }
150
151
        $prefix = $this->plugin->getPluginName();
152
        if (!(substr($settingName, 0, strlen($prefix)) == $prefix)) {
153
            $settingName = $prefix.'_'.$settingName;
154
        }
155
        api_set_setting($settingName, $value);
156
    }
157
158
    public function getServerUrl()
159
    {
160
        return api_get_path(WEB_PATH);
161
    }
162
163
    /**
164
     * Get link to Docs Cloud.
165
     *
166
     * @return string
167
     */
168
    public function getLinkToDocs()
169
    {
170
        return self::LINK_TO_DOCS;
171
    }
172
173
    public function isSettingUrl($settingName)
174
    {
175
        return in_array($settingName, [$this->documentServerUrl, $this->documentServerInternalUrl, $this->storageUrl]);
176
    }
177
}
178