Total Complexity | 40 |
Total Lines | 274 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like action_plugin_issuelinks_ajax 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 action_plugin_issuelinks_ajax, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class action_plugin_issuelinks_ajax extends DokuWiki_Action_Plugin { |
||
15 | |||
16 | /** @var helper_plugin_issuelinks_util util */ |
||
17 | public $util; |
||
18 | |||
19 | public function __construct() { |
||
22 | } |
||
23 | |||
24 | /** |
||
25 | * Registers a callback function for a given event |
||
26 | * |
||
27 | * @param Doku_Event_Handler $controller DokuWiki's event controller object |
||
28 | * @return void |
||
29 | */ |
||
30 | public function register(Doku_Event_Handler $controller) { |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Create/Delete the webhook of a repository |
||
39 | * |
||
40 | * @param Doku_Event $event event object by reference |
||
41 | * @param mixed $param [the parameters passed as fifth argument to register_hook() when this |
||
42 | * handler was registered] |
||
43 | * |
||
44 | * @return void |
||
45 | */ |
||
46 | public function repo_admin_toggle(Doku_Event $event, $param) { |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Get the repos of an organisation and create HTML from them and return it to the request |
||
85 | * |
||
86 | * @param Doku_Event $event event object by reference |
||
87 | * @param mixed $param [the parameters passed as fifth argument to register_hook() when this |
||
88 | * handler was registered] |
||
89 | * @return void |
||
90 | */ |
||
91 | public function repo_admin_org(Doku_Event $event, $param) { |
||
92 | if ($event->data !== 'issuelinks_repo_admin_getorg') { |
||
93 | return; |
||
94 | } |
||
95 | $event->preventDefault(); |
||
96 | $event->stopPropagation(); |
||
97 | |||
98 | if (empty($_SERVER['REMOTE_USER'])) { |
||
99 | $this->util->sendResponse(401, 'Not logged in!'); |
||
100 | return; |
||
101 | } |
||
102 | |||
103 | global $INPUT; |
||
104 | if (!auth_isadmin()) { |
||
105 | $this->util->sendResponse(403, 'Must be Admin'); |
||
106 | return; |
||
107 | } |
||
108 | |||
109 | $serviceId = $INPUT->str('servicename'); |
||
110 | $organisation = $INPUT->str('org'); |
||
111 | try { |
||
112 | $html = $this->createOrgRepoHTML($serviceId, $organisation); |
||
113 | } catch (\Throwable $e) { |
||
114 | $this->util->sendResponse($e->getCode(), $e->getMessage()); |
||
115 | } |
||
116 | $this->util->sendResponse(200, $html); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Create the HTML of the repositories of an organisation/group of a service. |
||
121 | * |
||
122 | * @param string $org the organisation from which to request the repositories |
||
123 | * @return string |
||
124 | */ |
||
125 | public function createOrgRepoHTML($serviceId, $org) { |
||
155 | } |
||
156 | |||
157 | public function asyncImportAllIssues(Doku_Event $event, $param) { |
||
190 | |||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Pass Ajax call to a type |
||
195 | * |
||
196 | * @param Doku_Event $event event object by reference |
||
197 | * @param mixed $param [the parameters passed as fifth argument to register_hook() when this |
||
198 | * handler was registered] |
||
199 | * @return void |
||
200 | */ |
||
201 | public function handle_ajax(Doku_Event $event, $param) { |
||
202 | if ($event->data !== 'plugin_issuelinks') return; |
||
203 | $event->preventDefault(); |
||
204 | $event->stopPropagation(); |
||
205 | |||
206 | if (empty($_SERVER['REMOTE_USER'])) { |
||
207 | $this->util->sendResponse(401, 'Not logged in!'); |
||
208 | return; |
||
209 | } |
||
210 | |||
211 | global $INPUT; |
||
212 | |||
213 | $action = $INPUT->str('issuelinks-action'); |
||
214 | $serviceName = $INPUT->str('issuelinks-service'); |
||
215 | $projectKey = $INPUT->str('issuelinks-project'); |
||
216 | $issueId = $INPUT->str('issuelinks-issueid'); |
||
217 | $isMergeRequest = $INPUT->bool('issuelinks-ismergerequest'); |
||
218 | |||
219 | switch ($action) { |
||
220 | case 'checkImportStatus': |
||
221 | list($code, $data) = $this->checkImportStatus($serviceName, $projectKey); |
||
222 | break; |
||
223 | case 'issueToolTip': |
||
224 | list($code, $data) = $this->getIssueTooltipHTML($serviceName, $projectKey, $issueId, $isMergeRequest); |
||
225 | break; |
||
226 | case 'getAdditionalIssueData': |
||
227 | list($code, $data) = $this->getAdditionalIssueData($serviceName, $projectKey, $issueId, $isMergeRequest); |
||
228 | break; |
||
229 | default: |
||
230 | $code = 400; |
||
231 | $data = 'malformed request'; |
||
232 | } |
||
233 | $this->util->sendResponse($code, $data); |
||
234 | } |
||
235 | |||
236 | protected function checkImportStatus($serviceName, $projectKey) { |
||
237 | if (!auth_isadmin()) { |
||
238 | return [403, 'Must be Admin']; |
||
239 | } |
||
240 | |||
241 | /** @var helper_plugin_issuelinks_data $data */ |
||
242 | $data = plugin_load('helper', 'issuelinks_data'); |
||
243 | $lockID = $data->getImportLockID($serviceName, $projectKey); |
||
244 | $lockData = $data->getLockContent($lockID); |
||
245 | if ($lockData === false) { |
||
246 | msg('Import not locked ' . $lockID,2); |
||
247 | return [200, ['status' => 'done']]; |
||
248 | } |
||
249 | if (!empty($lockData['status']) && $lockData['status'] === 'done') { |
||
250 | $data->removeLock($lockID); |
||
251 | } |
||
252 | |||
253 | return [200, $lockData]; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * @param $pmServiceName |
||
258 | * @param $projectKey |
||
259 | * @param $issueId |
||
260 | * |
||
261 | * @return array |
||
262 | */ |
||
263 | private function getIssueTooltipHTML($pmServiceName, $projectKey, $issueId, $isMergeRequest) { |
||
275 | } |
||
276 | |||
277 | private function getAdditionalIssueData($pmServiceName, $projectKey, $issueId, $isMergeRequest) { |
||
288 | } |
||
289 | |||
290 | } |
||
291 |