| Conditions | 14 |
| Paths | 98 |
| Total Lines | 91 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 76 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 77 | { |
||
| 78 | $tree = $request->getAttribute('tree'); |
||
| 79 | assert($tree instanceof Tree); |
||
| 80 | |||
| 81 | $xref = $request->getAttribute('xref'); |
||
| 82 | assert(is_string($xref)); |
||
| 83 | |||
| 84 | $fact_id = $request->getAttribute('fact_id'); |
||
| 85 | assert(is_string($fact_id)); |
||
| 86 | |||
| 87 | $data_filesystem = Registry::filesystem()->data(); |
||
| 88 | |||
| 89 | $params = (array) $request->getParsedBody(); |
||
| 90 | $folder = $params['folder'] ?? ''; |
||
| 91 | $new_file = $params['new_file'] ?? ''; |
||
| 92 | $remote = $params['remote'] ?? ''; |
||
| 93 | $title = $params['title'] ?? ''; |
||
| 94 | $type = $params['type'] ?? ''; |
||
| 95 | $media = Registry::mediaFactory()->make($xref, $tree); |
||
| 96 | |||
| 97 | // Tidy non-printing characters |
||
| 98 | $type = trim(preg_replace('/\s+/', ' ', $type)); |
||
| 99 | $title = trim(preg_replace('/\s+/', ' ', $title)); |
||
| 100 | |||
| 101 | // Media object oes not exist? Media object is read-only? |
||
| 102 | if ($media === null || $media->isPendingDeletion() || !$media->canEdit()) { |
||
| 103 | return redirect(route(TreePage::class, ['tree' => $tree->name()])); |
||
| 104 | } |
||
| 105 | |||
| 106 | // Find the fact to edit |
||
| 107 | $media_file = $media->mediaFiles() |
||
| 108 | ->first(static function (MediaFile $media_file) use ($fact_id): bool { |
||
| 109 | return $media_file->factId() === $fact_id; |
||
| 110 | }); |
||
| 111 | |||
| 112 | // Media file does not exist? |
||
| 113 | if ($media_file === null) { |
||
| 114 | return redirect(route(TreePage::class, ['tree' => $tree->name()])); |
||
| 115 | } |
||
| 116 | |||
| 117 | // We can edit the file as either a URL or a folder/file |
||
| 118 | if ($remote !== '') { |
||
| 119 | $file = $remote; |
||
| 120 | } else { |
||
| 121 | $new_file = str_replace('\\', '/', $new_file); |
||
| 122 | $folder = str_replace('\\', '/', $folder); |
||
| 123 | $folder = trim($folder, '/'); |
||
| 124 | |||
| 125 | if ($folder === '') { |
||
| 126 | $file = $new_file; |
||
| 127 | } else { |
||
| 128 | $file = $folder . '/' . $new_file; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | // Invalid filename? Do not change it. |
||
| 133 | if ($new_file === '') { |
||
| 134 | $file = $media_file->filename(); |
||
| 135 | } |
||
| 136 | |||
| 137 | $filesystem = $media->tree()->mediaFilesystem($data_filesystem); |
||
| 138 | $old = $media_file->filename(); |
||
| 139 | $new = $file; |
||
| 140 | |||
| 141 | // Update the filesystem, if we can. |
||
| 142 | if ($old !== $new && !$media_file->isExternal()) { |
||
| 143 | try { |
||
| 144 | $new = Util::normalizePath($new); |
||
| 145 | $filesystem->rename($old, $new); |
||
| 146 | FlashMessages::addMessage(I18N::translate('The media file %1$s has been renamed to %2$s.', Html::filename($media_file->filename()), Html::filename($file)), 'info'); |
||
| 147 | } catch (FileNotFoundException $ex) { |
||
| 148 | // The "old" file may not exist. For example, if the file was renamed on disk, |
||
| 149 | // and we are now renaming the GEDCOM data to match. |
||
| 150 | } catch (FileExistsException $ex) { |
||
| 151 | // Don't overwrite existing file |
||
| 152 | 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'); |
||
| 153 | $file = $old; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | $gedcom = $this->media_file_service->createMediaFileGedcom($file, $type, $title, ''); |
||
| 158 | |||
| 159 | $media->updateFact($fact_id, $gedcom, true); |
||
| 160 | |||
| 161 | // Accept the changes, to keep the filesystem in sync with the GEDCOM data. |
||
| 162 | if ($old !== $new && !$media_file->isExternal()) { |
||
| 163 | $this->pending_changes_service->acceptRecord($media); |
||
| 164 | } |
||
| 165 | |||
| 166 | return redirect($media->url()); |
||
| 167 | } |
||
| 169 |