Conditions | 13 |
Paths | 182 |
Total Lines | 79 |
Code Lines | 47 |
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 |
||
73 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
74 | { |
||
75 | $data_filesystem = Registry::filesystem()->data(); |
||
76 | |||
77 | $params = (array) $request->getParsedBody(); |
||
78 | |||
79 | $thumbnail = $params['thumbnail']; |
||
80 | $action = $params['action']; |
||
81 | $xrefs = $params['xref']; |
||
82 | $geds = $params['ged']; |
||
83 | |||
84 | try { |
||
85 | $file_exists = $data_filesystem->fileExists($thumbnail); |
||
86 | } catch (FilesystemException | UnableToRetrieveMetadata $ex) { |
||
87 | $file_exists = false; |
||
88 | } |
||
89 | |||
90 | if (!$file_exists) { |
||
91 | return response([]); |
||
92 | } |
||
93 | |||
94 | $media_objects = []; |
||
95 | |||
96 | foreach ($xrefs as $key => $xref) { |
||
97 | $tree = $this->tree_service->all()->get($geds[$key]); |
||
98 | $media_objects[] = Registry::mediaFactory()->make($xref, $tree); |
||
99 | } |
||
100 | |||
101 | switch ($action) { |
||
102 | case 'delete': |
||
103 | try { |
||
104 | $data_filesystem->delete($thumbnail); |
||
105 | } catch (FilesystemException | UnableToDeleteFile $ex) { |
||
106 | // Cannot delete the file. Leave it there. |
||
107 | } |
||
108 | break; |
||
109 | |||
110 | case 'add': |
||
111 | try { |
||
112 | $mime_type = $data_filesystem->mimeType($thumbnail) ?: Mime::DEFAULT_TYPE; |
||
113 | } catch (FilesystemException | UnableToRetrieveMetadata $ex) { |
||
114 | $mime_type = Mime::DEFAULT_TYPE; |
||
115 | } |
||
116 | |||
117 | try { |
||
118 | $directory = dirname($thumbnail, 2); |
||
119 | $sha1 = sha1($data_filesystem->read($thumbnail)); |
||
120 | $extension = explode('/', $mime_type)[1]; |
||
121 | $move_to = $directory . '/' . $sha1 . '.' . $extension; |
||
122 | |||
123 | $data_filesystem->move($thumbnail, $move_to); |
||
124 | |||
125 | foreach ($media_objects as $media_object) { |
||
126 | $prefix = $media_object->tree()->getPreference('MEDIA_DIRECTORY'); |
||
127 | $gedcom = '1 FILE ' . substr($move_to, strlen($prefix)) . "\n2 FORM " . $extension; |
||
128 | |||
129 | if ($media_object->firstImageFile() === null) { |
||
130 | // The media object doesn't have an image. Add this as a secondary file. |
||
131 | $media_object->createFact($gedcom, true); |
||
132 | } else { |
||
133 | // The media object already has an image. Show this custom one in preference. |
||
134 | $gedcom = '0 @' . $media_object->xref() . "@ OBJE\n" . $gedcom; |
||
135 | foreach ($media_object->facts() as $fact) { |
||
136 | $gedcom .= "\n" . $fact->gedcom(); |
||
137 | } |
||
138 | $media_object->updateRecord($gedcom, true); |
||
139 | } |
||
140 | |||
141 | // Accept the changes, to keep the filesystem in sync with the GEDCOM data. |
||
142 | $this->pending_changes_service->acceptRecord($media_object); |
||
143 | } |
||
144 | } catch (FilesystemException | UnableToReadFile | UnableToMoveFile $ex) { |
||
145 | // Cannot import the file? |
||
146 | } |
||
147 | |||
148 | break; |
||
149 | } |
||
150 | |||
151 | return response([]); |
||
152 | } |
||
154 |