Completed
Push — master ( 1e9c29...6fd018 )
by Greg
14:03 queued 07:52
created

ImportGedcomAction::handle()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 47
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 30
nc 12
nop 1
dl 0
loc 47
rs 8.1954
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2020 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 Fisharebest\Webtrees\Factory;
23
use Fisharebest\Webtrees\FlashMessages;
24
use Fisharebest\Webtrees\Functions\Functions;
25
use Fisharebest\Webtrees\I18N;
26
use Fisharebest\Webtrees\Services\AdminService;
27
use Fisharebest\Webtrees\Services\TimeoutService;
28
use Fisharebest\Webtrees\Tree;
29
use Nyholm\Psr7\UploadedFile;
30
use Psr\Http\Message\ResponseInterface;
31
use Psr\Http\Message\ServerRequestInterface;
32
use Psr\Http\Message\StreamFactoryInterface;
33
use Psr\Http\Server\RequestHandlerInterface;
34
35
use function app;
36
use function assert;
37
use function basename;
38
use function redirect;
39
use function route;
40
41
use const UPLOAD_ERR_OK;
42
43
/**
44
 * Import a GEDCOM file into a tree.
45
 */
46
class ImportGedcomAction implements RequestHandlerInterface
47
{
48
    /**
49
     * @param ServerRequestInterface $request
50
     *
51
     * @return ResponseInterface
52
     */
53
    public function handle(ServerRequestInterface $request): ResponseInterface
54
    {
55
        $tree = $request->getAttribute('tree');
56
        assert($tree instanceof Tree);
57
58
        $data_filesystem = Factory::filesystem()->data();
59
60
        $params             = (array) $request->getParsedBody();
61
        $source             = $params['source'];
62
        $keep_media         = (bool) ($params['keep_media'] ?? false);
63
        $WORD_WRAPPED_NOTES = (bool) ($params['WORD_WRAPPED_NOTES'] ?? false);
64
        $GEDCOM_MEDIA_PATH  = $params['GEDCOM_MEDIA_PATH'];
65
66
        // Save these choices as defaults
67
        $tree->setPreference('keep_media', $keep_media ? '1' : '0');
68
        $tree->setPreference('WORD_WRAPPED_NOTES', $WORD_WRAPPED_NOTES ? '1' : '0');
69
        $tree->setPreference('GEDCOM_MEDIA_PATH', $GEDCOM_MEDIA_PATH);
70
71
        if ($source === 'client') {
72
            $upload = $request->getUploadedFiles()['tree_name'] ?? null;
73
74
            if ($upload instanceof UploadedFile) {
75
                if ($upload->getError() === UPLOAD_ERR_OK) {
76
                    $tree->importGedcomFile($upload->getStream(), basename($upload->getClientFilename()));
77
                } else {
78
                    FlashMessages::addMessage(Functions::fileUploadErrorText($upload->getError()), 'danger');
79
                }
80
            } else {
81
                FlashMessages::addMessage(I18N::translate('No GEDCOM file was received.'), 'danger');
82
            }
83
        }
84
85
        if ($source === 'server') {
86
            $basename = basename($params['tree_name'] ?? '');
87
88
            if ($basename) {
89
                $resource = $data_filesystem->readStream($basename);
90
                $stream   = app(StreamFactoryInterface::class)->createStreamFromResource($resource);
0 ignored issues
show
Bug introduced by
It seems like $resource can also be of type false; however, parameter $resource of Psr\Http\Message\StreamF...ateStreamFromResource() does only seem to accept resource, 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

90
                $stream   = app(StreamFactoryInterface::class)->createStreamFromResource(/** @scrutinizer ignore-type */ $resource);
Loading history...
91
                $tree->importGedcomFile($stream, $basename);
92
            } else {
93
                FlashMessages::addMessage(I18N::translate('No GEDCOM file was received.'), 'danger');
94
            }
95
        }
96
97
        $url = route(ManageTrees::class, ['tree' => $tree->name()]);
98
99
        return redirect($url);
100
    }
101
}
102