Passed
Push — master ( 89f718...4b3ef6 )
by Greg
06:43
created

ImportThumbnailsAction::handle()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 59
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 36
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 59
rs 8.0555

How to fix   Long Method   

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 <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Fisharebest\Webtrees\Mime;
23
use Fisharebest\Webtrees\Registry;
24
use Fisharebest\Webtrees\Services\PendingChangesService;
25
use Fisharebest\Webtrees\Services\SearchService;
26
use Fisharebest\Webtrees\Services\TreeService;
27
use Psr\Http\Message\ResponseInterface;
28
use Psr\Http\Message\ServerRequestInterface;
29
use Psr\Http\Server\RequestHandlerInterface;
30
31
use function dirname;
32
use function explode;
33
use function response;
34
use function strlen;
35
use function substr;
36
37
/**
38
 * Import custom thumbnails from webtrees 1.x.
39
 */
40
class ImportThumbnailsAction implements RequestHandlerInterface
41
{
42
    /** @var PendingChangesService */
43
    private $pending_changes_service;
44
45
    /** @var TreeService */
46
    private $tree_service;
47
48
    /**
49
     * ImportThumbnailsController constructor.
50
     *
51
     * @param PendingChangesService $pending_changes_service
52
     * @param TreeService           $tree_service
53
     */
54
    public function __construct(
55
        PendingChangesService $pending_changes_service,
56
        TreeService $tree_service
57
    ) {
58
        $this->pending_changes_service = $pending_changes_service;
59
        $this->tree_service            = $tree_service;
60
    }
61
62
    /**
63
     * Import custom thumbnails from webtrees 1.x.
64
     *
65
     * @param ServerRequestInterface $request
66
     *
67
     * @return ResponseInterface
68
     */
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));
0 ignored issues
show
Bug introduced by
It seems like $data_filesystem->read($thumbnail) can also be of type false; however, parameter $string of sha1() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

99
                $sha1      = sha1(/** @scrutinizer ignore-type */ $data_filesystem->read($thumbnail));
Loading history...
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
    }
129
}
130