Passed
Push — master ( 203b6c...5818a3 )
by Greg
05:45
created

GedcomRecordPage   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 27 3
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2019 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\Auth;
23
use Fisharebest\Webtrees\Family;
24
use Fisharebest\Webtrees\GedcomRecord;
25
use Fisharebest\Webtrees\Http\ViewResponseTrait;
26
use Fisharebest\Webtrees\Individual;
27
use Fisharebest\Webtrees\Media;
28
use Fisharebest\Webtrees\Note;
29
use Fisharebest\Webtrees\Repository;
30
use Fisharebest\Webtrees\Source;
31
use Fisharebest\Webtrees\Tree;
32
use Psr\Http\Message\ResponseInterface;
33
use Psr\Http\Message\ServerRequestInterface;
34
use Psr\Http\Server\RequestHandlerInterface;
35
36
use function assert;
37
use function is_string;
38
use function redirect;
39
40
/**
41
 * Display non-standard genealogy records.
42
 */
43
class GedcomRecordPage implements RequestHandlerInterface
44
{
45
    use ViewResponseTrait;
46
47
    // These standard genealogy record types have their own pages.
48
    private const STANDARD_RECORDS = [
49
        Family::class,
50
        Individual::class,
51
        Media::class,
52
        Note::class,
53
        Repository::class,
54
        Source::class,
55
    ];
56
57
    /**
58
     * Show a gedcom record's page.
59
     *
60
     * @param ServerRequestInterface $request
61
     *
62
     * @return ResponseInterface
63
     */
64
    public function handle(ServerRequestInterface $request): ResponseInterface
65
    {
66
        $tree = $request->getAttribute('tree');
67
        assert($tree instanceof Tree);
68
69
        $xref = $request->getAttribute('xref');
70
        assert(is_string($xref));
71
72
        $record = GedcomRecord::getInstance($xref, $tree);
73
        $record = Auth::checkRecordAccess($record);
74
75
        // Standard genealogy records have their own pages.
76
        if ($record->xref() !== $xref || in_array(get_class($record), self::STANDARD_RECORDS, true)) {
77
            return redirect($record->url());
78
        }
79
80
        return $this->viewResponse('gedcom-record-page', [
81
            'facts'         => $record->facts(),
82
            'families'      => $record->linkedFamilies($record::RECORD_TYPE),
83
            'individuals'   => $record->linkedIndividuals($record::RECORD_TYPE),
84
            'meta_robots'   => 'index,follow',
85
            'notes'         => $record->linkedNotes($record::RECORD_TYPE),
86
            'media_objects' => $record->linkedMedia($record::RECORD_TYPE),
87
            'record'        => $record,
88
            'sources'       => $record->linkedSources($record::RECORD_TYPE),
89
            'title'         => $record->fullName(),
90
            'tree'          => $tree,
91
        ]);
92
    }
93
}
94