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 IJobList */ |
||
| 47 | private $jobList; |
||
| 48 | |||
| 49 | 1 | /** @var IConfig */ |
|
| 50 | 1 | private $config; |
|
| 51 | 1 | ||
| 52 | /** |
||
| 53 | * @param AddressHandler $addressHandler |
||
| 54 | * @param IClientService $httpClientService |
||
| 55 | * @param DiscoveryManager $discoveryManager |
||
| 56 | * @param IJobList $jobList |
||
| 57 | * @param IConfig $config |
||
| 58 | */ |
||
| 59 | public function __construct( |
||
| 60 | AddressHandler $addressHandler, |
||
| 61 | IClientService $httpClientService, |
||
| 62 | DiscoveryManager $discoveryManager, |
||
| 63 | IJobList $jobList, |
||
| 64 | IConfig $config |
||
| 65 | ) { |
||
| 66 | $this->addressHandler = $addressHandler; |
||
| 67 | $this->httpClientService = $httpClientService; |
||
| 68 | $this->discoveryManager = $discoveryManager; |
||
| 69 | $this->jobList = $jobList; |
||
| 70 | $this->config = $config; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * send server-to-server share to remote server |
||
| 75 | * |
||
| 76 | * @param Address $shareWithAddress |
||
| 77 | * @param Address $ownerAddress |
||
| 78 | * @param Address $sharedByAddress |
||
| 79 | * @param string $token |
||
| 80 | * @param string $name |
||
| 81 | * @param int $remote_id |
||
| 82 | * |
||
| 83 | * @return bool |
||
| 84 | * |
||
| 85 | * @throws \OC\HintException |
||
| 86 | * @throws \OC\ServerNotAvailableException |
||
| 87 | */ |
||
| 88 | public function sendRemoteShare(Address $shareWithAddress, |
||
| 115 | |||
| 116 | /** |
||
| 117 | * ask owner to re-share the file with the given user |
||
| 118 | * |
||
| 119 | * @param string $token |
||
| 120 | * @param int $id remote Id |
||
| 121 | * @param int $shareId internal share Id |
||
| 122 | * @param string $remote remote address of the owner |
||
| 123 | * @param string $shareWith |
||
| 124 | * @param int $permission |
||
| 125 | * @return bool |
||
| 126 | * @throws \Exception |
||
| 127 | */ |
||
| 128 | public function requestReShare($token, $id, $shareId, $remote, $shareWith, $permission) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * send server-to-server unshare to remote server |
||
| 180 | * |
||
| 181 | * @param string $remote url |
||
| 182 | * @param int $id share id |
||
| 183 | * @param string $token |
||
| 184 | * @return bool |
||
| 185 | */ |
||
| 186 | public function sendRemoteUnShare($remote, $id, $token) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * send server-to-server unshare to remote server |
||
| 192 | * |
||
| 193 | * @param string $remote url |
||
| 194 | * @param int $id share id |
||
| 195 | * @param string $token |
||
| 196 | * @return bool |
||
| 197 | */ |
||
| 198 | public function sendRevokeShare($remote, $id, $token) { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * send notification to remote server if the permissions was changed |
||
| 204 | * |
||
| 205 | * @param string $remote |
||
| 206 | * @param int $remoteId |
||
| 207 | * @param string $token |
||
| 208 | * @param int $permissions |
||
| 209 | * @return bool |
||
| 210 | */ |
||
| 211 | public function sendPermissionChange($remote, $remoteId, $token, $permissions) { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * forward accept reShare to remote server |
||
| 217 | * |
||
| 218 | * @param string $remote |
||
| 219 | * @param int $remoteId |
||
| 220 | * @param string $token |
||
| 221 | */ |
||
| 222 | public function sendAcceptShare($remote, $remoteId, $token) { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * forward decline reShare to remote server |
||
| 228 | * |
||
| 229 | * @param string $remote |
||
| 230 | * @param int $remoteId |
||
| 231 | * @param string $token |
||
| 232 | */ |
||
| 233 | public function sendDeclineShare($remote, $remoteId, $token) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * inform remote server whether server-to-server share was accepted/declined |
||
| 239 | * |
||
| 240 | * @param string $remote |
||
| 241 | * @param int $remoteId Share id on the remote host |
||
| 242 | * @param string $token |
||
| 243 | * @param string $action possible actions: |
||
| 244 | * accept, decline, unshare, revoke, permissions |
||
| 245 | * @param array $data |
||
| 246 | * @param int $try |
||
| 247 | * |
||
| 248 | * @return boolean |
||
| 249 | * |
||
| 250 | * @throws \Exception |
||
| 251 | */ |
||
| 252 | public function sendUpdateToRemote($remote, $remoteId, $token, $action, $data = [], $try = 0) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * return current timestamp |
||
| 299 | * |
||
| 300 | * @return int |
||
| 301 | */ |
||
| 302 | protected function getTimestamp() { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * try http post first with https and then with http as a fallback |
||
| 308 | * |
||
| 309 | * @param string $remoteDomain |
||
| 310 | * @param string $urlSuffix |
||
| 311 | * @param array $fields post parameters |
||
| 312 | * @param bool $useOcm send request to OCM endpoint instead of OCS |
||
| 313 | * |
||
| 314 | * @return array |
||
| 315 | * |
||
| 316 | * @throws \Exception |
||
| 317 | */ |
||
| 318 | protected function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields, $useOcm = false) { |
||
| 364 | |||
| 365 | protected function sendOcmRemoteShare(Address $shareWithAddress, Address $ownerAddress, Address $sharedByAddress, $token, $name, $remote_id) { |
||
| 392 | |||
| 393 | protected function sendPreOcmRemoteShare(Address $shareWithAddress, Address $ownerAddress, Address $sharedByAddress, $token, $name, $remote_id) { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Validate ocs response - 100 or 200 means success |
||
| 417 | * |
||
| 418 | * @param array $status |
||
| 419 | * |
||
| 420 | * @return bool |
||
| 421 | */ |
||
| 422 | private function isOcsStatusOk($status) { |
||
| 425 | } |
||
| 426 |