Passed
Push — master ( e82bf2...541863 )
by Greg
07:06 queued 01:36
created

RepositoryPage::facts()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
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\Fact;
24
use Fisharebest\Webtrees\Http\ViewResponseTrait;
25
use Fisharebest\Webtrees\Repository;
26
use Fisharebest\Webtrees\Services\ClipboardService;
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 repository's page.
42
 */
43
class RepositoryPage implements RequestHandlerInterface
44
{
45
    use ViewResponseTrait;
46
47
    // Show the repository's facts in this order:
48
    private const FACT_ORDER = [
49
        1 => 'NAME',
50
        'ADDR',
51
        'NOTE',
52
        'WWW',
53
        'REFN',
54
        'RIN',
55
        '_UID',
56
        'CHAN',
57
        'RESN',
58
    ];
59
60
    /** @var ClipboardService */
61
    private $clipboard_service;
62
63
    /**
64
     * RepositoryPage constructor.
65
     *
66
     * @param ClipboardService $clipboard_service
67
     */
68
    public function __construct(ClipboardService $clipboard_service)
69
    {
70
        $this->clipboard_service = $clipboard_service;
71
    }
72
73
    /**
74
     * @param ServerRequestInterface $request
75
     *
76
     * @return ResponseInterface
77
     */
78
    public function handle(ServerRequestInterface $request): ResponseInterface
79
    {
80
        $tree = $request->getAttribute('tree');
81
        assert($tree instanceof Tree);
82
83
        $xref = $request->getAttribute('xref');
84
        assert(is_string($xref));
85
86
        $repository = Repository::getInstance($xref, $tree);
87
        $repository = Auth::checkRepositoryAccess($repository, false);
88
89
        // Redirect to correct xref/slug
90
        if ($repository->xref() !== $xref ||  $request->getAttribute('slug') !== $repository->slug()) {
91
            return redirect($repository->url());
92
        }
93
94
        return $this->viewResponse('repository-page', [
95
            'clipboard_facts' => $this->clipboard_service->pastableFacts($repository, new Collection()),
96
            'facts'           => $this->facts($repository),
97
            'meta_robots'     => 'index,follow',
98
            'repository'      => $repository,
99
            'sources'         => $repository->linkedSources('REPO'),
100
            'title'           => $repository->fullName(),
101
            'tree'            => $tree,
102
        ]);
103
    }
104
105
    /**
106
     * @param Repository $record
107
     *
108
     * @return Collection
109
     */
110
    private function facts(Repository $record): Collection
111
    {
112
        return $record->facts()
113
            ->sort(static function (Fact $x, Fact $y): int {
114
                $sort_x = array_search($x->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
115
                $sort_y = array_search($y->getTag(), self::FACT_ORDER, true) ?: PHP_INT_MAX;
116
117
                return $sort_x <=> $sort_y;
118
            });
119
    }
120
}
121