| Conditions | 10 |
| Paths | 9 |
| Total Lines | 88 |
| Code Lines | 35 |
| Lines | 56 |
| Ratio | 63.64 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 139 | public function httpPost(RequestInterface $request, ResponseInterface $response) { |
||
| 140 | $path = $request->getPath(); |
||
| 141 | |||
| 142 | // Only handling xml |
||
| 143 | $contentType = $request->getHeader('Content-Type'); |
||
| 144 | View Code Duplication | if (strpos($contentType, 'application/xml') === false && strpos($contentType, 'text/xml') === false) { |
|
|
|
|||
| 145 | return; |
||
| 146 | } |
||
| 147 | |||
| 148 | // Making sure the node exists |
||
| 149 | try { |
||
| 150 | $node = $this->server->tree->getNodeForPath($path); |
||
| 151 | } catch (NotFound $e) { |
||
| 152 | return; |
||
| 153 | } |
||
| 154 | |||
| 155 | $requestBody = $request->getBodyAsString(); |
||
| 156 | |||
| 157 | // If this request handler could not deal with this POST request, it |
||
| 158 | // will return 'null' and other plugins get a chance to handle the |
||
| 159 | // request. |
||
| 160 | // |
||
| 161 | // However, we already requested the full body. This is a problem, |
||
| 162 | // because a body can only be read once. This is why we preemptively |
||
| 163 | // re-populated the request body with the existing data. |
||
| 164 | $request->setBody($requestBody); |
||
| 165 | |||
| 166 | $this->server->xml->parse($requestBody, $request->getUrl(), $documentType); |
||
| 167 | |||
| 168 | switch ($documentType) { |
||
| 169 | |||
| 170 | View Code Duplication | case '{'.self::NS_CALENDARSERVER.'}publish-calendar' : |
|
| 171 | |||
| 172 | // We can only deal with IShareableCalendar objects |
||
| 173 | if (!$node instanceof Calendar) { |
||
| 174 | return; |
||
| 175 | } |
||
| 176 | $this->server->transactionType = 'post-publish-calendar'; |
||
| 177 | |||
| 178 | // Getting ACL info |
||
| 179 | $acl = $this->server->getPlugin('acl'); |
||
| 180 | |||
| 181 | // If there's no ACL support, we allow everything |
||
| 182 | if ($acl) { |
||
| 183 | $acl->checkPrivileges($path, '{DAV:}write'); |
||
| 184 | } |
||
| 185 | |||
| 186 | $node->setPublishStatus(true); |
||
| 187 | |||
| 188 | // iCloud sends back the 202, so we will too. |
||
| 189 | $response->setStatus(202); |
||
| 190 | |||
| 191 | // Adding this because sending a response body may cause issues, |
||
| 192 | // and I wanted some type of indicator the response was handled. |
||
| 193 | $response->setHeader('X-Sabre-Status', 'everything-went-well'); |
||
| 194 | |||
| 195 | // Breaking the event chain |
||
| 196 | return false; |
||
| 197 | |||
| 198 | View Code Duplication | case '{'.self::NS_CALENDARSERVER.'}unpublish-calendar' : |
|
| 199 | |||
| 200 | // We can only deal with IShareableCalendar objects |
||
| 201 | if (!$node instanceof Calendar) { |
||
| 202 | return; |
||
| 203 | } |
||
| 204 | $this->server->transactionType = 'post-unpublish-calendar'; |
||
| 205 | |||
| 206 | // Getting ACL info |
||
| 207 | $acl = $this->server->getPlugin('acl'); |
||
| 208 | |||
| 209 | // If there's no ACL support, we allow everything |
||
| 210 | if ($acl) { |
||
| 211 | $acl->checkPrivileges($path, '{DAV:}write'); |
||
| 212 | } |
||
| 213 | |||
| 214 | $node->setPublishStatus(false); |
||
| 215 | |||
| 216 | $response->setStatus(200); |
||
| 217 | |||
| 218 | // Adding this because sending a response body may cause issues, |
||
| 219 | // and I wanted some type of indicator the response was handled. |
||
| 220 | $response->setHeader('X-Sabre-Status', 'everything-went-well'); |
||
| 221 | |||
| 222 | // Breaking the event chain |
||
| 223 | return false; |
||
| 224 | |||
| 225 | } |
||
| 226 | } |
||
| 227 | } |
||
| 228 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.