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

MediaPage::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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\Media;
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 assert;
34
use function is_string;
35
use function redirect;
36
37
/**
38
 * Show a media object's page.
39
 */
40
class MediaPage implements RequestHandlerInterface
41
{
42
    use ViewResponseTrait;
43
44
    /** @var ClipboardService */
45
    private $clipboard_service;
46
47
    /**
48
     * MediaPage 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
        $media = Media::getInstance($xref, $tree);
71
        $media = Auth::checkMediaAccess($media);
72
73
        // Redirect to correct xref/slug
74
        if ($media->xref() !== $xref || $request->getAttribute('slug') !== $media->slug()) {
75
            return redirect($media->url());
76
        }
77
78
        return $this->viewResponse('media-page', [
79
            'clipboard_facts' => $this->clipboard_service->pastableFacts($media, new Collection()),
80
            'families'        => $media->linkedFamilies('OBJE'),
81
            'facts'           => $this->facts($media),
82
            'individuals'     => $media->linkedIndividuals('OBJE'),
83
            'media'           => $media,
84
            'meta_robots'     => 'index,follow',
85
            'notes'           => $media->linkedNotes('OBJE'),
86
            'sources'         => $media->linkedSources('OBJE'),
87
            'title'           => $media->fullName(),
88
            'tree'            => $tree,
89
        ]);
90
    }
91
92
    /**
93
     * @param Media $record
94
     *
95
     * @return Collection
96
     */
97
    private function facts(Media $record): Collection
98
    {
99
        return $record->facts()
100
            ->filter(static function (Fact $fact): bool {
101
                return $fact->getTag() !== 'FILE';
102
            });
103
    }
104
}
105