Passed
Push — master ( 63f369...ac5ee7 )
by Greg
05:52
created

HeaderPage::facts()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
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 Fig\Http\Message\StatusCodeInterface;
23
use Fisharebest\Webtrees\Auth;
24
use Fisharebest\Webtrees\Fact;
25
use Fisharebest\Webtrees\Http\ViewResponseTrait;
26
use Fisharebest\Webtrees\Header;
27
use Fisharebest\Webtrees\Tree;
28
use Illuminate\Support\Collection;
29
use Psr\Http\Message\ResponseInterface;
30
use Psr\Http\Message\ServerRequestInterface;
31
use Psr\Http\Server\RequestHandlerInterface;
32
33
use function array_search;
34
use function assert;
35
use function is_string;
36
use function redirect;
37
38
use const PHP_INT_MAX;
39
40
/**
41
 * Show a header's page.
42
 */
43
class HeaderPage implements RequestHandlerInterface
44
{
45
    use ViewResponseTrait;
46
47
    // Show the header's facts in this order:
48
    private const FACT_ORDER = [
49
        1 => 'SOUR',
50
        'DEST',
51
        'DATE',
52
        'SUBM',
53
        'SUBN',
54
        'FILE',
55
        'COPR',
56
        'GEDC',
57
        'CHAR',
58
        'LANG',
59
        'PLAC',
60
        'NOTE',
61
    ];
62
63
    /**
64
     * @param ServerRequestInterface $request
65
     *
66
     * @return ResponseInterface
67
     */
68
    public function handle(ServerRequestInterface $request): ResponseInterface
69
    {
70
        $tree = $request->getAttribute('tree');
71
        assert($tree instanceof Tree);
72
73
        $xref = $request->getAttribute('xref');
74
        assert(is_string($xref));
75
76
        $header = Header::getInstance($xref, $tree);
77
        $header = Auth::checkHeaderAccess($header, false);
78
79
        // Redirect to correct xref/slug
80
        if ($header->xref() !== $xref || $request->getAttribute('slug') !== $header->slug()) {
81
            return redirect($header->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
82
        }
83
84
        return $this->viewResponse('gedcom-record-page', [
85
            'facts'            => $this->facts($header),
86
            'record'           => $header,
87
            'families'         => new Collection(),
88
            'individuals'      => new Collection(),
89
            'media_objects'    => new Collection(),
90
            'meta_description' => '',
91
            'meta_robots'      => 'index,follow',
92
            'notes'            => new Collection(),
93
            'sources'          => new Collection(),
94
            'title'            => $header->fullName(),
95
            'tree'             => $tree,
96
        ]);
97
    }
98
99
    /**
100
     * @param Header $record
101
     *
102
     * @return Collection<Fact>
103
     */
104
    private function facts(Header $record): Collection
105
    {
106
        return $record->facts()
107
            ->sort(static function (Fact $x, Fact $y): int {
108
                $sort_x = array_search($x->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
109
                $sort_y = array_search($y->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
110
111
                return $sort_x <=> $sort_y;
112
            });
113
    }
114
}
115