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