| Conditions | 32 | 
| Paths | 14 | 
| Total Lines | 220 | 
| Code Lines | 123 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 6 | ||
| Bugs | 0 | Features | 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 | ||
| 72 | public function attachmentAction() | ||
| 73 |     { | ||
| 74 | |||
| 75 |         $piVars = GeneralUtility::_GP('tx_dpf'); // get GET params from powermail | ||
| 76 | |||
| 77 | $fedoraHost = $this->clientConfigurationManager->getFedoraHost(); | ||
| 78 | |||
| 79 |         if ($this->isForbidden($piVars['action'])) { | ||
| 80 | $this->response->setStatus(403); | ||
|  | |||
| 81 | return 'Forbidden'; | ||
| 82 | } | ||
| 83 | |||
| 84 | $isRepositoryObject = !is_numeric($piVars['qid']); | ||
| 85 | |||
| 86 | $fedoraNamespace = $this->clientConfigurationManager->getFedoraNamespace(); | ||
| 87 | |||
| 88 |         switch ($piVars['action']) { | ||
| 89 | case 'mets': | ||
| 90 |                 $path = rtrim('http://' . $fedoraHost, | ||
| 91 | "/") . '/fedora/objects/' . $piVars['qid'] . '/methods/' . $fedoraNamespace . ':SDef/getMETSDissemination?supplement=yes'; | ||
| 92 | break; | ||
| 93 | |||
| 94 | case 'preview': | ||
| 95 | // Fixme: Can be removed due to the details page. | ||
| 96 | $document = $this->documentRepository->findByUid($piVars['qid']); | ||
| 97 | |||
| 98 |                 if ($document) { | ||
| 99 | |||
| 100 | $metsXml = $this->buildMetsXml($document); | ||
| 101 |                     $this->response->setHeader('Content-Type', 'text/xml; charset=UTF-8'); | ||
| 102 | return $metsXml; | ||
| 103 | |||
| 104 |                 } else { | ||
| 105 | |||
| 106 | $this->response->setStatus(404); | ||
| 107 | return 'No such document'; | ||
| 108 | |||
| 109 | } | ||
| 110 | |||
| 111 | case 'attachment': | ||
| 112 | |||
| 113 | $qid = $piVars['qid']; | ||
| 114 | |||
| 115 | $attachment = $piVars['attachment']; | ||
| 116 | |||
| 117 |                 if (is_numeric($piVars['qid'])) { | ||
| 118 | |||
| 119 | // qid is local uid | ||
| 120 | $document = $this->documentRepository->findByUid($piVars['qid']); | ||
| 121 | |||
| 122 | /** @var File $file */ | ||
| 123 |                     if (is_a($this->getFile(), '\TYPO3\CMS\Extbase\Persistence\ObjectStorage')) { | ||
| 124 |                         foreach ($document->getFile() as $file) { | ||
| 125 |                             if (!$file->isFileGroupDeleted()) { | ||
| 126 |                                 if ($file->getDownload()) { | ||
| 127 |                                     if ($file->getDatastreamIdentifier() == $attachment) { | ||
| 128 | $path = $file->getUrl(); | ||
| 129 | $contentType = $file->getContentType(); | ||
| 130 | break; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | } | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 |                 } else { | ||
| 138 | |||
| 139 |                     $path = rtrim('http://' . $fedoraHost, | ||
| 140 | "/") . '/fedora/objects/' . $qid . '/datastreams/' . $attachment . '/content'; | ||
| 141 | |||
| 142 | } | ||
| 143 | |||
| 144 |                 if (empty($path)) { | ||
| 145 | $this->response->setStatus(404); | ||
| 146 | return 'No file found'; | ||
| 147 | } | ||
| 148 | |||
| 149 | break; | ||
| 150 | |||
| 151 | case 'dataCite': | ||
| 152 | |||
| 153 | $qid = $piVars['qid']; | ||
| 154 |                 $source = explode(':', $qid); | ||
| 155 |                 if ($source[0] == $fedoraNamespace) { | ||
| 156 | |||
| 157 |                     $path = rtrim('http://' . $fedoraHost, | ||
| 158 | "/") . '/fedora/objects/' . $piVars['qid'] . '/methods/' . $fedoraNamespace . ':SDef/getMETSDissemination?supplement=yes'; | ||
| 159 |                     $metsXml = str_replace('&', '&', file_get_contents($path)); | ||
| 160 | $dataCiteXml = \EWW\Dpf\Helper\DataCiteXml::convertFromMetsXml($metsXml); | ||
| 161 | |||
| 162 |                 } elseif ($document = $this->documentRepository->findByUid($piVars['qid'])) { | ||
| 163 | |||
| 164 |                     $metsXml = str_replace('&', '&', $this->buildMetsXml($document)); | ||
| 165 | $dataCiteXml = \EWW\Dpf\Helper\DataCiteXml::convertFromMetsXml($metsXml); | ||
| 166 | |||
| 167 |                 } else { | ||
| 168 | |||
| 169 | $this->response->setStatus(404); | ||
| 170 | return 'No such document'; | ||
| 171 | |||
| 172 | } | ||
| 173 |                 $dom = new \DOMDocument('1.0', 'UTF-8'); | ||
| 174 | $dom->loadXML($dataCiteXml); | ||
| 175 |                 $title = $dom->getElementsByTagName('title')[0]; | ||
| 176 | |||
| 177 |                 $this->response->setHeader('Content-Disposition', | ||
| 178 | 'attachment; filename="' . self::sanitizeFilename($title->nodeValue) . '.DataCite.xml"'); | ||
| 179 |                 $this->response->setHeader('Content-Type', 'text/xml; charset=UTF-8'); | ||
| 180 | return $dataCiteXml; | ||
| 181 | |||
| 182 | break; | ||
| 183 | |||
| 184 | case 'zip': | ||
| 185 | // FIXME Service locations on Fedora host are hard coded | ||
| 186 |                 $metsUrl = rtrim('http://' . $fedoraHost, "/") . '/mets?pid=' . $piVars['qid']; | ||
| 187 |                 $path = rtrim('http://' . $fedoraHost, "/") . '/zip?xmdpfilter=true&metsurl=' . rawurlencode($metsUrl); | ||
| 188 | break; | ||
| 189 | |||
| 190 | default: | ||
| 191 | |||
| 192 | $this->response->setStatus(404); | ||
| 193 | |||
| 194 | return 'No such action'; | ||
| 195 | } | ||
| 196 | |||
| 197 | // stop here, if inactive Fedora objects are not allowed to be disseminated | ||
| 198 | |||
| 199 | // allow dissemination if a request parameter 'deliverInactive' has the secret | ||
| 200 | // TYPOScript configuration value 'deliverInactiveSecretKey' | ||
| 201 | |||
| 202 | $restrictToActiveDocuments = true; | ||
| 203 | $deliverInactiveSecretKey = $this->settings['deliverInactiveSecretKey']; | ||
| 204 | |||
| 205 |         if ($deliverInactiveSecretKey == $piVars['deliverInactive']) { | ||
| 206 | $restrictToActiveDocuments = false; | ||
| 207 | } | ||
| 208 | |||
| 209 |         if (true === $isRepositoryObject) { | ||
| 210 |             if (true === $restrictToActiveDocuments) { | ||
| 211 | // if restriction applies, check object state before dissemination | ||
| 212 |                 $objectProfileURI = rtrim('http://' . $fedoraHost, | ||
| 213 | "/") . '/fedora/objects/' . $piVars['qid'] . '?format=XML'; | ||
| 214 | $objectProfileXML = file_get_contents($objectProfileURI); | ||
| 215 |                 if (false !== $objectProfileXML) { | ||
| 216 |                     $objectProfileDOM = new \DOMDocument('1.0', 'UTF-8'); | ||
| 217 |                     if (true === $objectProfileDOM->loadXML($objectProfileXML)) { | ||
| 218 |                         $objectState = $objectProfileDOM->getElementsByTagName('objState')[0]; | ||
| 219 |                         if ('I' === $objectState->nodeValue) { | ||
| 220 | $this->response->setStatus(403); | ||
| 221 | return 'Forbidden'; | ||
| 222 | } | ||
| 223 |                         if ('D' === $objectState->nodeValue) { | ||
| 224 | $this->response->setStatus(404); | ||
| 225 | return 'Not Found'; | ||
| 226 | } | ||
| 227 | } | ||
| 228 |                 } else { | ||
| 229 | $this->response->setStatus(500); | ||
| 230 | return 'Internal Server Error'; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | } | ||
| 234 | |||
| 235 | // get remote header and set it before passtrough | ||
| 236 | $headers = get_headers($path); | ||
| 237 | |||
| 238 |         if (false === $headers) { | ||
| 239 | $this->response->setStatus(500); | ||
| 240 | return 'Error while fetching headers'; | ||
| 241 | } | ||
| 242 | |||
| 243 | $contentDispFlag = false; | ||
| 244 | $contentTypeFlag = false; | ||
| 245 | |||
| 246 |         foreach ($headers as $value) { | ||
| 247 | |||
| 248 |             if (false !== stripos($value, 'Content-Disposition')) { | ||
| 249 | header($value); | ||
| 250 | $contentDispFlag = true; | ||
| 251 | continue; | ||
| 252 | } | ||
| 253 | |||
| 254 |             if (false !== stripos($value, 'Content-Type')) { | ||
| 255 | header($value); | ||
| 256 | $contentTypeFlag = true; | ||
| 257 | continue; | ||
| 258 | } | ||
| 259 | |||
| 260 |             if (false !== stripos($value, 'Content-Length')) { | ||
| 261 | header($value); | ||
| 262 | continue; | ||
| 263 | } | ||
| 264 | } | ||
| 265 | |||
| 266 |         if (!$contentDispFlag) { | ||
| 267 |             header('Content-Disposition: attachment'); | ||
| 268 | } | ||
| 269 | |||
| 270 |         if (!$contentTypeFlag) { | ||
| 271 |             header('Content-Type: ' . $contentType); | ||
| 272 | } | ||
| 273 | |||
| 274 |         if ($stream = fopen($path, 'r')) { | ||
| 275 | |||
| 276 | // close active session if any | ||
| 277 | session_write_close(); | ||
| 278 | |||
| 279 | // stop output buffering | ||
| 280 | ob_end_clean(); | ||
| 281 | |||
| 282 | fpassthru($stream); | ||
| 283 | |||
| 284 | fclose($stream); | ||
| 285 | |||
| 286 | // Hard exit PHP script to avoid sending TYPO3 framework HTTP artifacts | ||
| 287 | exit; | ||
| 288 | |||
| 289 |         } else { | ||
| 290 | $this->response->setStatus(500); | ||
| 291 | return 'Error while opening stream'; | ||
| 292 | } | ||
| 336 |