Completed
Push — master ( 9dce57...ddb44b )
by Greg
12:52 queued 06:45
created

EditMediaFileAction::handle()   C

Complexity

Conditions 14
Paths 98

Size

Total Lines 91
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 52
c 1
b 0
f 0
nc 98
nop 1
dl 0
loc 91
rs 6.2666

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2021 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Exception;
23
use Fisharebest\Webtrees\FlashMessages;
24
use Fisharebest\Webtrees\Html;
25
use Fisharebest\Webtrees\I18N;
26
use Fisharebest\Webtrees\MediaFile;
27
use Fisharebest\Webtrees\Registry;
28
use Fisharebest\Webtrees\Services\MediaFileService;
29
use Fisharebest\Webtrees\Services\PendingChangesService;
30
use Fisharebest\Webtrees\Tree;
31
use League\Flysystem\FileExistsException;
32
use League\Flysystem\FileNotFoundException;
33
use League\Flysystem\Util;
34
use Psr\Http\Message\ResponseInterface;
35
use Psr\Http\Message\ServerRequestInterface;
36
use Psr\Http\Server\RequestHandlerInterface;
37
38
use function assert;
39
use function is_string;
40
use function preg_replace;
41
use function redirect;
42
use function route;
43
use function str_replace;
44
use function trim;
45
46
/**
47
 * Edit a media file.
48
 */
49
class EditMediaFileAction implements RequestHandlerInterface
50
{
51
    /** @var MediaFileService */
52
    private $media_file_service;
53
54
    /** @var PendingChangesService */
55
    private $pending_changes_service;
56
57
    /**
58
     * EditMediaFileAction constructor.
59
     *
60
     * @param MediaFileService      $media_file_service
61
     * @param PendingChangesService $pending_changes_service
62
     */
63
    public function __construct(MediaFileService $media_file_service, PendingChangesService $pending_changes_service)
64
    {
65
        $this->media_file_service      = $media_file_service;
66
        $this->pending_changes_service = $pending_changes_service;
67
    }
68
69
    /**
70
     * Save an edited media file.
71
     *
72
     * @param ServerRequestInterface $request
73
     *
74
     * @return ResponseInterface
75
     */
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
    }
168
}
169