Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 47 | class OcmController extends Controller { |
||
| 48 | const API_VERSION = '1.0-proposal1'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var OcmMiddleware |
||
| 52 | */ |
||
| 53 | private $ocmMiddleware; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var IURLGenerator |
||
| 57 | */ |
||
| 58 | protected $urlGenerator; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var IUserManager |
||
| 62 | */ |
||
| 63 | protected $userManager; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var AddressHandler |
||
| 67 | */ |
||
| 68 | protected $addressHandler; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var FedShareManager |
||
| 72 | */ |
||
| 73 | protected $fedShareManager; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var ILogger |
||
| 77 | */ |
||
| 78 | protected $logger; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * OcmController constructor. |
||
| 82 | * |
||
| 83 | * @param string $appName |
||
| 84 | * @param IRequest $request |
||
| 85 | * @param OcmMiddleware $ocmMiddleware |
||
| 86 | * @param IURLGenerator $urlGenerator |
||
| 87 | * @param IUserManager $userManager |
||
| 88 | * @param AddressHandler $addressHandler |
||
| 89 | * @param FedShareManager $fedShareManager |
||
| 90 | * @param ILogger $logger |
||
| 91 | */ |
||
| 92 | View Code Duplication | public function __construct($appName, |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @NoCSRFRequired |
||
| 113 | * @PublicPage |
||
| 114 | * |
||
| 115 | * EndPoint discovery |
||
| 116 | * Responds to /ocm-provider/ requests |
||
| 117 | * |
||
| 118 | * @return array |
||
| 119 | */ |
||
| 120 | public function discovery() { |
||
| 121 | $endPoint = $this->urlGenerator->linkToRouteAbsolute( |
||
| 122 | "{$this->appName}.ocm.index" |
||
| 123 | ); |
||
| 124 | return [ |
||
| 125 | 'enabled' => true, |
||
| 126 | 'apiVersion' => self::API_VERSION, |
||
| 127 | 'endPoint' => \rtrim($endPoint, '/'), |
||
| 128 | 'shareTypes' => [ |
||
| 129 | 'name' => FileNotification::RESOURCE_TYPE_FILE, |
||
| 130 | 'protocols' => $this->getProtocols() |
||
| 131 | ] |
||
| 132 | ]; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @NoCSRFRequired |
||
| 137 | * @PublicPage |
||
| 138 | * |
||
| 139 | * @param string $shareWith identifier of the user or group |
||
| 140 | * to share the resource with |
||
| 141 | * @param string $name name of the shared resource |
||
| 142 | * @param string $description share description (optional) |
||
| 143 | * @param string $providerId Identifier of the resource at the provider side |
||
| 144 | * @param string $owner identifier of the user that owns the resource |
||
| 145 | * @param string $ownerDisplayName display name of the owner |
||
| 146 | * @param string $sender Provider specific identifier of the user that wants |
||
| 147 | * to share the resource |
||
| 148 | * @param string $senderDisplayName Display name of the user that wants |
||
| 149 | * to share the resource |
||
| 150 | * @param string $shareType Share type ('user' is supported atm) |
||
| 151 | * @param string $resourceType only 'file' is supported atm |
||
| 152 | * @param array $protocol |
||
| 153 | * [ |
||
| 154 | * 'name' => (string) protocol name. Only 'webdav' is supported atm, |
||
| 155 | * 'options' => [ |
||
| 156 | * protocol specific options |
||
| 157 | * only `webdav` options are supported atm |
||
| 158 | * e.g. `uri`, `access_token`, `password`, `permissions` etc. |
||
| 159 | * |
||
| 160 | * For backward compatibility the webdav protocol will use |
||
| 161 | * the 'sharedSecret" as username and password |
||
| 162 | * ] |
||
| 163 | * |
||
| 164 | * @return JSONResponse |
||
| 165 | */ |
||
| 166 | public function createShare($shareWith, |
||
| 167 | $name, |
||
| 168 | $description, |
||
|
|
|||
| 169 | $providerId, |
||
| 170 | $owner, |
||
| 171 | $ownerDisplayName, |
||
| 172 | $sender, |
||
| 173 | $senderDisplayName, |
||
| 174 | $shareType, |
||
| 175 | $resourceType, |
||
| 176 | $protocol |
||
| 177 | |||
| 178 | ) { |
||
| 179 | try { |
||
| 180 | $this->ocmMiddleware->assertIncomingSharingEnabled(); |
||
| 181 | $this->ocmMiddleware->assertNotNull( |
||
| 182 | [ |
||
| 183 | 'shareWith' => $shareWith, |
||
| 184 | 'name' => $name, |
||
| 185 | 'providerId' => $providerId, |
||
| 186 | 'owner' => $owner, |
||
| 187 | 'shareType' => $shareType, |
||
| 188 | 'resourceType' => $resourceType |
||
| 189 | ] |
||
| 190 | ); |
||
| 191 | if (!\is_array($protocol) |
||
| 192 | || !isset($protocol['name']) |
||
| 193 | || !isset($protocol['options']) |
||
| 194 | || !\is_array($protocol['options']) |
||
| 195 | || !isset($protocol['options']['sharedSecret']) |
||
| 196 | ) { |
||
| 197 | throw new BadRequestException( |
||
| 198 | 'server can not add remote share, missing parameter' |
||
| 199 | ); |
||
| 200 | } |
||
| 201 | if (!\OCP\Util::isValidFileName($name)) { |
||
| 202 | throw new BadRequestException( |
||
| 203 | 'The mountpoint name contains invalid characters.' |
||
| 204 | ); |
||
| 205 | } |
||
| 206 | |||
| 207 | $shareWithAddress = new Address($shareWith); |
||
| 208 | $localShareWith = $shareWithAddress->getUserId(); |
||
| 209 | |||
| 210 | // FIXME this should be a method in the user management instead |
||
| 211 | $this->logger->debug( |
||
| 212 | "shareWith before, $localShareWith", |
||
| 213 | ['app' => $this->appName] |
||
| 214 | ); |
||
| 215 | \OCP\Util::emitHook( |
||
| 216 | '\OCA\Files_Sharing\API\Server2Server', |
||
| 217 | 'preLoginNameUsedAsUserName', |
||
| 218 | ['uid' => &$localShareWith] |
||
| 219 | ); |
||
| 220 | $this->logger->debug( |
||
| 221 | "shareWith after, $localShareWith", |
||
| 222 | ['app' => $this->appName] |
||
| 223 | ); |
||
| 224 | |||
| 225 | if ($this->isSupportedProtocol($protocol['name']) === false) { |
||
| 226 | throw new NotImplementedException( |
||
| 227 | "Protocol {$protocol['name']} is not supported" |
||
| 228 | ); |
||
| 229 | } |
||
| 230 | |||
| 231 | if ($this->isSupportedShareType($shareType) === false) { |
||
| 232 | throw new NotImplementedException( |
||
| 233 | "ShareType {$shareType} is not supported" |
||
| 234 | ); |
||
| 235 | } |
||
| 236 | |||
| 237 | if ($this->isSupportedResourceType($resourceType) === false) { |
||
| 238 | throw new NotImplementedException( |
||
| 239 | "ResourceType {$resourceType} is not supported" |
||
| 240 | ); |
||
| 241 | } |
||
| 242 | |||
| 243 | if (!$this->userManager->userExists($localShareWith)) { |
||
| 244 | throw new BadRequestException("User $localShareWith does not exist"); |
||
| 245 | } |
||
| 246 | |||
| 247 | $ownerAddress = new Address($owner, $ownerDisplayName); |
||
| 248 | $sharedByAddress = new Address($sender, $senderDisplayName); |
||
| 249 | |||
| 250 | $this->fedShareManager->createShare( |
||
| 251 | $ownerAddress, |
||
| 252 | $sharedByAddress, |
||
| 253 | $localShareWith, |
||
| 254 | $providerId, |
||
| 255 | $name, |
||
| 256 | $protocol['options']['sharedSecret'] |
||
| 257 | ); |
||
| 258 | } catch (OcmException $e) { |
||
| 259 | return new JSONResponse( |
||
| 260 | ['message' => $e->getMessage()], |
||
| 261 | $e->getHttpStatusCode() |
||
| 262 | ); |
||
| 263 | } catch (\Exception $e) { |
||
| 264 | $this->logger->error( |
||
| 265 | "server can not add remote share, {$e->getMessage()}", |
||
| 266 | ['app' => 'federatefilesharing'] |
||
| 267 | ); |
||
| 268 | return new JSONResponse( |
||
| 269 | [ |
||
| 270 | 'message' => "internal server error, was not able to add share from {$ownerAddress->getHostName()}" |
||
| 271 | ], |
||
| 272 | Http::STATUS_INTERNAL_SERVER_ERROR |
||
| 273 | ); |
||
| 274 | } |
||
| 275 | return new JSONResponse( |
||
| 276 | [], |
||
| 277 | Http::STATUS_CREATED |
||
| 278 | ); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @NoCSRFRequired |
||
| 283 | * @PublicPage |
||
| 284 | * |
||
| 285 | * @param string $notificationType notification type (SHARE_REMOVED, etc) |
||
| 286 | * @param string $resourceType only 'file' is supported atm |
||
| 287 | * @param string $providerId Identifier of the resource at the provider side |
||
| 288 | * @param array $notification |
||
| 289 | * [ |
||
| 290 | * optional additional parameters, depending on the notification |
||
| 291 | * and the resource type |
||
| 292 | * ] |
||
| 293 | * |
||
| 294 | * @return JSONResponse |
||
| 295 | */ |
||
| 296 | public function processNotification($notificationType, |
||
| 297 | $resourceType, |
||
| 298 | $providerId, |
||
| 299 | $notification |
||
| 300 | ) { |
||
| 301 | try { |
||
| 302 | $this->ocmMiddleware->assertNotNull( |
||
| 303 | [ |
||
| 304 | 'notifictationType' => $notificationType, |
||
| 305 | 'resourceType' => $resourceType, |
||
| 306 | 'providerId' => $providerId |
||
| 307 | ] |
||
| 308 | ); |
||
| 309 | if (!\is_array($notification)) { |
||
| 310 | throw new BadRequestException( |
||
| 311 | 'server can not add remote share, missing parameter' |
||
| 312 | ); |
||
| 313 | } |
||
| 314 | |||
| 315 | if ($this->isSupportedResourceType($resourceType) === false) { |
||
| 316 | throw new NotImplementedException( |
||
| 317 | "ResourceType {$resourceType} is not supported" |
||
| 318 | ); |
||
| 319 | } |
||
| 320 | // TODO: check permissions/preconditions in all cases |
||
| 321 | switch ($notificationType) { |
||
| 322 | View Code Duplication | case FileNotification::NOTIFICATION_TYPE_SHARE_ACCEPTED: |
|
| 323 | $share = $this->ocmMiddleware->getValidShare( |
||
| 324 | $providerId, $notification['sharedSecret'] |
||
| 325 | ); |
||
| 326 | $this->fedShareManager->acceptShare($share); |
||
| 327 | break; |
||
| 328 | View Code Duplication | case FileNotification::NOTIFICATION_TYPE_SHARE_DECLINED: |
|
| 329 | $share = $this->ocmMiddleware->getValidShare( |
||
| 330 | $providerId, $notification['sharedSecret'] |
||
| 331 | ); |
||
| 332 | $this->fedShareManager->declineShare($share); |
||
| 333 | break; |
||
| 334 | case FileNotification::NOTIFICATION_TYPE_REQUEST_RESHARE: |
||
| 335 | $shareWithAddress = new Address($notification['shareWith']); |
||
| 336 | $localShareWith = $shareWithAddress->getUserId(); |
||
| 337 | $share = $this->ocmMiddleware->getValidShare( |
||
| 338 | $providerId, $notification['sharedSecret'] |
||
| 339 | ); |
||
| 340 | // TODO: permissions not needed ??? |
||
| 341 | $share = $this->fedShareManager->reShare( |
||
| 342 | $share, $providerId, $localShareWith, 0 |
||
| 343 | ); |
||
| 344 | return new JSONResponse( |
||
| 345 | [ |
||
| 346 | 'sharedSecret' => $share->getToken(), |
||
| 347 | 'providerId' => $share->getId() |
||
| 348 | ], |
||
| 349 | Http::STATUS_CREATED |
||
| 350 | ); |
||
| 351 | break; |
||
| 352 | case FileNotification::NOTIFICATION_TYPE_RESHARE_CHANGE_PERMISSION: |
||
| 353 | $permissions = $notification['permission']; |
||
| 354 | // TODO: Map OCM permissions to numeric |
||
| 355 | $share = $this->ocmMiddleware->getValidShare( |
||
| 356 | $providerId, $notification['sharedSecret'] |
||
| 357 | ); |
||
| 358 | $this->fedShareManager->updatePermissions($share, $permissions); |
||
| 359 | break; |
||
| 360 | case FileNotification::NOTIFICATION_TYPE_SHARE_UNSHARED: |
||
| 361 | $this->fedShareManager->unshare( |
||
| 362 | $providerId, $notification['sharedSecret'] |
||
| 363 | ); |
||
| 364 | break; |
||
| 365 | View Code Duplication | case FileNotification::NOTIFICATION_TYPE_RESHARE_UNDO: |
|
| 366 | $share = $this->ocmMiddleware->getValidShare( |
||
| 367 | $providerId, $notification['sharedSecret'] |
||
| 368 | ); |
||
| 369 | $this->fedShareManager->revoke($share); |
||
| 370 | break; |
||
| 371 | default: |
||
| 372 | return new JSONResponse( |
||
| 373 | ['message' => "Notification of type {$notificationType} is not supported"], |
||
| 374 | Http::STATUS_NOT_IMPLEMENTED |
||
| 375 | ); |
||
| 376 | } |
||
| 377 | } catch (OcmException $e) { |
||
| 378 | return new JSONResponse( |
||
| 379 | ['message' => $e->getMessage()], |
||
| 380 | $e->getHttpStatusCode() |
||
| 381 | ); |
||
| 382 | } catch (Share\Exceptions\ShareNotFound $e) { |
||
| 383 | return new JSONResponse( |
||
| 384 | ['message' => $e->getMessage()], |
||
| 385 | Http::STATUS_BAD_REQUEST |
||
| 386 | ); |
||
| 387 | } catch (\Exception $e) { |
||
| 388 | $this->logger->error( |
||
| 389 | "server can not process notification, {$e->getMessage()}", |
||
| 390 | ['app' => 'federatefilesharing'] |
||
| 391 | ); |
||
| 392 | return new JSONResponse( |
||
| 393 | [ |
||
| 394 | 'message' => "internal server error, was not able to process notification" |
||
| 395 | ], |
||
| 396 | Http::STATUS_INTERNAL_SERVER_ERROR |
||
| 397 | ); |
||
| 398 | } |
||
| 399 | return new JSONResponse( |
||
| 400 | [], |
||
| 401 | Http::STATUS_CREATED |
||
| 402 | ); |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Get list of supported protocols |
||
| 407 | * |
||
| 408 | * @return array |
||
| 409 | */ |
||
| 410 | protected function getProtocols() { |
||
| 411 | return [ |
||
| 412 | 'webdav' => '/public.php/webdav/' |
||
| 413 | ]; |
||
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @param string $resourceType |
||
| 418 | * |
||
| 419 | * @return bool |
||
| 420 | */ |
||
| 421 | protected function isSupportedResourceType($resourceType) { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @param string $shareType |
||
| 427 | * @return bool |
||
| 428 | */ |
||
| 429 | protected function isSupportedShareType($shareType) { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param string $protocolName |
||
| 436 | * @return bool |
||
| 437 | */ |
||
| 438 | protected function isSupportedProtocol($protocolName) { |
||
| 443 | } |
||
| 444 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.