Passed
Push — master ( 541863...c46acb )
by Greg
06:06
created

FamilyPage   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 26 3
A significant() 0 15 2
A __construct() 0 3 1
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\Family;
24
use Fisharebest\Webtrees\Http\ViewResponseTrait;
25
use Fisharebest\Webtrees\Services\ClipboardService;
26
use Fisharebest\Webtrees\Tree;
27
use Illuminate\Support\Collection;
28
use Psr\Http\Message\ResponseInterface;
29
use Psr\Http\Message\ServerRequestInterface;
30
use Psr\Http\Server\RequestHandlerInterface;
31
use stdClass;
32
33
use function assert;
34
use function is_string;
35
use function redirect;
36
37
/**
38
 * Show a family's page.
39
 */
40
class FamilyPage implements RequestHandlerInterface
41
{
42
    use ViewResponseTrait;
43
44
    /** @var ClipboardService */
45
    private $clipboard_service;
46
47
    /**
48
     * FamilyPage constructor.
49
     *
50
     * @param ClipboardService $clipboard_service
51
     */
52
    public function __construct(ClipboardService $clipboard_service)
53
    {
54
        $this->clipboard_service = $clipboard_service;
55
    }
56
57
    /**
58
     * @param ServerRequestInterface $request
59
     *
60
     * @return ResponseInterface
61
     */
62
    public function handle(ServerRequestInterface $request): ResponseInterface
63
    {
64
        $tree = $request->getAttribute('tree');
65
        assert($tree instanceof Tree);
66
67
        $xref = $request->getAttribute('xref');
68
        assert(is_string($xref));
69
70
        $family = Family::getInstance($xref, $tree);
71
        $family = Auth::checkFamilyAccess($family, false);
72
73
        // Redirect to correct xref/slug
74
        if ($family->xref() !== $xref || $request->getAttribute('slug') !== $family->slug()) {
75
            return redirect($family->url());
76
        }
77
78
        $clipboard_facts = $this->clipboard_service->pastableFacts($family, new Collection());
79
80
        return $this->viewResponse('family-page', [
81
            'facts'           => $family->facts([], true),
82
            'meta_robots'     => 'index,follow',
83
            'clipboard_facts' => $clipboard_facts,
84
            'record'          => $family,
85
            'significant'     => $this->significant($family),
86
            'title'           => $family->fullName(),
87
            'tree'            => $tree,
88
        ]);
89
    }
90
91
    /**
92
     * What are the significant elements of this page?
93
     * The layout will need them to generate URLs for charts and reports.
94
     *
95
     * @param Family $family
96
     *
97
     * @return stdClass
98
     */
99
    private function significant(Family $family): stdClass
100
    {
101
        $significant = (object) [
102
            'family'     => $family,
103
            'individual' => null,
104
            'surname'    => '',
105
        ];
106
107
        foreach ($family->spouses()->merge($family->children()) as $individual) {
108
            $significant->individual = $individual;
109
            [$significant->surname] = explode(',', $individual->sortName());
110
            break;
111
        }
112
113
        return $significant;
114
    }
115
}
116