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

SynchronizeTrees   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 72
rs 10
c 1
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B handle() 0 39 7
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 Fig\Http\Message\StatusCodeInterface;
23
use Fisharebest\Webtrees\Factory;
24
use Fisharebest\Webtrees\FlashMessages;
25
use Fisharebest\Webtrees\I18N;
26
use Fisharebest\Webtrees\Services\AdminService;
27
use Fisharebest\Webtrees\Services\TimeoutService;
28
use Fisharebest\Webtrees\Services\TreeService;
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 redirect;
36
use function route;
37
38
/**
39
 * Synchronize GEDCOM files with trees.
40
 */
41
class SynchronizeTrees implements RequestHandlerInterface
42
{
43
    /** @var AdminService */
44
    private $admin_service;
45
46
    /** @var TimeoutService */
47
    private $timeout_service;
48
49
    /** @var TreeService */
50
    private $tree_service;
51
52
    /**
53
     * AdminTreesController constructor.
54
     *
55
     * @param AdminService        $admin_service
56
     * @param TimeoutService      $timeout_service
57
     * @param TreeService         $tree_service
58
     */
59
    public function __construct(
60
        AdminService $admin_service,
61
        TimeoutService $timeout_service,
62
        TreeService $tree_service
63
    ) {
64
        $this->admin_service   = $admin_service;
65
        $this->timeout_service = $timeout_service;
66
        $this->tree_service    = $tree_service;
67
    }
68
69
    /**
70
     * @param ServerRequestInterface $request
71
     *
72
     * @return ResponseInterface
73
     */
74
    public function handle(ServerRequestInterface $request): ResponseInterface
75
    {
76
        $data_filesystem = Factory::filesystem()->data();
77
78
        $gedcom_files = $this->admin_service->gedcomFiles($data_filesystem);
79
80
        foreach ($gedcom_files as $gedcom_file) {
81
            // Only import files that have changed
82
            $filemtime = (string) $data_filesystem->getTimestamp($gedcom_file);
83
84
            $tree = $this->tree_service->all()->get($gedcom_file) ?? $this->tree_service->create($gedcom_file, $gedcom_file);
85
86
            if ($tree->getPreference('filemtime') !== $filemtime) {
87
                $resource = $data_filesystem->readStream($gedcom_file);
88
                $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

88
                $stream   = app(StreamFactoryInterface::class)->createStreamFromResource(/** @scrutinizer ignore-type */ $resource);
Loading history...
89
                $tree->importGedcomFile($stream, $gedcom_file);
90
                $stream->close();
91
                $tree->setPreference('filemtime', $filemtime);
92
93
                FlashMessages::addMessage(I18N::translate('The GEDCOM file “%s” has been imported.', e($gedcom_file)), 'success');
94
95
                if ($this->timeout_service->isTimeNearlyUp(10.0)) {
96
                    return redirect(route(__CLASS__), StatusCodeInterface::STATUS_TEMPORARY_REDIRECT);
97
                }
98
            }
99
        }
100
101
        foreach ($this->tree_service->all() as $tree) {
102
            if (!$gedcom_files->containsStrict($tree->name())) {
103
                $this->tree_service->delete($tree);
104
                FlashMessages::addMessage(I18N::translate('The family tree “%s” has been deleted.', e($tree->title())), 'success');
105
106
                if ($this->timeout_service->isTimeNearlyUp(10.0)) {
107
                    return redirect(route(__CLASS__), StatusCodeInterface::STATUS_TEMPORARY_REDIRECT);
108
                }
109
            }
110
        }
111
112
        return redirect(route(ManageTrees::class, ['tree' => $this->tree_service->all()->first()->name()]));
113
    }
114
}
115