| Total Complexity | 40 |
| Total Lines | 404 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like EditMediaController 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 EditMediaController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class EditMediaController extends AbstractEditController |
||
| 47 | { |
||
| 48 | /** @var MediaFileService */ |
||
| 49 | private $media_file_service; |
||
| 50 | |||
| 51 | /** @var PendingChangesService */ |
||
| 52 | private $pending_changes_service; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * EditMediaController constructor. |
||
| 56 | * |
||
| 57 | * @param MediaFileService $media_file_service |
||
| 58 | * @param PendingChangesService $pending_changes_service |
||
| 59 | */ |
||
| 60 | public function __construct(MediaFileService $media_file_service, PendingChangesService $pending_changes_service) |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Add a media file to an existing media object. |
||
| 68 | * |
||
| 69 | * @param ServerRequestInterface $request |
||
| 70 | * |
||
| 71 | * @return ResponseInterface |
||
| 72 | */ |
||
| 73 | public function addMediaFile(ServerRequestInterface $request): ResponseInterface |
||
| 99 | ])); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Add a media file to an existing media object. |
||
| 104 | * |
||
| 105 | * @param ServerRequestInterface $request |
||
| 106 | * |
||
| 107 | * @return ResponseInterface |
||
| 108 | */ |
||
| 109 | public function addMediaFileAction(ServerRequestInterface $request): ResponseInterface |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Edit an existing media file. |
||
| 156 | * |
||
| 157 | * @param ServerRequestInterface $request |
||
| 158 | * |
||
| 159 | * @return ResponseInterface |
||
| 160 | */ |
||
| 161 | public function editMediaFile(ServerRequestInterface $request): ResponseInterface |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Save an edited media file. |
||
| 201 | * |
||
| 202 | * @param ServerRequestInterface $request |
||
| 203 | * |
||
| 204 | * @return ResponseInterface |
||
| 205 | */ |
||
| 206 | public function editMediaFileAction(ServerRequestInterface $request): ResponseInterface |
||
| 207 | { |
||
| 208 | $tree = $request->getAttribute('tree'); |
||
| 209 | assert($tree instanceof Tree); |
||
| 210 | |||
| 211 | $data_filesystem = $request->getAttribute('filesystem.data'); |
||
| 212 | assert($data_filesystem instanceof FilesystemInterface); |
||
| 213 | |||
| 214 | $xref = $request->getQueryParams()['xref']; |
||
| 215 | $fact_id = $request->getQueryParams()['fact_id']; |
||
| 216 | |||
| 217 | $params = (array) $request->getParsedBody(); |
||
| 218 | |||
| 219 | $folder = $params['folder']; |
||
| 220 | $new_file = $params['new_file']; |
||
| 221 | $remote = $params['remote']; |
||
| 222 | $title = $params['title']; |
||
| 223 | $type = $params['type']; |
||
| 224 | $media = Media::getInstance($xref, $tree); |
||
| 225 | |||
| 226 | // Tidy whitespace |
||
| 227 | $type = trim(preg_replace('/\s+/', ' ', $type)); |
||
| 228 | $title = trim(preg_replace('/\s+/', ' ', $title)); |
||
| 229 | |||
| 230 | // Media object oes not exist? Media object is read-only? |
||
| 231 | if ($media === null || $media->isPendingDeletion() || !$media->canEdit()) { |
||
| 232 | return redirect(route('tree-page', ['tree' => $tree->name()])); |
||
| 233 | } |
||
| 234 | |||
| 235 | // Find the fact we are editing. |
||
| 236 | $media_file = null; |
||
| 237 | foreach ($media->mediaFiles() as $tmp) { |
||
| 238 | if ($tmp->factId() === $fact_id) { |
||
| 239 | $media_file = $tmp; |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | // Media file does not exist? |
||
| 244 | if ($media_file === null) { |
||
|
|
|||
| 245 | return redirect(route('tree-page', ['tree' => $tree->name()])); |
||
| 246 | } |
||
| 247 | |||
| 248 | // We can edit the file as either a URL or a folder/file |
||
| 249 | if ($remote !== '') { |
||
| 250 | $file = $remote; |
||
| 251 | } else { |
||
| 252 | $new_file = str_replace('\\', '/', $new_file); |
||
| 253 | $folder = str_replace('\\', '/', $folder); |
||
| 254 | $folder = trim($folder, '/'); |
||
| 255 | |||
| 256 | if ($folder === '') { |
||
| 257 | $file = $new_file; |
||
| 258 | } else { |
||
| 259 | $file = $folder . '/' . $new_file; |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | // Invalid filename? Do not change it. |
||
| 264 | if ($new_file === '') { |
||
| 265 | $file = $media_file->filename(); |
||
| 266 | } |
||
| 267 | |||
| 268 | $filesystem = $media->tree()->mediaFilesystem($data_filesystem); |
||
| 269 | $old = $media_file->filename(); |
||
| 270 | $new = $file; |
||
| 271 | |||
| 272 | // Update the filesystem, if we can. |
||
| 273 | if ($old !== $new && !$media_file->isExternal()) { |
||
| 274 | try { |
||
| 275 | $new = Util::normalizePath($new); |
||
| 276 | $filesystem->rename($old, $new); |
||
| 277 | FlashMessages::addMessage(I18N::translate('The media file %1$s has been renamed to %2$s.', Html::filename($media_file->filename()), Html::filename($file)), 'info'); |
||
| 278 | } catch (FileNotFoundException $ex) { |
||
| 279 | // The "old" file may not exist. For example, if the file was renamed on disk, |
||
| 280 | // and we are now renaming the GEDCOM data to match. |
||
| 281 | } catch (FileExistsException $ex) { |
||
| 282 | // Don't overwrite existing file |
||
| 283 | FlashMessages::addMessage(I18N::translate('The media file %1$s could not be renamed to %2$s.', Html::filename($media_file->filename()), Html::filename($file)), 'info'); |
||
| 284 | $file = $old; |
||
| 285 | } |
||
| 286 | } |
||
| 287 | |||
| 288 | $gedcom = $this->media_file_service->createMediaFileGedcom($file, $type, $title); |
||
| 289 | |||
| 290 | $media->updateFact($fact_id, $gedcom, true); |
||
| 291 | |||
| 292 | // Accept the changes, to keep the filesystem in sync with the GEDCOM data. |
||
| 293 | if ($old !== $new && !$media_file->isExternal()) { |
||
| 294 | $this->pending_changes_service->acceptRecord($media); |
||
| 295 | } |
||
| 296 | |||
| 297 | return redirect($media->url()); |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Show a form to create a new media object. |
||
| 302 | * |
||
| 303 | * @param ServerRequestInterface $request |
||
| 304 | * |
||
| 305 | * @return ResponseInterface |
||
| 306 | */ |
||
| 307 | public function createMediaObject(ServerRequestInterface $request): ResponseInterface |
||
| 319 | ])); |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param ServerRequestInterface $request |
||
| 324 | * |
||
| 325 | * @return ResponseInterface |
||
| 326 | */ |
||
| 327 | public function createMediaObjectFromFileAction(ServerRequestInterface $request): ResponseInterface |
||
| 328 | { |
||
| 329 | $tree = $request->getAttribute('tree'); |
||
| 330 | assert($tree instanceof Tree); |
||
| 331 | |||
| 332 | $params = (array) $request->getParsedBody(); |
||
| 333 | $file = $params['file']; |
||
| 334 | $type = $params['type']; |
||
| 335 | $title = $params['title']; |
||
| 336 | $note = $params['note']; |
||
| 337 | |||
| 338 | if (preg_match('/\.([a-zA-Z0-9]+)$/', $file, $match)) { |
||
| 339 | $format = ' ' . $match[1]; |
||
| 340 | } else { |
||
| 341 | $format = ''; |
||
| 342 | } |
||
| 343 | |||
| 344 | $gedcom = "0 @@ OBJE\n1 FILE " . $file . "\n2 FORM " . $format; |
||
| 345 | |||
| 346 | if ($type !== '') { |
||
| 347 | $gedcom .= "\n3 TYPE " . $type; |
||
| 348 | } |
||
| 349 | |||
| 350 | if ($title !== '') { |
||
| 351 | $gedcom .= "\n2 TITL " . $title; |
||
| 352 | } |
||
| 353 | |||
| 354 | // Convert HTML line endings to GEDCOM continuations |
||
| 355 | $note = strtr($note, ["\r\n" => "\n2 CONT "]); |
||
| 356 | |||
| 357 | if ($note !== '') { |
||
| 358 | $gedcom .= "\n1 NOTE " . $note; |
||
| 359 | } |
||
| 360 | |||
| 361 | $media_object = $tree->createRecord($gedcom); |
||
| 362 | // Accept the new record. Rejecting it would leave the filesystem out-of-sync with the genealogy |
||
| 363 | $this->pending_changes_service->acceptRecord($media_object); |
||
| 364 | |||
| 365 | return redirect($media_object->url()); |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @param ServerRequestInterface $request |
||
| 370 | * |
||
| 371 | * @return ResponseInterface |
||
| 372 | */ |
||
| 373 | public function linkMediaToIndividual(ServerRequestInterface $request): ResponseInterface |
||
| 374 | { |
||
| 375 | $tree = $request->getAttribute('tree'); |
||
| 376 | assert($tree instanceof Tree); |
||
| 377 | |||
| 378 | $xref = $request->getQueryParams()['xref']; |
||
| 379 | $media = Media::getInstance($xref, $tree); |
||
| 380 | |||
| 381 | return response(view('modals/link-media-to-individual', [ |
||
| 382 | 'media' => $media, |
||
| 383 | 'tree' => $tree, |
||
| 384 | ])); |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @param ServerRequestInterface $request |
||
| 389 | * |
||
| 390 | * @return ResponseInterface |
||
| 391 | */ |
||
| 392 | public function linkMediaToFamily(ServerRequestInterface $request): ResponseInterface |
||
| 404 | ])); |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @param ServerRequestInterface $request |
||
| 409 | * |
||
| 410 | * @return ResponseInterface |
||
| 411 | */ |
||
| 412 | public function linkMediaToSource(ServerRequestInterface $request): ResponseInterface |
||
| 424 | ])); |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @param ServerRequestInterface $request |
||
| 429 | * |
||
| 430 | * @return ResponseInterface |
||
| 431 | */ |
||
| 432 | public function linkMediaToRecordAction(ServerRequestInterface $request): ResponseInterface |
||
| 450 | } |
||
| 451 | } |
||
| 452 |