Conditions | 8 |
Paths | 10 |
Total Lines | 77 |
Code Lines | 50 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
90 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
91 | { |
||
92 | $tree = $request->getAttribute('tree'); |
||
93 | assert($tree instanceof Tree); |
||
94 | |||
95 | $data_filesystem = Registry::filesystem()->data(); |
||
96 | |||
97 | $format = Validator::parsedBody($request)->isInArray(['gedcom', 'zip'])->requiredString('format'); |
||
98 | $privacy = Validator::parsedBody($request)->isInArray(['none', 'gedadmin', 'user', 'visitor'])->requiredString('privacy'); |
||
99 | $encoding = Validator::parsedBody($request)->isInArray([UTF8::NAME, UTF16BE::NAME, ANSEL::NAME, ASCII::NAME, Windows1252::NAME])->requiredString('encoding'); |
||
100 | $line_endings = Validator::parsedBody($request)->isInArray(['CRLF', 'LF'])->requiredString('line_endings'); |
||
101 | $media_path = Validator::parsedBody($request)->string('media_path') ?? ''; |
||
102 | |||
103 | $access_levels = [ |
||
104 | 'gedadmin' => Auth::PRIV_NONE, |
||
105 | 'user' => Auth::PRIV_USER, |
||
106 | 'visitor' => Auth::PRIV_PRIVATE, |
||
107 | 'none' => Auth::PRIV_HIDE, |
||
108 | ]; |
||
109 | |||
110 | $access_level = $access_levels[$privacy]; |
||
111 | |||
112 | // What to call the downloaded file |
||
113 | $download_filename = $tree->name(); |
||
114 | |||
115 | // Force a ".ged" suffix |
||
116 | if (strtolower(pathinfo($download_filename, PATHINFO_EXTENSION)) !== 'ged') { |
||
1 ignored issue
–
show
|
|||
117 | $download_filename .= '.ged'; |
||
118 | } |
||
119 | |||
120 | if ($format === 'zip') { |
||
121 | $resource = $this->gedcom_export_service->export($tree, true, $encoding, $access_level, $media_path, $line_endings); |
||
122 | |||
123 | $path = $tree->getPreference('MEDIA_DIRECTORY'); |
||
124 | |||
125 | // Create a new/empty .ZIP file |
||
126 | $temp_zip_file = stream_get_meta_data(tmpfile())['uri']; |
||
127 | $zip_provider = new FilesystemZipArchiveProvider($temp_zip_file, 0755); |
||
128 | $zip_adapter = new ZipArchiveAdapter($zip_provider); |
||
129 | $zip_filesystem = new Filesystem($zip_adapter); |
||
130 | $zip_filesystem->writeStream($download_filename, $resource); |
||
131 | fclose($resource); |
||
132 | |||
133 | $media_filesystem = $tree->mediaFilesystem($data_filesystem); |
||
134 | |||
135 | $records = DB::table('media') |
||
136 | ->where('m_file', '=', $tree->id()) |
||
137 | ->get() |
||
138 | ->map(Registry::mediaFactory()->mapper($tree)) |
||
139 | ->filter(GedcomRecord::accessFilter()); |
||
140 | |||
141 | foreach ($records as $record) { |
||
142 | foreach ($record->mediaFiles() as $media_file) { |
||
143 | $from = $media_file->filename(); |
||
144 | $to = $path . $media_file->filename(); |
||
145 | if (!$media_file->isExternal() && $media_filesystem->fileExists($from) && !$zip_filesystem->fileExists($to)) { |
||
146 | $zip_filesystem->writeStream($to, $media_filesystem->readStream($from)); |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 | |||
151 | $stream = $this->stream_factory->createStreamFromFile($temp_zip_file); |
||
152 | $filename = addcslashes($download_filename, '"') . '.zip'; |
||
153 | |||
154 | return $this->response_factory->createResponse() |
||
155 | ->withBody($stream) |
||
156 | ->withHeader('Content-Type', 'application/zip') |
||
157 | ->withHeader('Content-Disposition', 'attachment; filename="' . $filename . '"'); |
||
158 | } |
||
159 | |||
160 | $resource = $this->gedcom_export_service->export($tree, true, $encoding, $access_level, $media_path); |
||
161 | $stream = $this->stream_factory->createStreamFromResource($resource); |
||
162 | |||
163 | return $this->response_factory->createResponse() |
||
164 | ->withBody($stream) |
||
165 | ->withHeader('Content-Type', 'text/x-gedcom; charset=' . UTF8::NAME) |
||
166 | ->withHeader('Content-Disposition', 'attachment; filename="' . addcslashes($download_filename, '"') . '"'); |
||
167 | } |
||
169 |