| Total Complexity | 40 |
| Total Lines | 263 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like OnlyofficeDocumentManager 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 OnlyofficeDocumentManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class OnlyofficeDocumentManager extends DocumentManager |
||
| 21 | { |
||
| 22 | private $docInfo; |
||
| 23 | |||
| 24 | public function __construct($settingsManager, array $docInfo, $formats = null, $systemLangCode = 'en') |
||
| 25 | { |
||
| 26 | $formats = new OnlyofficeFormatsManager(); |
||
| 27 | parent::__construct($settingsManager, $formats, $systemLangCode); |
||
| 28 | $this->docInfo = $docInfo; |
||
| 29 | } |
||
| 30 | |||
| 31 | public function getDocumentKey(string $fileId, $courseCode, bool $embedded = false) |
||
| 32 | { |
||
| 33 | if (!isset($this->docInfo['absolute_path'])) { |
||
| 34 | return null; |
||
| 35 | } |
||
| 36 | $mtime = filemtime($this->docInfo['absolute_path']); |
||
| 37 | $key = $mtime.$courseCode.$fileId; |
||
| 38 | |||
| 39 | return self::generateRevisionId($key); |
||
| 40 | } |
||
| 41 | |||
| 42 | public function getDocumentName(string $fileId = '') |
||
| 43 | { |
||
| 44 | return $this->docInfo['title']; |
||
| 45 | } |
||
| 46 | |||
| 47 | public static function getLangMapping() |
||
| 48 | { |
||
| 49 | } |
||
| 50 | |||
| 51 | public function getFileUrl(string $fileId) |
||
| 52 | { |
||
| 53 | $data = [ |
||
| 54 | 'type' => 'download', |
||
| 55 | 'courseId' => api_get_course_int_id(), |
||
| 56 | 'userId' => api_get_user_id(), |
||
| 57 | 'docId' => $fileId, |
||
| 58 | 'sessionId' => api_get_session_id(), |
||
| 59 | ]; |
||
| 60 | |||
| 61 | if (!empty($this->getGroupId())) { |
||
| 62 | $data['groupId'] = $this->getGroupId(); |
||
| 63 | } |
||
| 64 | |||
| 65 | if (isset($this->docInfo['path']) && str_contains($this->docInfo['path'], 'exercises/')) { |
||
| 66 | $data['doctype'] = 'exercise'; |
||
| 67 | $data['docPath'] = urlencode($this->docInfo['path']); |
||
| 68 | } |
||
| 69 | |||
| 70 | $jwtManager = new OnlyofficeJwtManager($this->settingsManager); |
||
| 71 | $hashUrl = $jwtManager->getHash($data); |
||
| 72 | |||
| 73 | return api_get_path(WEB_PLUGIN_PATH).$this->settingsManager->plugin->getPluginName().'/callback.php?hash='.$hashUrl; |
||
| 74 | } |
||
| 75 | |||
| 76 | public function getGroupId() |
||
| 77 | { |
||
| 78 | $groupId = isset($_GET['groupId']) && !empty($_GET['groupId']) ? $_GET['groupId'] : null; |
||
| 79 | |||
| 80 | return $groupId; |
||
| 81 | } |
||
| 82 | |||
| 83 | public function getCallbackUrl(string $fileId) |
||
| 84 | { |
||
| 85 | $data = [ |
||
| 86 | 'type' => 'track', |
||
| 87 | 'courseId' => api_get_course_int_id(), |
||
| 88 | 'userId' => api_get_user_id(), |
||
| 89 | 'docId' => $fileId, |
||
| 90 | 'sessionId' => api_get_session_id(), |
||
| 91 | ]; |
||
| 92 | |||
| 93 | if (!empty($this->getGroupId())) { |
||
| 94 | $data['groupId'] = $this->getGroupId(); |
||
| 95 | } |
||
| 96 | |||
| 97 | if (isset($this->docInfo['path']) && str_contains($this->docInfo['path'], 'exercises/')) { |
||
| 98 | $data['doctype'] = 'exercise'; |
||
| 99 | $data['docPath'] = urlencode($this->docInfo['path']); |
||
| 100 | } |
||
| 101 | |||
| 102 | $jwtManager = new OnlyofficeJwtManager($this->settingsManager); |
||
| 103 | $hashUrl = $jwtManager->getHash($data); |
||
| 104 | |||
| 105 | return api_get_path(WEB_PLUGIN_PATH).'onlyoffice/callback.php?hash='.$hashUrl; |
||
| 106 | } |
||
| 107 | |||
| 108 | public function getGobackUrl(string $fileId): string |
||
| 109 | { |
||
| 110 | if (!empty($this->docInfo)) { |
||
| 111 | if (isset($this->docInfo['path']) && str_contains($this->docInfo['path'], 'exercises/')) { |
||
| 112 | return api_get_path(WEB_CODE_PATH).'exercise/exercise_submit.php' |
||
| 113 | .'?cidReq='.Security::remove_XSS(api_get_course_id()) |
||
| 114 | .'&id_session='.Security::remove_XSS(api_get_session_id()) |
||
| 115 | .'&gidReq='.Security::remove_XSS($this->getGroupId()) |
||
| 116 | .'&exerciseId='.Security::remove_XSS($this->docInfo['exercise_id']); |
||
| 117 | } |
||
| 118 | |||
| 119 | return self::getUrlToLocation(api_get_course_id(), api_get_session_id(), $this->getGroupId(), $this->docInfo['parent_id'], $this->docInfo['path'] ?? ''); |
||
| 120 | } |
||
| 121 | |||
| 122 | return ''; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Return location file in Chamilo documents or exercises. |
||
| 127 | */ |
||
| 128 | public static function getUrlToLocation($courseCode, $sessionId, $groupId, $folderId, $filePath = ''): string |
||
| 129 | { |
||
| 130 | if (!empty($filePath) && str_contains($filePath, 'exercises/')) { |
||
| 131 | return api_get_path(WEB_CODE_PATH).'exercise/exercise_submit.php' |
||
| 132 | .'?cidReq='.Security::remove_XSS($courseCode) |
||
| 133 | .'&id_session='.Security::remove_XSS($sessionId) |
||
| 134 | .'&gidReq='.Security::remove_XSS($groupId) |
||
| 135 | .'&exerciseId='.Security::remove_XSS($folderId); |
||
| 136 | } |
||
| 137 | |||
| 138 | return api_get_path(WEB_CODE_PATH).'document/document.php' |
||
| 139 | .'?cidReq='.Security::remove_XSS($courseCode) |
||
| 140 | .'&id_session='.Security::remove_XSS($sessionId) |
||
| 141 | .'&gidReq='.Security::remove_XSS($groupId) |
||
| 142 | .'&id='.Security::remove_XSS($folderId); |
||
| 143 | } |
||
| 144 | |||
| 145 | public function getCreateUrl(string $fileId) |
||
| 146 | { |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Get the value of docInfo. |
||
| 151 | */ |
||
| 152 | public function getDocInfo($elem = null) |
||
| 153 | { |
||
| 154 | if (empty($elem)) { |
||
| 155 | return $this->docInfo; |
||
| 156 | } else { |
||
| 157 | if (isset($this->docInfo[$elem])) { |
||
| 158 | return $this->docInfo[$elem]; |
||
| 159 | } |
||
| 160 | |||
| 161 | return []; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Set the value of docInfo. |
||
| 167 | */ |
||
| 168 | public function setDocInfo($docInfo) |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Return file extension by file type. |
||
| 175 | */ |
||
| 176 | public static function getDocExtByType(string $type): string |
||
| 177 | { |
||
| 178 | if ('text' === $type) { |
||
| 179 | return 'docx'; |
||
| 180 | } |
||
| 181 | if ('spreadsheet' === $type) { |
||
| 182 | return 'xlsx'; |
||
| 183 | } |
||
| 184 | if ('presentation' === $type) { |
||
| 185 | return 'pptx'; |
||
| 186 | } |
||
| 187 | if ('formTemplate' === $type) { |
||
| 188 | return 'pdf'; |
||
| 189 | } |
||
| 190 | |||
| 191 | return ''; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Create new file. |
||
| 196 | */ |
||
| 197 | public static function createFile( |
||
| 283 | } |
||
| 284 | } |
||
| 285 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: