OnlyofficeAppsettings   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 74
c 3
b 0
f 0
dl 0
loc 182
rs 10
wmc 30

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getLinkToDocs() 0 3 1
F getSetting() 0 62 21
A setSetting() 0 29 5
A getServerUrl() 0 3 1
A isSettingUrl() 0 3 1
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
18
use Chamilo\CoreBundle\Entity\AccessUrlRelPlugin;
19
use Chamilo\CoreBundle\Entity\Plugin as PluginEntity;
20
use Chamilo\CoreBundle\Framework\Container;
21
use Doctrine\ORM\Exception\NotSupported;
22
use Doctrine\ORM\Exception\ORMException;
23
use Doctrine\ORM\OptimisticLockException;
24
use Onlyoffice\DocsIntegrationSdk\Manager\Settings\SettingsManager;
25
26
class OnlyofficeAppsettings extends SettingsManager
27
{
28
    /**
29
     * Link to Docs Cloud.
30
     *
31
     * @var string
32
     */
33
    public const LINK_TO_DOCS = 'https://www.onlyoffice.com/docs-registration.aspx?referer=chamilo';
34
    /**
35
     * The settings key for the document server address.
36
     *
37
     * @var string
38
     */
39
    public $documentServerUrl = 'document_server_url';
40
41
    /**
42
     * The config key for the jwt header.
43
     *
44
     * @var string
45
     */
46
    public $jwtHeader = 'onlyoffice_jwt_header';
47
48
    /**
49
     * The config key for the internal url.
50
     *
51
     * @var string
52
     */
53
    public $documentServerInternalUrl = 'onlyoffice_internal_url';
54
55
    /**
56
     * The config key for the storage url.
57
     *
58
     * @var string
59
     */
60
    public $storageUrl = 'onlyoffice_storage_url';
61
62
    /**
63
     * The config key for the demo data.
64
     *
65
     * @var string
66
     */
67
    public $useDemoName = 'onlyoffice_connect_demo_data';
68
69
    /**
70
     * Chamilo plugin.
71
     */
72
    public $plugin;
73
74
    public $newSettings;
75
76
    /**
77
     * The config key for JWT secret key.
78
     *
79
     * @var string
80
     */
81
    protected $jwtKey = 'jwt_secret';
82
83
    public function __construct(Plugin $plugin, ?array $newSettings = null)
84
    {
85
        parent::__construct();
86
        $this->plugin = $plugin;
87
        $this->newSettings = $newSettings;
88
    }
89
90
    public function getSetting($settingName)
91
    {
92
        $plugin = Database::getManager()->getRepository(PluginEntity::class)->findOneBy(['title' => 'Onlyoffice']);
93
        $configuration = $plugin?->getConfigurationsByAccessUrl(
94
            Container::getAccessUrlUtil()->getCurrent()
95
        );
96
97
        $value = null;
98
        if (null !== $this->newSettings) {
99
            if (isset($this->newSettings[$settingName])) {
100
                $value = $this->newSettings[$settingName];
101
            }
102
103
            if (empty($value)) {
104
                $prefix = $this->plugin->get_name();
105
106
                if (substr($settingName, 0, strlen($prefix)) == $prefix) {
107
                    $settingNameWithoutPrefix = substr($settingName, strlen($prefix) + 1);
108
                }
109
110
                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...
111
                    $value = $this->newSettings[$settingNameWithoutPrefix];
112
                }
113
            }
114
            if ($this->isSettingUrl($value)) {
115
                $value = $this->processUrl($value);
116
            }
117
            if (!empty($value)) {
118
                return $value;
119
            }
120
        }
121
        switch ($settingName) {
122
            case $this->jwtHeader:
123
                $settings = api_get_setting($settingName);
124
                $value = is_array($settings) && array_key_exists($this->plugin->get_name(), $settings)
125
                    ? $settings[$this->plugin->get_name()]
126
                    : null;
127
128
                if (empty($value)) {
129
                    $value = 'Authorization';
130
                }
131
                break;
132
            case $this->documentServerInternalUrl:
133
                $settings = api_get_setting($settingName);
134
                $value = is_array($settings) ? ($settings[$this->plugin->get_name()] ?? null) : null;
135
                break;
136
            case $this->useDemoName:
137
                $value = $configuration ? ($configuration[$settingName] ?: null) : null;
138
                break;
139
            case $this->jwtPrefix:
140
                $value = 'Bearer ';
141
                break;
142
            default:
143
                if (!empty($this->plugin) && method_exists($this->plugin, 'get')) {
144
                    $value = $this->plugin->get($settingName);
145
                }
146
        }
147
        if (empty($value)) {
148
            $value = api_get_configuration_value($settingName);
149
        }
150
151
        return $value;
152
    }
153
154
    /**
155
     * @throws OptimisticLockException
156
     * @throws NotSupported
157
     * @throws ORMException
158
     */
159
    public function setSetting($settingName, $value, $createSetting = false): void
160
    {
161
        if (($settingName === $this->useDemoName) && $createSetting) {
162
            $em = Database::getManager();
163
164
            $pluginConfig = $em->getRepository(PluginEntity::class)
165
                ->findOneBy(['title' => 'Onlyoffice'])
166
                ?->getConfigurationsByAccessUrl(
167
                    Container::getAccessUrlUtil()->getCurrent()
168
                )
169
            ;
170
171
            if ($pluginConfig) {
172
                $settings = $pluginConfig->getConfiguration();
173
                $settings[$this->useDemoName] = $value;
174
175
                $pluginConfig->setConfiguration($settings);
176
177
                $em->flush();
178
            }
179
180
            return;
181
        }
182
183
        $prefix = $this->plugin->get_name();
184
        if (!(substr($settingName, 0, strlen($prefix)) == $prefix)) {
185
            $settingName = $prefix.'_'.$settingName;
186
        }
187
        api_set_setting($settingName, $value);
188
    }
189
190
    public function getServerUrl()
191
    {
192
        return api_get_path(WEB_PATH);
193
    }
194
195
    /**
196
     * Get link to Docs Cloud.
197
     *
198
     * @return string
199
     */
200
    public function getLinkToDocs()
201
    {
202
        return self::LINK_TO_DOCS;
203
    }
204
205
    public function isSettingUrl($settingName)
206
    {
207
        return in_array($settingName, [$this->documentServerUrl, $this->documentServerInternalUrl, $this->storageUrl]);
208
    }
209
}
210