| Total Complexity | 79 |
| Total Lines | 372 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like DocumentAnnotation often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DocumentAnnotation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class DocumentAnnotation |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var null|DocumentAnnotation |
||
| 35 | */ |
||
| 36 | private static $instance; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $annotationData; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var Document |
||
| 45 | */ |
||
| 46 | protected $document; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @access protected |
||
| 50 | * @var Logger This holds the logger |
||
| 51 | */ |
||
| 52 | protected Logger $logger; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param array $annotationData |
||
| 56 | * @param Document $document |
||
| 57 | */ |
||
| 58 | private function __construct($annotationData, $document) |
||
| 59 | { |
||
| 60 | $this->annotationData = $annotationData; |
||
| 61 | $this->document = $document; |
||
| 62 | $this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(static::class); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Returns all annotations with valid targets. |
||
| 67 | * |
||
| 68 | * @return Annotation[]|array |
||
| 69 | */ |
||
| 70 | public function getAnnotations() |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Gets the logicalId related page numbers |
||
| 167 | * |
||
| 168 | * @param string $logicalId |
||
| 169 | * @return array |
||
| 170 | */ |
||
| 171 | protected function getPagesByLogicalId($logicalId) |
||
| 172 | { |
||
| 173 | $pages = []; |
||
| 174 | if ( |
||
| 175 | array_key_exists('l2p', $this->document->getCurrentDocument()->smLinks) && |
||
| 176 | array_key_exists($logicalId, $this->document->getCurrentDocument()->smLinks['l2p']) |
||
| 177 | ) { |
||
| 178 | $physicalIdentifiers = $this->document->getCurrentDocument()->smLinks['l2p'][$logicalId]; |
||
| 179 | foreach ($physicalIdentifiers as $physicalIdentifier) { |
||
| 180 | if (array_key_exists($physicalIdentifier, $this->document->getCurrentDocument()->physicalStructureInfo)) { |
||
| 181 | $order = $this->document->getCurrentDocument()->physicalStructureInfo[$physicalIdentifier]['order']; |
||
| 182 | if (is_numeric($order)) { |
||
| 183 | $pages[] = $order; |
||
| 184 | } |
||
| 185 | } |
||
| 186 | } |
||
| 187 | } |
||
| 188 | return $pages; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Gets the physicalId related page numbers |
||
| 193 | * @param string $physicalId |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | protected function getPagesByPhysicalId($physicalId) |
||
| 197 | { |
||
| 198 | $pages = []; |
||
| 199 | foreach ($this->document->getCurrentDocument()->physicalStructureInfo as $physicalInfo) { |
||
| 200 | $order = $physicalInfo['order']; |
||
| 201 | if (is_numeric($order)) { |
||
| 202 | $pages[] = $order; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | if (array_key_exists($physicalId, $this->document->getCurrentDocument()->physicalStructureInfo)) { |
||
| 206 | if ($this->document->getCurrentDocument()->physicalStructureInfo[$physicalId]['type'] === 'physSequence') { |
||
| 207 | return $pages; |
||
| 208 | } |
||
| 209 | return [$this->document->getCurrentDocument()->physicalStructureInfo[$physicalId]['order']]; |
||
| 210 | } |
||
| 211 | return []; |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Gets the fileId related page numbers |
||
| 216 | * |
||
| 217 | * @param string $fileId |
||
| 218 | * @return array |
||
| 219 | */ |
||
| 220 | protected function getPagesByFileId($fileId) |
||
| 221 | { |
||
| 222 | $pages = []; |
||
| 223 | foreach ($this->document->getCurrentDocument()->physicalStructureInfo as $physicalInfo) { |
||
| 224 | if ( |
||
| 225 | array_key_exists('files', $physicalInfo) && |
||
| 226 | is_array($physicalInfo['files']) && |
||
| 227 | $physicalInfo['type'] !== 'physSequence' |
||
| 228 | ) { |
||
| 229 | foreach ($physicalInfo['files'] as $file) { |
||
| 230 | if ($file === $fileId) { |
||
| 231 | $pages[] = $physicalInfo['order']; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | } |
||
| 235 | } |
||
| 236 | return $pages; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Gets the fileId and audio related page numbers |
||
| 241 | * |
||
| 242 | * @param string $fileId |
||
| 243 | * @param string $range |
||
| 244 | * @return array |
||
| 245 | */ |
||
| 246 | protected function getAudioPagesByFileId($fileId, $range = null) |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Gets the fileId and measure range related page numbers from the musical structMap |
||
| 297 | * |
||
| 298 | * @param string $fileId |
||
| 299 | * @param string $range |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | protected function getMeasurePagesByFileId($fileId, $range = null) |
||
| 303 | { |
||
| 304 | // Get all measures referencing the fileid |
||
| 305 | $measures = []; |
||
| 306 | // Get the related page numbers |
||
| 307 | $measurePages = []; |
||
| 308 | $measureIndex = 1; |
||
| 309 | $startOrder = 0; |
||
| 310 | $endOrder = 0; |
||
| 311 | if ($this->document->getCurrentDocument() instanceof MetsDocument) { |
||
| 312 | foreach ($this->document->getCurrentDocument()->musicalStructureInfo as $key => $musicalInfo) { |
||
| 313 | if ($musicalInfo['type'] === 'measure' && is_array($musicalInfo['files'])) { |
||
| 314 | foreach ($musicalInfo['files'] as $file) { |
||
| 315 | if ($file['fileid'] === $fileId && $file['type'] === 'IDREF') { |
||
| 316 | $measures[] = $musicalInfo; |
||
| 317 | } |
||
| 318 | } |
||
| 319 | if ($measureIndex === 1) { |
||
| 320 | $startOrder = $musicalInfo['order']; |
||
| 321 | } |
||
| 322 | $endOrder = $musicalInfo['order']; |
||
| 323 | $measureIndex += 1; |
||
| 324 | } |
||
| 325 | } |
||
| 326 | // Filter measures by the given range of measure numbers |
||
| 327 | if ($measures && $range && !preg_match("/\ball\b/", $range)) { |
||
| 328 | $measureNumbers = []; |
||
| 329 | $range = preg_replace("/\bend\b/", $endOrder, $range); |
||
| 330 | $range = preg_replace("/\bstart\b/", $startOrder, $range); |
||
| 331 | $ranges = array_map('trim', explode(',', $range)); |
||
| 332 | foreach ($ranges as $measureNumber) { |
||
| 333 | if (preg_match('/\d+-\d+/', $measureNumber)) { |
||
| 334 | list($from, $to) = array_map('trim', explode('-', $measureNumber)); |
||
| 335 | $measureNumbers = array_merge($measureNumbers, range($from, $to)); |
||
| 336 | } else { |
||
| 337 | $measureNumbers[] = (int) $measureNumber; |
||
| 338 | } |
||
| 339 | } |
||
| 340 | foreach ($measures as $key => $measure) { |
||
| 341 | if (!in_array($measure['order'], $measureNumbers)) { |
||
| 342 | unset($measures[$key]); |
||
| 343 | } |
||
| 344 | } |
||
| 345 | } |
||
| 346 | foreach ($measures as $measure) { |
||
| 347 | $measurePages[$measure['order']] = $this->document->getCurrentDocument()->musicalStructure[$measure['order']]['page']; |
||
| 348 | } |
||
| 349 | } |
||
| 350 | return $measurePages; |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Returns the raw data of all annotations with a valid verovio target |
||
| 355 | * |
||
| 356 | * @return array |
||
| 357 | */ |
||
| 358 | public function getVerovioRelevantAnnotations() |
||
| 359 | { |
||
| 360 | $annotations = []; |
||
| 361 | /** @var Annotation $annotation */ |
||
| 362 | foreach ($this->getAnnotations() as $annotation) { |
||
| 363 | if ($annotation->isVerovioRelevant()) { |
||
| 364 | $annotations[] = $annotation->getRawData(); |
||
| 365 | } |
||
| 366 | } |
||
| 367 | return $annotations; |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Loads all annotation data from the annotation server |
||
| 372 | * |
||
| 373 | * @param Document $document |
||
| 374 | * @return array |
||
| 375 | */ |
||
| 376 | protected static function loadData($document) |
||
| 377 | { |
||
| 378 | $annotationData = []; |
||
| 379 | $conf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('dlf'); |
||
| 380 | $apiBaseUrl = $conf['annotationServerUrl']; |
||
| 381 | if ($apiBaseUrl && $document->getCurrentDocument() instanceof MetsDocument) { |
||
| 382 | $purl = $document->getCurrentDocument()->mets->xpath('//mods:mods/mods:identifier[@type="purl"]'); |
||
| 383 | if (count($purl) > 0) { |
||
| 384 | $annotationRequest = new AnnotationRequest($apiBaseUrl); |
||
| 385 | $annotationData = $annotationRequest->getAll((string) $purl[0]); |
||
| 386 | } |
||
| 387 | } |
||
| 388 | return $annotationData; |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param $document |
||
| 393 | * @return DocumentAnnotation|null |
||
| 394 | * |
||
| 395 | */ |
||
| 396 | public static function getInstance($document) |
||
| 403 | } |
||
| 404 | } |
||
| 405 |