Passed
Push — master ( 466fee...8256f4 )
by Greg
06:43 queued 01:19
created

SubmitterPage   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A facts() 0 8 3
A handle() 0 23 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\Services\ClipboardService;
27
use Fisharebest\Webtrees\Submitter;
28
use Fisharebest\Webtrees\Tree;
29
use Illuminate\Support\Collection;
30
use Psr\Http\Message\ResponseInterface;
31
use Psr\Http\Message\ServerRequestInterface;
32
use Psr\Http\Server\RequestHandlerInterface;
33
34
use function array_search;
35
use function assert;
36
use function is_string;
37
use function redirect;
38
39
use const PHP_INT_MAX;
40
41
/**
42
 * Show a submitter's page.
43
 */
44
class SubmitterPage implements RequestHandlerInterface
45
{
46
    use ViewResponseTrait;
47
48
    // Show the submitter's facts in this order:
49
    private const FACT_ORDER = [
50
        1 => 'NAME',
51
        'ADDR',
52
        'PHON',
53
        'EMAIL',
54
        'WWW',
55
        'LANG',
56
        'OBJE',
57
        'RFN',
58
        'RIN',
59
        'NOTE',
60
        'CHAN',
61
    ];
62
63
    /** @var ClipboardService */
64
    private $clipboard_service;
0 ignored issues
show
introduced by
The private property $clipboard_service is not used, and could be removed.
Loading history...
65
66
    /**
67
     * @param ServerRequestInterface $request
68
     *
69
     * @return ResponseInterface
70
     */
71
    public function handle(ServerRequestInterface $request): ResponseInterface
72
    {
73
        $tree = $request->getAttribute('tree');
74
        assert($tree instanceof Tree);
75
76
        $xref = $request->getAttribute('xref');
77
        assert(is_string($xref));
78
79
        $submitter = Submitter::getInstance($xref, $tree);
80
        $submitter = Auth::checkSubmitterAccess($submitter, false);
81
82
        // Redirect to correct xref/slug
83
        if ($submitter->xref() !== $xref || $request->getAttribute('slug') !== $submitter->slug()) {
84
            return redirect($submitter->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
85
        }
86
87
        return $this->viewResponse('submitter-page', [
88
            'facts'       => $this->facts($submitter),
89
            'submitter'   => $submitter,
90
            'families'    => $submitter->linkedFamilies('SUBM'),
91
            'individuals' => $submitter->linkedIndividuals('SUBM'),
92
            'title'       => $submitter->fullName(),
93
            'tree'        => $tree,
94
        ]);
95
    }
96
97
    /**
98
     * @param Submitter $record
99
     *
100
     * @return Collection<Fact>
101
     */
102
    private function facts(Submitter $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