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 |
||
| 53 | class RequestHandlerController extends Controller { |
||
| 54 | |||
| 55 | /** @var ILogger */ |
||
| 56 | private $logger; |
||
| 57 | |||
| 58 | /** @var IUserManager */ |
||
| 59 | private $userManager; |
||
| 60 | |||
| 61 | /** @var IGroupManager */ |
||
| 62 | private $groupManager; |
||
| 63 | |||
| 64 | /** @var IURLGenerator */ |
||
| 65 | private $urlGenerator; |
||
| 66 | |||
| 67 | /** @var ICloudFederationProviderManager */ |
||
| 68 | private $cloudFederationProviderManager; |
||
| 69 | |||
| 70 | /** @var Config */ |
||
| 71 | private $config; |
||
| 72 | |||
| 73 | /** @var ICloudFederationFactory */ |
||
| 74 | private $factory; |
||
| 75 | |||
| 76 | /** @var ICloudIdManager */ |
||
| 77 | private $cloudIdManager; |
||
| 78 | |||
| 79 | View Code Duplication | public function __construct($appName, |
|
| 101 | |||
| 102 | /** |
||
| 103 | * add share |
||
| 104 | * |
||
| 105 | * @NoCSRFRequired |
||
| 106 | * @PublicPage |
||
| 107 | * @BruteForceProtection(action=receiveFederatedShare) |
||
| 108 | * |
||
| 109 | * @param string $shareWith |
||
| 110 | * @param string $name resource name (e.g. document.odt) |
||
| 111 | * @param string $description share description (optional) |
||
| 112 | * @param string $providerId resource UID on the provider side |
||
| 113 | * @param string $owner provider specific UID of the user who owns the resource |
||
| 114 | * @param string $ownerDisplayName display name of the user who shared the item |
||
| 115 | * @param string $sharedBy provider specific UID of the user who shared the resource |
||
| 116 | * @param string $sharedByDisplayName display name of the user who shared the resource |
||
| 117 | * @param array $protocol (e,.g. ['name' => 'webdav', 'options' => ['username' => 'john', 'permissions' => 31]]) |
||
| 118 | * @param string $shareType ('group' or 'user' share) |
||
| 119 | * @param $resourceType ('file', 'calendar',...) |
||
| 120 | * @return Http\DataResponse|JSONResponse |
||
| 121 | * |
||
| 122 | * Example: curl -H "Content-Type: application/json" -X POST -d '{"shareWith":"admin1@serve1","name":"welcome server2.txt","description":"desc","providerId":"2","owner":"admin2@http://localhost/server2","ownerDisplayName":"admin2 display","shareType":"user","resourceType":"file","protocol":{"name":"webdav","options":{"sharedSecret":"secret","permissions":"webdav-property"}}}' http://localhost/server/index.php/ocm/shares |
||
| 123 | */ |
||
| 124 | public function addShare($shareWith, $name, $description, $providerId, $owner, $ownerDisplayName, $sharedBy, $sharedByDisplayName, $protocol, $shareType, $resourceType) { |
||
| 125 | |||
| 126 | // check if all required parameters are set |
||
| 127 | if ($shareWith === null || |
||
| 128 | $name === null || |
||
| 129 | $providerId === null || |
||
| 130 | $owner === null || |
||
| 131 | $resourceType === null || |
||
| 132 | $shareType === null || |
||
| 133 | !is_array($protocol) || |
||
| 134 | !isset($protocol['name']) || |
||
| 135 | !isset ($protocol['options']) || |
||
| 136 | !is_array($protocol['options']) || |
||
| 137 | !isset($protocol['options']['sharedSecret']) |
||
| 138 | ) { |
||
| 139 | return new JSONResponse( |
||
| 140 | ['message' => 'Missing arguments'], |
||
| 141 | Http::STATUS_BAD_REQUEST |
||
| 142 | ); |
||
| 143 | } |
||
| 144 | |||
| 145 | $supportedShareTypes = $this->config->getSupportedShareTypes($resourceType); |
||
| 146 | if (!in_array($shareType, $supportedShareTypes)) { |
||
| 147 | return new JSONResponse( |
||
| 148 | ['message' => 'Share type "' . $shareType . '" not implemented'], |
||
| 149 | Http::STATUS_NOT_IMPLEMENTED |
||
| 150 | ); |
||
| 151 | } |
||
| 152 | |||
| 153 | $cloudId = $this->cloudIdManager->resolveCloudId($shareWith); |
||
| 154 | $shareWith = $cloudId->getUser(); |
||
| 155 | |||
| 156 | if ($shareType === 'user') { |
||
| 157 | $shareWith = $this->mapUid($shareWith); |
||
| 158 | |||
| 159 | if (!$this->userManager->userExists($shareWith)) { |
||
| 160 | return new JSONResponse( |
||
| 161 | ['message' => 'User "' . $shareWith . '" does not exists at ' . $this->urlGenerator->getBaseUrl()], |
||
| 162 | Http::STATUS_BAD_REQUEST |
||
| 163 | ); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | if ($shareType === 'group') { |
||
| 168 | if(!$this->groupManager->groupExists($shareWith)) { |
||
| 169 | return new JSONResponse( |
||
| 170 | ['message' => 'Group "' . $shareWith . '" does not exists at ' . $this->urlGenerator->getBaseUrl()], |
||
| 171 | Http::STATUS_BAD_REQUEST |
||
| 172 | ); |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | // if no explicit display name is given, we use the uid as display name |
||
| 177 | $ownerDisplayName = $ownerDisplayName === null ? $owner : $ownerDisplayName; |
||
| 178 | $sharedByDisplayName = $sharedByDisplayName === null ? $sharedBy : $sharedByDisplayName; |
||
| 179 | |||
| 180 | // sharedBy* parameter is optional, if nothing is set we assume that it is the same user as the owner |
||
| 181 | if ($sharedBy === null) { |
||
| 182 | $sharedBy = $owner; |
||
| 183 | $sharedByDisplayName = $ownerDisplayName; |
||
| 184 | } |
||
| 185 | |||
| 186 | try { |
||
| 187 | $provider = $this->cloudFederationProviderManager->getCloudFederationProvider($resourceType); |
||
| 188 | $share = $this->factory->getCloudFederationShare($shareWith, $name, $description, $providerId, $owner, $ownerDisplayName, $sharedBy, $sharedByDisplayName, '', $shareType, $resourceType); |
||
| 189 | $share->setProtocol($protocol); |
||
| 190 | $provider->shareReceived($share); |
||
| 191 | } catch (ProviderDoesNotExistsException $e) { |
||
| 192 | return new JSONResponse( |
||
| 193 | ['message' => $e->getMessage()], |
||
| 194 | Http::STATUS_NOT_IMPLEMENTED |
||
| 195 | ); |
||
| 196 | } catch (ProviderCouldNotAddShareException $e) { |
||
| 197 | return new JSONResponse( |
||
| 198 | ['message' => $e->getMessage()], |
||
| 199 | $e->getCode() |
||
| 200 | ); |
||
| 201 | } catch (\Exception $e) { |
||
| 202 | return new JSONResponse( |
||
| 203 | ['message' => 'Internal error at ' . $this->urlGenerator->getBaseUrl()], |
||
| 204 | Http::STATUS_BAD_REQUEST |
||
| 205 | ); |
||
| 206 | } |
||
| 207 | |||
| 208 | $user = $this->userManager->get($shareWith); |
||
| 209 | $recipientDisplayName = ''; |
||
| 210 | if($user) { |
||
| 211 | $recipientDisplayName = $user->getDisplayName(); |
||
| 212 | } |
||
| 213 | |||
| 214 | return new JSONResponse( |
||
| 215 | ['recipientDisplayName' => $recipientDisplayName], |
||
| 216 | Http::STATUS_CREATED); |
||
| 217 | |||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * receive notification about existing share |
||
| 222 | * |
||
| 223 | * @NoCSRFRequired |
||
| 224 | * @PublicPage |
||
| 225 | * @BruteForceProtection(action=receiveFederatedShareNotification) |
||
| 226 | * |
||
| 227 | * @param string $notificationType (notification type, e.g. SHARE_ACCEPTED) |
||
| 228 | * @param string $resourceType (calendar, file, contact,...) |
||
| 229 | * @param string $providerId id of the share |
||
| 230 | * @param array $notification the actual payload of the notification |
||
| 231 | * @return JSONResponse |
||
| 232 | */ |
||
| 233 | public function receiveNotification($notificationType, $resourceType, $providerId, array $notification) { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * map login name to internal LDAP UID if a LDAP backend is in use |
||
| 283 | * |
||
| 284 | * @param string $uid |
||
| 285 | * @return string mixed |
||
| 286 | */ |
||
| 287 | private function mapUid($uid) { |
||
| 299 | |||
| 300 | } |
||
| 301 |