Completed
Push — master ( 4a8100...5cd281 )
by Greg
07:25
created

ImportGedcomAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\FlashMessages;
23
use Fisharebest\Webtrees\Functions\Functions;
24
use Fisharebest\Webtrees\I18N;
25
use Fisharebest\Webtrees\Registry;
26
use Fisharebest\Webtrees\Services\TreeService;
27
use Fisharebest\Webtrees\Tree;
28
use Nyholm\Psr7\UploadedFile;
29
use Psr\Http\Message\ResponseInterface;
30
use Psr\Http\Message\ServerRequestInterface;
31
use Psr\Http\Message\StreamFactoryInterface;
32
use Psr\Http\Server\RequestHandlerInterface;
33
34
use function app;
35
use function assert;
36
use function basename;
37
use function redirect;
38
use function route;
39
40
use const UPLOAD_ERR_OK;
41
42
/**
43
 * Import a GEDCOM file into a tree.
44
 */
45
class ImportGedcomAction implements RequestHandlerInterface
46
{
47
    /** @var TreeService */
48
    private $tree_service;
49
50
    public function __construct(TreeService $tree_service)
51
    {
52
        $this->tree_service = $tree_service;
53
    }
54
55
    /**
56
     * @param ServerRequestInterface $request
57
     *
58
     * @return ResponseInterface
59
     */
60
    public function handle(ServerRequestInterface $request): ResponseInterface
61
    {
62
        $tree = $request->getAttribute('tree');
63
        assert($tree instanceof Tree);
64
65
        $data_filesystem = Registry::filesystem()->data();
66
67
        $params             = (array) $request->getParsedBody();
68
        $source             = $params['source'];
69
        $keep_media         = (bool) ($params['keep_media'] ?? false);
70
        $WORD_WRAPPED_NOTES = (bool) ($params['WORD_WRAPPED_NOTES'] ?? false);
71
        $GEDCOM_MEDIA_PATH  = $params['GEDCOM_MEDIA_PATH'];
72
73
        // Save these choices as defaults
74
        $tree->setPreference('keep_media', $keep_media ? '1' : '0');
75
        $tree->setPreference('WORD_WRAPPED_NOTES', $WORD_WRAPPED_NOTES ? '1' : '0');
76
        $tree->setPreference('GEDCOM_MEDIA_PATH', $GEDCOM_MEDIA_PATH);
77
78
        if ($source === 'client') {
79
            $upload = $request->getUploadedFiles()['tree_name'] ?? null;
80
81
            if ($upload instanceof UploadedFile) {
82
                if ($upload->getError() === UPLOAD_ERR_OK) {
83
                    $this->tree_service->importGedcomFile($tree, $upload->getStream(), basename($upload->getClientFilename()));
84
                } else {
85
                    FlashMessages::addMessage(Functions::fileUploadErrorText($upload->getError()), 'danger');
86
                }
87
            } else {
88
                FlashMessages::addMessage(I18N::translate('No GEDCOM file was received.'), 'danger');
89
            }
90
        }
91
92
        if ($source === 'server') {
93
            $basename = basename($params['tree_name'] ?? '');
94
95
            if ($basename) {
96
                $resource = $data_filesystem->readStream($basename);
97
                $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

97
                $stream   = app(StreamFactoryInterface::class)->createStreamFromResource(/** @scrutinizer ignore-type */ $resource);
Loading history...
98
                $this->tree_service->importGedcomFile($tree, $stream, $basename);
99
            } else {
100
                FlashMessages::addMessage(I18N::translate('No GEDCOM file was received.'), 'danger');
101
            }
102
        }
103
104
        $url = route(ManageTrees::class, ['tree' => $tree->name()]);
105
106
        return redirect($url);
107
    }
108
}
109