Complex classes like Notifications 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Notifications, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class Notifications { |
||
35 | const RESPONSE_FORMAT = 'json'; // default response format for ocs calls |
||
36 | |||
37 | /** @var AddressHandler */ |
||
38 | private $addressHandler; |
||
39 | |||
40 | /** @var IClientService */ |
||
41 | private $httpClientService; |
||
42 | |||
43 | /** @var DiscoveryManager */ |
||
44 | private $discoveryManager; |
||
45 | 1 | ||
46 | /** @var NotificationManager */ |
||
47 | private $notificationManager; |
||
48 | |||
49 | 1 | /** @var IJobList */ |
|
50 | 1 | private $jobList; |
|
51 | 1 | ||
52 | /** @var IConfig */ |
||
53 | private $config; |
||
54 | |||
55 | /** |
||
56 | * @param AddressHandler $addressHandler |
||
57 | * @param IClientService $httpClientService |
||
58 | * @param DiscoveryManager $discoveryManager |
||
59 | * @param IJobList $jobList |
||
60 | * @param IConfig $config |
||
61 | */ |
||
62 | public function __construct( |
||
77 | |||
78 | /** |
||
79 | * send server-to-server share to remote server |
||
80 | * |
||
81 | * @param Address $shareWithAddress |
||
82 | * @param Address $ownerAddress |
||
83 | * @param Address $sharedByAddress |
||
84 | * @param string $token |
||
85 | * @param string $name |
||
86 | * @param int $remote_id |
||
87 | * |
||
88 | * @return bool |
||
89 | * |
||
90 | * @throws \OC\HintException |
||
91 | * @throws \OC\ServerNotAvailableException |
||
92 | */ |
||
93 | public function sendRemoteShare(Address $shareWithAddress, |
||
120 | |||
121 | /** |
||
122 | * ask owner to re-share the file with the given user |
||
123 | * |
||
124 | * @param string $token |
||
125 | * @param int $id remote Id |
||
126 | * @param int $shareId internal share Id |
||
127 | * @param string $remote remote address of the owner |
||
128 | * @param string $shareWith |
||
129 | * @param int $permission |
||
130 | * @return bool |
||
131 | * @throws \Exception |
||
132 | */ |
||
133 | public function requestReShare($token, $id, $shareId, $remote, $shareWith, $permission) { |
||
181 | |||
182 | /** |
||
183 | * send server-to-server unshare to remote server |
||
184 | * |
||
185 | * @param string $remote url |
||
186 | * @param int $id share id |
||
187 | * @param string $token |
||
188 | * @return bool |
||
189 | */ |
||
190 | public function sendRemoteUnShare($remote, $id, $token) { |
||
193 | |||
194 | /** |
||
195 | * send server-to-server unshare to remote server |
||
196 | * |
||
197 | * @param string $remote url |
||
198 | * @param int $id share id |
||
199 | * @param string $token |
||
200 | * @return bool |
||
201 | */ |
||
202 | public function sendRevokeShare($remote, $id, $token) { |
||
205 | |||
206 | /** |
||
207 | * send notification to remote server if the permissions was changed |
||
208 | * |
||
209 | * @param string $remote |
||
210 | * @param int $remoteId |
||
211 | * @param string $token |
||
212 | * @param int $permissions |
||
213 | * @return bool |
||
214 | */ |
||
215 | public function sendPermissionChange($remote, $remoteId, $token, $permissions) { |
||
218 | |||
219 | /** |
||
220 | * forward accept reShare to remote server |
||
221 | * |
||
222 | * @param string $remote |
||
223 | * @param int $remoteId |
||
224 | * @param string $token |
||
225 | */ |
||
226 | public function sendAcceptShare($remote, $remoteId, $token) { |
||
229 | |||
230 | /** |
||
231 | * forward decline reShare to remote server |
||
232 | * |
||
233 | * @param string $remote |
||
234 | * @param int $remoteId |
||
235 | * @param string $token |
||
236 | */ |
||
237 | public function sendDeclineShare($remote, $remoteId, $token) { |
||
240 | |||
241 | /** |
||
242 | * inform remote server whether server-to-server share was accepted/declined |
||
243 | * |
||
244 | * @param string $remote |
||
245 | * @param int $remoteId Share id on the remote host |
||
246 | * @param string $token |
||
247 | * @param string $action possible actions: |
||
248 | * accept, decline, unshare, revoke, permissions |
||
249 | * @param array $data |
||
250 | * @param int $try |
||
251 | * |
||
252 | * @return boolean |
||
253 | * |
||
254 | * @throws \Exception |
||
255 | */ |
||
256 | public function sendUpdateToRemote($remote, $remoteId, $token, $action, $data = [], $try = 0) { |
||
299 | |||
300 | /** |
||
301 | * return current timestamp |
||
302 | * |
||
303 | * @return int |
||
304 | */ |
||
305 | protected function getTimestamp() { |
||
308 | |||
309 | /** |
||
310 | * try http post first with https and then with http as a fallback |
||
311 | * |
||
312 | * @param string $remoteDomain |
||
313 | * @param string $urlSuffix |
||
314 | * @param array $fields post parameters |
||
315 | * @param bool $useOcm send request to OCM endpoint instead of OCS |
||
316 | * |
||
317 | * @return array |
||
318 | * |
||
319 | * @throws \Exception |
||
320 | */ |
||
321 | protected function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields, $useOcm = false) { |
||
367 | |||
368 | protected function sendOcmRemoteShare(Address $shareWithAddress, Address $ownerAddress, Address $sharedByAddress, $token, $name, $remote_id) { |
||
395 | |||
396 | protected function sendPreOcmRemoteShare(Address $shareWithAddress, Address $ownerAddress, Address $sharedByAddress, $token, $name, $remote_id) { |
||
417 | |||
418 | /** |
||
419 | * Validate ocs response - 100 or 200 means success |
||
420 | * |
||
421 | * @param array $status |
||
422 | * |
||
423 | * @return bool |
||
424 | */ |
||
425 | private function isOcsStatusOk($status) { |
||
428 | } |
||
429 |