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

SubmissionPage   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 67
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A facts() 0 8 3
A handle() 0 28 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 Fig\Http\Message\StatusCodeInterface;
23
use Fisharebest\Webtrees\Auth;
24
use Fisharebest\Webtrees\Fact;
25
use Fisharebest\Webtrees\Http\ViewResponseTrait;
26
use Fisharebest\Webtrees\Submission;
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 submission's page.
42
 */
43
class SubmissionPage implements RequestHandlerInterface
44
{
45
    use ViewResponseTrait;
46
47
    // Show the submission's facts in this order:
48
    private const FACT_ORDER = [
49
        1 => 'SUBM',
50
        'FAMF',
51
        'TEMP',
52
        'ANCE',
53
        'DESC',
54
        'ORDI',
55
        'OBJE',
56
        'RIN',
57
        'NOTE',
58
        'CHAN',
59
    ];
60
61
    /**
62
     * @param ServerRequestInterface $request
63
     *
64
     * @return ResponseInterface
65
     */
66
    public function handle(ServerRequestInterface $request): ResponseInterface
67
    {
68
        $tree = $request->getAttribute('tree');
69
        assert($tree instanceof Tree);
70
71
        $xref = $request->getAttribute('xref');
72
        assert(is_string($xref));
73
74
        $submission = Submission::getInstance($xref, $tree);
75
        $submission = Auth::checkSubmissionAccess($submission, false);
76
77
        // Redirect to correct xref/slug
78
        if ($submission->xref() !== $xref || $request->getAttribute('slug') !== $submission->slug()) {
79
            return redirect($submission->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
80
        }
81
82
        return $this->viewResponse('gedcom-record-page', [
83
            'facts'            => $this->facts($submission),
84
            'record'           => $submission,
85
            'families'         => new Collection(),
86
            'individuals'      => new Collection(),
87
            'media_objects'    => new Collection(),
88
            'meta_description' => '',
89
            'meta_robots'      => 'index,follow',
90
            'notes'            => new Collection(),
91
            'sources'          => new Collection(),
92
            'title'            => $submission->fullName(),
93
            'tree'             => $tree,
94
        ]);
95
    }
96
97
    /**
98
     * @param Submission $record
99
     *
100
     * @return Collection<Fact>
101
     */
102
    private function facts(Submission $record): Collection
103
    {
104
        return $record->facts()
105
            ->sort(static function (Fact $x, Fact $y): int {
106
                $sort_x = array_search($x->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
107
                $sort_y = array_search($y->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
108
109
                return $sort_x <=> $sort_y;
110
            });
111
    }
112
}
113