Passed
Push — develop ( b4bdc7...7bf468 )
by Greg
10:24
created

LocationPage::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2021 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 <https://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\Http\ViewResponseTrait;
25
use Fisharebest\Webtrees\Registry;
26
use Fisharebest\Webtrees\Services\ClipboardService;
27
use Fisharebest\Webtrees\Services\LinkedRecordService;
28
use Fisharebest\Webtrees\Validator;
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
/**
35
 * Show a location's page.
36
 */
37
class LocationPage implements RequestHandlerInterface
38
{
39
    use ViewResponseTrait;
40
41
    private ClipboardService $clipboard_service;
42
43
    private LinkedRecordService $linked_record_service;
44
45
    /**
46
     * @param ClipboardService $clipboard_service
47
     * @param LinkedRecordService $linked_record_service
48
     */
49
    public function __construct(ClipboardService $clipboard_service, LinkedRecordService $linked_record_service)
50
    {
51
        $this->clipboard_service     = $clipboard_service;
52
        $this->linked_record_service = $linked_record_service;
53
    }
54
55
    /**
56
     * @param ServerRequestInterface $request
57
     *
58
     * @return ResponseInterface
59
     */
60
    public function handle(ServerRequestInterface $request): ResponseInterface
61
    {
62
        $tree   = Validator::attributes($request)->tree();
63
        $xref   = Validator::attributes($request)->isXref()->string('xref');
64
        $slug   = Validator::attributes($request)->string('slug', '');
65
        $record = Registry::locationFactory()->make($xref, $tree);
66
        $record = Auth::checkLocationAccess($record, false);
67
68
        // Redirect to correct xref/slug
69
        if ($record->xref() !== $xref || Registry::slugFactory()->make($record) !== $slug) {
70
            return Registry::responseFactory()->redirectUrl($record->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
71
        }
72
73
        return $this->viewResponse('record-page', [
74
            'clipboard_facts'      => $this->clipboard_service->pastableFacts($record),
75
            'linked_families'      => $this->linked_record_service->linkedFamilies($record),
76
            'linked_individuals'   => $this->linked_record_service->linkedIndividuals($record),
77
            'linked_locations'     => $this->linked_record_service->linkedLocations($record),
78
            'linked_media_objects' => null,
79
            'linked_notes'         => null,
80
            'linked_repositories'  => null,
81
            'linked_sources'       => $this->linked_record_service->linkedSources($record),
82
            'linked_submitters'    => null,
83
            'record'               => $record,
84
            'title'                => $record->fullName(),
85
            'tree'                 => $tree,
86
        ]);
87
    }
88
}
89