| Conditions | 10 |
| Paths | 9 |
| Total Lines | 85 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 142 | public function httpPost(RequestInterface $request, ResponseInterface $response) { |
||
| 143 | $path = $request->getPath(); |
||
| 144 | |||
| 145 | // Only handling xml |
||
| 146 | $contentType = $request->getHeader('Content-Type'); |
||
| 147 | if (strpos($contentType, 'application/xml') === false && strpos($contentType, 'text/xml') === false) { |
||
| 148 | return; |
||
| 149 | } |
||
| 150 | |||
| 151 | // Making sure the node exists |
||
| 152 | try { |
||
| 153 | $node = $this->server->tree->getNodeForPath($path); |
||
| 154 | } catch (NotFound $e) { |
||
| 155 | return; |
||
| 156 | } |
||
| 157 | |||
| 158 | $requestBody = $request->getBodyAsString(); |
||
| 159 | |||
| 160 | // If this request handler could not deal with this POST request, it |
||
| 161 | // will return 'null' and other plugins get a chance to handle the |
||
| 162 | // request. |
||
| 163 | // |
||
| 164 | // However, we already requested the full body. This is a problem, |
||
| 165 | // because a body can only be read once. This is why we preemptively |
||
| 166 | // re-populated the request body with the existing data. |
||
| 167 | $request->setBody($requestBody); |
||
| 168 | |||
| 169 | $this->server->xml->parse($requestBody, $request->getUrl(), $documentType); |
||
| 170 | |||
| 171 | switch ($documentType) { |
||
| 172 | |||
| 173 | case '{'.self::NS_CALENDARSERVER.'}publish-calendar' : |
||
| 174 | |||
| 175 | // We can only deal with IShareableCalendar objects |
||
| 176 | if (!$node instanceof Calendar) { |
||
| 177 | return; |
||
| 178 | } |
||
| 179 | $this->server->transactionType = 'post-publish-calendar'; |
||
| 180 | |||
| 181 | // Getting ACL info |
||
| 182 | $acl = $this->server->getPlugin('acl'); |
||
| 183 | |||
| 184 | // If there's no ACL support, we allow everything |
||
| 185 | if ($acl) { |
||
|
|
|||
| 186 | $acl->checkPrivileges($path, '{DAV:}write'); |
||
| 187 | } |
||
| 188 | |||
| 189 | $node->setPublishStatus(true); |
||
| 190 | |||
| 191 | // iCloud sends back the 202, so we will too. |
||
| 192 | $response->setStatus(202); |
||
| 193 | |||
| 194 | // Adding this because sending a response body may cause issues, |
||
| 195 | // and I wanted some type of indicator the response was handled. |
||
| 196 | $response->setHeader('X-Sabre-Status', 'everything-went-well'); |
||
| 197 | |||
| 198 | // Breaking the event chain |
||
| 199 | return false; |
||
| 200 | |||
| 201 | case '{'.self::NS_CALENDARSERVER.'}unpublish-calendar' : |
||
| 202 | |||
| 203 | // We can only deal with IShareableCalendar objects |
||
| 204 | if (!$node instanceof Calendar) { |
||
| 205 | return; |
||
| 206 | } |
||
| 207 | $this->server->transactionType = 'post-unpublish-calendar'; |
||
| 208 | |||
| 209 | // Getting ACL info |
||
| 210 | $acl = $this->server->getPlugin('acl'); |
||
| 211 | |||
| 212 | // If there's no ACL support, we allow everything |
||
| 213 | if ($acl) { |
||
| 214 | $acl->checkPrivileges($path, '{DAV:}write'); |
||
| 215 | } |
||
| 216 | |||
| 217 | $node->setPublishStatus(false); |
||
| 218 | |||
| 219 | $response->setStatus(200); |
||
| 220 | |||
| 221 | // Adding this because sending a response body may cause issues, |
||
| 222 | // and I wanted some type of indicator the response was handled. |
||
| 223 | $response->setHeader('X-Sabre-Status', 'everything-went-well'); |
||
| 224 | |||
| 225 | // Breaking the event chain |
||
| 226 | return false; |
||
| 227 | |||
| 231 |