| Conditions | 9 |
| Paths | 7 |
| Total Lines | 59 |
| Code Lines | 36 |
| 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 |
||
| 69 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 70 | { |
||
| 71 | $data_filesystem = Registry::filesystem()->data(); |
||
| 72 | |||
| 73 | $params = (array) $request->getParsedBody(); |
||
| 74 | |||
| 75 | $thumbnail = $params['thumbnail']; |
||
| 76 | $action = $params['action']; |
||
| 77 | $xrefs = $params['xref']; |
||
| 78 | $geds = $params['ged']; |
||
| 79 | |||
| 80 | if (!$data_filesystem->has($thumbnail)) { |
||
| 81 | return response([]); |
||
| 82 | } |
||
| 83 | |||
| 84 | $media_objects = []; |
||
| 85 | |||
| 86 | foreach ($xrefs as $key => $xref) { |
||
| 87 | $tree = $this->tree_service->all()->get($geds[$key]); |
||
| 88 | $media_objects[] = Registry::mediaFactory()->make($xref, $tree); |
||
| 89 | } |
||
| 90 | |||
| 91 | switch ($action) { |
||
| 92 | case 'delete': |
||
| 93 | $data_filesystem->delete($thumbnail); |
||
| 94 | break; |
||
| 95 | |||
| 96 | case 'add': |
||
| 97 | $mime_type = $data_filesystem->getMimetype($thumbnail) ?: Mime::DEFAULT_TYPE; |
||
| 98 | $directory = dirname($thumbnail, 2); |
||
| 99 | $sha1 = sha1($data_filesystem->read($thumbnail)); |
||
|
|
|||
| 100 | $extension = explode('/', $mime_type)[1]; |
||
| 101 | $move_to = $directory . '/' . $sha1 . '.' . $extension; |
||
| 102 | |||
| 103 | $data_filesystem->rename($thumbnail, $move_to); |
||
| 104 | |||
| 105 | foreach ($media_objects as $media_object) { |
||
| 106 | $prefix = $media_object->tree()->getPreference('MEDIA_DIRECTORY'); |
||
| 107 | $gedcom = '1 FILE ' . substr($move_to, strlen($prefix)) . "\n2 FORM " . $extension; |
||
| 108 | |||
| 109 | if ($media_object->firstImageFile() === null) { |
||
| 110 | // The media object doesn't have an image. Add this as a secondary file. |
||
| 111 | $media_object->createFact($gedcom, true); |
||
| 112 | } else { |
||
| 113 | // The media object already has an image. Show this custom one in preference. |
||
| 114 | $gedcom = '0 @' . $media_object->xref() . "@ OBJE\n" . $gedcom; |
||
| 115 | foreach ($media_object->facts() as $fact) { |
||
| 116 | $gedcom .= "\n" . $fact->gedcom(); |
||
| 117 | } |
||
| 118 | $media_object->updateRecord($gedcom, true); |
||
| 119 | } |
||
| 120 | |||
| 121 | // Accept the changes, to keep the filesystem in sync with the GEDCOM data. |
||
| 122 | $this->pending_changes_service->acceptRecord($media_object); |
||
| 123 | } |
||
| 124 | break; |
||
| 125 | } |
||
| 126 | |||
| 127 | return response([]); |
||
| 128 | } |
||
| 130 |