Conditions | 11 |
Paths | 16 |
Total Lines | 101 |
Code Lines | 61 |
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 |
||
64 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
65 | { |
||
66 | $tree = $request->getAttribute('tree'); |
||
67 | assert($tree instanceof Tree); |
||
68 | |||
69 | $convert = (bool) ($request->getParsedBody()['convert'] ?? false); |
||
70 | $zip = (bool) ($request->getParsedBody()['zip'] ?? false); |
||
71 | $media = (bool) ($request->getParsedBody()['media'] ?? false); |
||
72 | $media_path = $request->getParsedBody()['media-path'] ?? ''; |
||
73 | $privatize_export = $request->getParsedBody()['privatize_export']; |
||
74 | |||
75 | $access_levels = [ |
||
76 | 'gedadmin' => Auth::PRIV_NONE, |
||
77 | 'user' => Auth::PRIV_USER, |
||
78 | 'visitor' => Auth::PRIV_PRIVATE, |
||
79 | 'none' => Auth::PRIV_HIDE, |
||
80 | ]; |
||
81 | |||
82 | $access_level = $access_levels[$privatize_export]; |
||
83 | $encoding = $convert ? 'ANSI' : 'UTF-8'; |
||
84 | |||
85 | // What to call the downloaded file |
||
86 | $download_filename = $tree->name(); |
||
87 | |||
88 | // Force a ".ged" suffix |
||
89 | if (strtolower(pathinfo($download_filename, PATHINFO_EXTENSION)) !== 'ged') { |
||
90 | $download_filename .= '.ged'; |
||
91 | } |
||
92 | |||
93 | if ($zip || $media) { |
||
94 | // Export the GEDCOM to an in-memory stream. |
||
95 | $tmp_stream = tmpfile(); |
||
96 | FunctionsExport::exportGedcom($tree, $tmp_stream, $access_level, $media_path, $encoding); |
||
|
|||
97 | rewind($tmp_stream); |
||
98 | |||
99 | $path = $tree->getPreference('MEDIA_DIRECTORY', 'media/'); |
||
100 | |||
101 | // Create a new/empty .ZIP file |
||
102 | $temp_zip_file = tempnam(sys_get_temp_dir(), 'webtrees-zip-'); |
||
103 | $zip_adapter = new ZipArchiveAdapter($temp_zip_file); |
||
104 | $zip_filesystem = new Filesystem($zip_adapter); |
||
105 | $zip_filesystem->writeStream($download_filename, $tmp_stream); |
||
106 | fclose($tmp_stream); |
||
107 | |||
108 | if ($media) { |
||
109 | $manager = new MountManager([ |
||
110 | 'media' => $tree->mediaFilesystem(), |
||
111 | 'zip' => $zip_filesystem, |
||
112 | ]); |
||
113 | |||
114 | $records = DB::table('media') |
||
115 | ->where('m_file', '=', $tree->id()) |
||
116 | ->get() |
||
117 | ->map(Media::rowMapper()) |
||
118 | ->filter(GedcomRecord::accessFilter()); |
||
119 | |||
120 | foreach ($records as $record) { |
||
121 | foreach ($record->mediaFiles() as $media_file) { |
||
122 | $from = 'media://' . $media_file->filename(); |
||
123 | $to = 'zip://' . $path . $media_file->filename(); |
||
124 | if (!$media_file->isExternal() && $manager->has($from)) { |
||
125 | $manager->copy($from, $to); |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | |||
131 | // Need to force-close ZipArchive filesystems. |
||
132 | $zip_adapter->getArchive()->close(); |
||
133 | |||
134 | // Use a stream, so that we do not have to load the entire file into memory. |
||
135 | $stream = app(StreamFactoryInterface::class)->createStreamFromFile($temp_zip_file); |
||
136 | $filename = addcslashes($download_filename, '"') . '.zip'; |
||
137 | |||
138 | /** @var ResponseFactoryInterface $response_factory */ |
||
139 | $response_factory = app(ResponseFactoryInterface::class); |
||
140 | |||
141 | return $response_factory->createResponse() |
||
142 | ->withBody($stream) |
||
143 | ->withHeader('Content-Type', 'application/zip') |
||
144 | ->withHeader('Content-Disposition', 'attachment; filename="' . $filename . '"'); |
||
145 | } |
||
146 | |||
147 | $resource = fopen('php://temp', 'wb+'); |
||
148 | FunctionsExport::exportGedcom($tree, $resource, $access_level, $media_path, $encoding); |
||
149 | rewind($resource); |
||
150 | |||
151 | $charset = $convert ? 'ISO-8859-1' : 'UTF-8'; |
||
152 | |||
153 | /** @var StreamFactoryInterface $response_factory */ |
||
154 | $stream_factory = app(StreamFactoryInterface::class); |
||
155 | |||
156 | $stream = $stream_factory->createStreamFromResource($resource); |
||
157 | |||
158 | /** @var ResponseFactoryInterface $response_factory */ |
||
159 | $response_factory = app(ResponseFactoryInterface::class); |
||
160 | |||
161 | return $response_factory->createResponse() |
||
162 | ->withBody($stream) |
||
163 | ->withHeader('Content-Type', 'text/x-gedcom; charset=' . $charset) |
||
164 | ->withHeader('Content-Disposition', 'attachment; filename="' . addcslashes($download_filename, '"') . '"'); |
||
165 | } |
||
167 |