Passed
Push — develop ( f3d484...e21e10 )
by Greg
06:29
created

ModuleTabTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 110
rs 10
c 0
b 0
f 0
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getTabContent() 0 3 1
A getTabAction() 0 19 2
A setTabOrder() 0 3 1
A supportedFacts() 0 3 1
A getTabOrder() 0 3 1
A defaultTabOrder() 0 3 1
A tabTitle() 0 3 1
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\Module;
21
22
use Fisharebest\Webtrees\Auth;
23
use Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException;
24
use Fisharebest\Webtrees\Individual;
25
use Fisharebest\Webtrees\Registry;
26
use Fisharebest\Webtrees\Tree;
27
use Fisharebest\Webtrees\Validator;
28
use Illuminate\Support\Collection;
29
use Psr\Http\Message\ResponseInterface;
30
use Psr\Http\Message\ServerRequestInterface;
31
32
use function view;
33
34
/**
35
 * Trait ModuleTabTrait - default implementation of ModuleTabInterface
36
 */
37
trait ModuleTabTrait
38
{
39
    // The default position for this tab.  It can be changed in the control panel.
40
    protected int $tab_order;
41
42
    /**
43
     * How should this module be identified in the control panel, etc.?
44
     *
45
     * @return string
46
     */
47
    abstract public function title(): string;
48
49
    /**
50
     * The text that appears on the tab.
51
     *
52
     * @return string
53
     */
54
    public function tabTitle(): string
55
    {
56
        return $this->title();
57
    }
58
59
    /**
60
     * Get a the current access level for a module
61
     *
62
     * @param Tree   $tree
63
     * @param string $interface
64
     *
65
     * @return int
66
     */
67
    abstract public function accessLevel(Tree $tree, string $interface): int;
68
69
    /**
70
     * Users change change the order of tabs using the control panel.
71
     *
72
     * @param int $tab_order
73
     *
74
     * @return void
75
     */
76
    public function setTabOrder(int $tab_order): void
77
    {
78
        $this->tab_order = $tab_order;
79
    }
80
81
    /**
82
     * Users change change the order of tabs using the control panel.
83
     *
84
     * @return int
85
     */
86
    public function getTabOrder(): int
87
    {
88
        return $this->tab_order ?? $this->defaultTabOrder();
89
    }
90
91
    /**
92
     * The default position for this tab.  It can be changed in the control panel.
93
     *
94
     * @return int
95
     */
96
    public function defaultTabOrder(): int
97
    {
98
        return 9999;
99
    }
100
101
    /**
102
     * This module handles the following facts - so don't show them on the "Facts and events" tab.
103
     *
104
     * @return Collection<int,string>
105
     */
106
    public function supportedFacts(): Collection
107
    {
108
        return new Collection();
109
    }
110
111
    /**
112
     * Generate the HTML content of this tab.
113
     *
114
     * @param Individual $individual
115
     *
116
     * @return string
117
     */
118
    public function getTabContent(Individual $individual): string
0 ignored issues
show
Unused Code introduced by
The parameter $individual is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

118
    public function getTabContent(/** @scrutinizer ignore-unused */ Individual $individual): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
119
    {
120
        return '';
121
    }
122
123
    /**
124
     * @param ServerRequestInterface $request
125
     *
126
     * @return ResponseInterface
127
     */
128
    public function getTabAction(ServerRequestInterface $request): ResponseInterface
129
    {
130
        $tree = Validator::attributes($request)->tree();
131
        $user = Validator::attributes($request)->user();
132
133
        $xref = $request->getQueryParams()['xref'];
134
135
        $record = Registry::individualFactory()->make($xref, $tree);
136
        $record = Auth::checkIndividualAccess($record);
137
138
        if ($this->accessLevel($tree, ModuleTabInterface::class) < Auth::accessLevel($tree, $user)) {
139
            throw new HttpAccessDeniedException();
140
        }
141
142
        $layout = view('layouts/ajax', [
143
            'content' => $this->getTabContent($record),
144
        ]);
145
146
        return Registry::responseFactory()->response($layout);
147
    }
148
}
149