Conditions | 18 |
Paths | 32 |
Total Lines | 103 |
Code Lines | 71 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
84 | function track(): array |
||
85 | { |
||
86 | global $courseCode; |
||
87 | global $userId; |
||
88 | global $docId; |
||
89 | global $docPath; |
||
90 | global $groupId; |
||
91 | global $sessionId; |
||
92 | global $courseInfo; |
||
93 | global $appSettings; |
||
94 | global $jwtManager; |
||
95 | |||
96 | $body_stream = file_get_contents('php://input'); |
||
97 | if ($body_stream === false) { |
||
98 | return ['error' => 'Bad Request']; |
||
99 | } |
||
100 | |||
101 | $data = json_decode($body_stream, true); |
||
102 | |||
103 | if (null === $data) { |
||
104 | return ['error' => 'Bad Response']; |
||
105 | } |
||
106 | |||
107 | if ($data['status'] == 4) { |
||
108 | return ['status' => 'success', 'message' => 'No changes detected']; |
||
109 | } |
||
110 | |||
111 | if ($jwtManager->isJwtEnabled()) { |
||
112 | if (!empty($data['token'])) { |
||
113 | try { |
||
114 | $payload = $jwtManager->decode($data['token'], $appSettings->getJwtKey()); |
||
115 | } catch (UnexpectedValueException $e) { |
||
116 | return ['status' => 'error', 'error' => '403 Access denied']; |
||
117 | } |
||
118 | } else { |
||
119 | $token = substr(getallheaders()[$appSettings->getJwtHeader()], strlen('Bearer ')); |
||
120 | try { |
||
121 | $decodeToken = $jwtManager->decode($token, $appSettings->getJwtKey()); |
||
122 | $payload = $decodeToken->payload; |
||
123 | } catch (UnexpectedValueException $e) { |
||
124 | return ['status' => 'error', 'error' => '403 Access denied']; |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | |||
129 | if (!empty($docPath)) { |
||
130 | $docPath = urldecode($docPath); |
||
131 | $filePath = api_get_path(SYS_COURSE_PATH).$docPath; |
||
132 | |||
133 | if (!file_exists($filePath)) { |
||
134 | return ['status' => 'error', 'error' => 'File not found']; |
||
135 | } |
||
136 | |||
137 | $documentKey = basename($docPath); |
||
138 | if ($data['status'] == 2 || $data['status'] == 3) { |
||
139 | if (!empty($data['url'])) { |
||
140 | $newContent = file_get_contents($data['url']); |
||
141 | if ($newContent === false) { |
||
142 | return ['status' => 'error', 'error' => 'Failed to fetch document']; |
||
143 | } |
||
144 | |||
145 | if (file_put_contents($filePath, $newContent) === false) { |
||
146 | return ['status' => 'error', 'error' => 'Failed to save document']; |
||
147 | } |
||
148 | } else { |
||
149 | return ['status' => 'error', 'error' => 'No file URL provided']; |
||
150 | } |
||
151 | } |
||
152 | } else if (!empty($docId)) { |
||
153 | $docInfo = DocumentManager::get_document_data_by_id($docId, $courseCode, false, $sessionId); |
||
154 | if (!$docInfo || !file_exists($docInfo['absolute_path'])) { |
||
155 | return ['status' => 'error', 'error' => 'File not found']; |
||
156 | } |
||
157 | |||
158 | $documentKey = $docId; |
||
159 | $data['url'] = $payload->url ?? null; |
||
160 | $data['status'] = $payload->status; |
||
161 | } else { |
||
162 | return ['status' => 'error', 'error' => 'File not found']; |
||
163 | } |
||
164 | |||
165 | $docStatus = new CallbackDocStatus($data['status']); |
||
166 | $callback = new OnlyofficeCallback(); |
||
167 | $callback->setStatus($docStatus); |
||
168 | $callback->setKey($documentKey); |
||
169 | $callback->setUrl($data['url']); |
||
170 | $callbackService = new OnlyofficeCallbackService( |
||
171 | $appSettings, |
||
172 | $jwtManager, |
||
173 | [ |
||
174 | 'courseCode' => $courseCode, |
||
175 | 'userId' => $userId, |
||
176 | 'docId' => $docId ?? '', |
||
177 | 'docPath' => $docPath ?? '', |
||
178 | 'groupId' => $groupId, |
||
179 | 'sessionId' => $sessionId, |
||
180 | 'courseInfo' => $courseInfo, |
||
181 | ] |
||
182 | ); |
||
183 | |||
184 | $result = $callbackService->processCallback($callback, $documentKey); |
||
185 | |||
186 | return $result; |
||
187 | } |
||
293 |
If you suppress an error, we recommend checking for the error condition explicitly: