Issues (2560)

app/Module/ModuleAnalyticsTrait.php (2 issues)

1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 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\Http\RequestHandlers\ModulesAnalyticsPage;
23
use Fisharebest\Webtrees\Http\ViewResponseTrait;
24
use Fisharebest\Webtrees\I18N;
25
use Fisharebest\Webtrees\Registry;
26
use Fisharebest\Webtrees\Validator;
27
use Psr\Http\Message\ResponseInterface;
28
use Psr\Http\Message\ServerRequestInterface;
29
30
/**
31
 * Trait ModuleAnalyticsTrait - default implementation of ModuleAnalyticsInterface
32
 */
33
trait ModuleAnalyticsTrait
34
{
35
    use ViewResponseTrait;
36
37
    /**
38
     * A unique internal name for this module (based on the installation folder).
39
     *
40
     * @return string
41
     */
42
    abstract public function name(): string;
43
44
    abstract public function title(): string;
45
46
    /**
47
     * Set a module setting.
48
     *
49
     * Since module settings are NOT NULL, setting a value to NULL will cause
50
     * it to be deleted.
51
     *
52
     * @param string $setting_name
53
     * @param string $setting_value
54
     *
55
     * @return void
56
     */
57
    abstract public function setPreference(string $setting_name, string $setting_value): void;
58
59
    /**
60
     * Should we add this tracker?
61
     *
62
     * @return bool
63
     */
64
    public function analyticsCanShow(): bool
65
    {
66
        $request = Registry::container()->get(ServerRequestInterface::class);
67
68
        // If the browser sets the DNT header, then we won't use analytics.
69
        if (Validator::serverParams($request)->boolean('HTTP_DNT', false)) {
70
            return false;
71
        }
72
73
        foreach ($this->analyticsParameters() as $parameter) {
74
            if ($parameter === '') {
75
                return false;
76
            }
77
        }
78
79
        return true;
80
    }
81
82
    /**
83
     * The parameters that need to be embedded in the snippet.
84
     *
85
     * @return array<string>
86
     */
87
    public function analyticsParameters(): array
88
    {
89
        return [];
90
    }
91
92
    public function description(): string
93
    {
94
        return I18N::translate('Tracking and analytics');
95
    }
96
97
    /**
98
     * @param ServerRequestInterface $request
99
     *
100
     * @return ResponseInterface
101
     */
102
    public function getAdminAction(ServerRequestInterface $request): ResponseInterface
0 ignored issues
show
The parameter $request 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

102
    public function getAdminAction(/** @scrutinizer ignore-unused */ ServerRequestInterface $request): ResponseInterface

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...
103
    {
104
        $this->layout = 'layouts/administration';
105
106
        return $this->viewResponse('admin/analytics-edit', [
107
            'action'      => route('module', ['module' => $this->name(), 'action' => 'Admin']),
108
            'form_fields' => $this->analyticsFormFields(),
109
            'preview'     => $this->analyticsSnippet($this->analyticsParameters()),
110
            'title'       => $this->title(),
111
        ]);
112
    }
113
114
    /**
115
     * Form fields to edit the parameters.
116
     *
117
     * @return string
118
     */
119
    public function analyticsFormFields(): string
120
    {
121
        return '';
122
    }
123
124
    /**
125
     * Embed placeholders in the snippet.
126
     *
127
     * @param array<string> $parameters
128
     *
129
     * @return string
130
     */
131
    public function analyticsSnippet(array $parameters): string
0 ignored issues
show
The parameter $parameters 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

131
    public function analyticsSnippet(/** @scrutinizer ignore-unused */ array $parameters): 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...
132
    {
133
        return '';
134
    }
135
136
    /**
137
     * Is this a tracker, as opposed to just a site-verification.
138
     *
139
     * @return bool
140
     */
141
    public function isTracker(): bool
142
    {
143
        return true;
144
    }
145
146
    /**
147
     * @param ServerRequestInterface $request
148
     *
149
     * @return ResponseInterface
150
     */
151
    public function postAdminAction(ServerRequestInterface $request): ResponseInterface
152
    {
153
        foreach (array_keys($this->analyticsParameters()) as $parameter) {
154
            $new_value = Validator::parsedBody($request)->string($parameter);
155
156
            $this->setPreference($parameter, $new_value);
157
        }
158
159
        return redirect(route(ModulesAnalyticsPage::class));
160
    }
161
}
162