| Total Complexity | 46 |
| Total Lines | 289 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like OnlyofficePlugin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use OnlyofficePlugin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class OnlyofficePlugin extends Plugin implements HookPluginInterface |
||
| 26 | { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * OnlyofficePlugin name. |
||
| 30 | */ |
||
| 31 | private $pluginName = "onlyoffice"; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * OnlyofficePlugin constructor. |
||
| 35 | */ |
||
| 36 | protected function __construct() |
||
| 37 | { |
||
| 38 | parent::__construct( |
||
| 39 | "1.3.0", |
||
| 40 | "Asensio System SIA", |
||
| 41 | [ |
||
| 42 | "enable_onlyoffice_plugin" => "boolean", |
||
| 43 | "document_server_url" => "text", |
||
| 44 | "jwt_secret" => "text", |
||
| 45 | "jwt_header" => "text", |
||
| 46 | "document_server_internal" => "text", |
||
| 47 | "storage_url" => "text" |
||
| 48 | ] |
||
| 49 | ); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Create OnlyofficePlugin object |
||
| 54 | */ |
||
| 55 | public static function create(): OnlyofficePlugin |
||
| 56 | { |
||
| 57 | static $result = null; |
||
| 58 | |||
| 59 | return $result ?: $result = new self(); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * This method install the plugin tables. |
||
| 64 | */ |
||
| 65 | public function install() |
||
| 66 | { |
||
| 67 | $this->installHook(); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * This method drops the plugin tables. |
||
| 72 | */ |
||
| 73 | public function uninstall() |
||
| 74 | { |
||
| 75 | $this->uninstallHook(); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Install the "create" hooks. |
||
| 80 | */ |
||
| 81 | public function installHook() |
||
| 82 | { |
||
| 83 | $itemActionObserver = OnlyofficeItemActionObserver::create(); |
||
| 84 | HookDocumentItemAction::create()->attach($itemActionObserver); |
||
| 85 | |||
| 86 | $actionObserver = OnlyofficeActionObserver::create(); |
||
| 87 | HookDocumentAction::create()->attach($actionObserver); |
||
| 88 | |||
| 89 | $viewObserver = OnlyofficeItemViewObserver::create(); |
||
| 90 | HookDocumentItemView::create()->attach($viewObserver); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Uninstall the "create" hooks. |
||
| 95 | */ |
||
| 96 | public function uninstallHook() |
||
| 97 | { |
||
| 98 | $itemActionObserver = OnlyofficeItemActionObserver::create(); |
||
| 99 | HookDocumentItemAction::create()->detach($itemActionObserver); |
||
| 100 | |||
| 101 | $actionObserver = OnlyofficeActionObserver::create(); |
||
| 102 | HookDocumentAction::create()->detach($actionObserver); |
||
| 103 | |||
| 104 | $viewObserver = OnlyofficeItemViewObserver::create(); |
||
| 105 | HookDocumentItemView::create()->detach($viewObserver); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Get status of demo server |
||
| 110 | * |
||
| 111 | * @return bool |
||
| 112 | */ |
||
| 113 | public function useDemo() { |
||
| 114 | return $this->getDemoData()["enabled"] === true; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Get demo data |
||
| 119 | * |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | public function getDemoData() { |
||
| 123 | $data = api_get_setting('onlyoffice_connect_demo_data')[0]; |
||
| 124 | |||
| 125 | if (empty($data)) { |
||
| 126 | $data = [ |
||
| 127 | "available" => true, |
||
| 128 | "enabled" => false |
||
| 129 | ]; |
||
| 130 | api_add_setting(json_encode($data), 'onlyoffice_connect_demo_data', null, 'setting', 'Plugins'); |
||
| 131 | return $data; |
||
| 132 | } |
||
| 133 | $data = json_decode($data, true); |
||
| 134 | |||
| 135 | if (isset($data['start'])) { |
||
| 136 | $overdue = $data["start"]; |
||
| 137 | $overdue += 24*60*60*AppConfig::GetDemoParams()["TRIAL"]; |
||
| 138 | if ($overdue > time()) { |
||
| 139 | $data["available"] = true; |
||
| 140 | $data["enabled"] = $data["enabled"] === true; |
||
| 141 | } else { |
||
| 142 | $data["available"] = false; |
||
| 143 | $data["enabled"] = false; |
||
| 144 | api_set_setting('onlyoffice_connect_demo_data', json_encode($data)); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | return $data; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Switch on demo server |
||
| 152 | * |
||
| 153 | * @param bool $value - select demo |
||
| 154 | * |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | public function selectDemo($value) { |
||
| 158 | $data = $this->getDemoData(); |
||
| 159 | |||
| 160 | if ($value === true && !$data["available"]) { |
||
| 161 | return false; |
||
| 162 | } |
||
| 163 | |||
| 164 | $data["enabled"] = $value === true; |
||
| 165 | |||
| 166 | if (!isset($data["start"])) { |
||
| 167 | $data["start"] = time(); |
||
| 168 | } |
||
| 169 | api_set_setting('onlyoffice_connect_demo_data', json_encode($data)); |
||
| 170 | return true; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get the document server url |
||
| 175 | * |
||
| 176 | * @param bool $origin - take origin |
||
| 177 | * |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | public function getDocumentServerUrl($origin = false) |
||
| 181 | { |
||
| 182 | if (!$origin && $this->useDemo()) { |
||
| 183 | return AppConfig::GetDemoParams()["ADDR"]; |
||
| 184 | } |
||
| 185 | |||
| 186 | $url = $this->get("document_server_url"); |
||
| 187 | return self::processUrl($url); |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get the document service address available from Chamilo from the application configuration |
||
| 192 | * |
||
| 193 | * @param bool $origin - take origin |
||
| 194 | * |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | public function getDocumentServerInternalUrl($origin = false) { |
||
| 198 | if (!$origin && $this->useDemo()) { |
||
| 199 | return $this->getDocumentServerUrl(); |
||
| 200 | } |
||
| 201 | |||
| 202 | $url = $this->get('document_server_internal'); |
||
| 203 | if (empty($url)) { |
||
| 204 | $url = AppConfig::InternalUrl(); |
||
| 205 | } |
||
| 206 | if (!$origin && empty($url)) { |
||
| 207 | $url = $this->getDocumentServerUrl(); |
||
| 208 | } |
||
| 209 | return self::processUrl($url); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Replace domain in document server url with internal address from configuration |
||
| 214 | * |
||
| 215 | * @param string $url - document server url |
||
| 216 | * |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | public function replaceDocumentServerUrlToInternal($url) { |
||
| 220 | $documentServerUrl = $this->getDocumentServerInternalUrl(); |
||
| 221 | if (!empty($documentServerUrl)) { |
||
| 222 | $from = $this->getDocumentServerUrl(); |
||
| 223 | |||
| 224 | if (!preg_match("/^https?:\/\//i", $from)) { |
||
| 225 | $parsedUrl = parse_url($url); |
||
| 226 | $from = $parsedUrl["scheme"] . "://" . $parsedUrl["host"] . (array_key_exists("port", $parsedUrl) ? (":" . $parsedUrl["port"]) : "") . $from; |
||
| 227 | } |
||
| 228 | |||
| 229 | if ($from !== $documentServerUrl) |
||
| 230 | { |
||
| 231 | $url = str_replace($from, $documentServerUrl, $url); |
||
| 232 | } |
||
| 233 | } |
||
| 234 | return $url; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Get the Chamilo address available from document server from the application configuration |
||
| 239 | * |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | public function getStorageUrl() { |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Get the document service secret key from the application configuration |
||
| 252 | * |
||
| 253 | * @param bool $origin - take origin |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | public function getDocumentServerSecret($origin = false) { |
||
| 258 | if (!$origin && $this->useDemo()) { |
||
| 259 | return AppConfig::GetDemoParams()["SECRET"]; |
||
| 260 | } |
||
| 261 | return $this->get("jwt_secret"); |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Get the jwt header setting |
||
| 266 | * |
||
| 267 | * @param bool $origin - take origin |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | public function getJwtHeader($origin = false) { |
||
| 272 | if (!$origin && $this->useDemo()) { |
||
| 273 | return AppConfig::GetDemoParams()["HEADER"]; |
||
| 274 | } |
||
| 275 | |||
| 276 | $header = $this->get('jwt_header'); |
||
| 277 | if (empty($header)) { |
||
| 278 | $header = AppConfig::JwtHeader() ? AppConfig::JwtHeader() : "Authorization"; |
||
| 279 | } |
||
| 280 | return $header; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Get link to plugin settings |
||
| 285 | * |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public function getConfigLink() { |
||
| 289 | return api_get_path(WEB_PATH)."main/admin/configure_plugin.php?name=".$this->pluginName; |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Get plugin name |
||
| 294 | * |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | public function getPluginName() { |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Add backslash to url if it's needed |
||
| 303 | * |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | public function processUrl($url) { |
||
| 307 | if ($url !== null && $url !== "/") { |
||
| 308 | $url = rtrim($url, "/"); |
||
| 316 |