Passed
Pull Request — main (#4760)
by David
06:12
created

ModuleMapProviderTrait::hasApiKey()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 15
rs 10
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2023 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\HttpServerErrorException;
24
use Fisharebest\Webtrees\I18N;
25
26
use function e;
27
28
/**
29
 * Trait ModuleMapProviderTrait - default implementation of ModuleMapProviderInterface
30
 */
31
trait ModuleMapProviderTrait
32
{
33
    use ModuleConfigTrait;
34
35
    /**
36
     * Parameters to create a TileLayer in LeafletJs.
37
     *
38
     * @return array<object>
39
     */
40
    public function leafletJsTileLayers(): array
41
    {
42
        return [];
43
    }
44
45
    /**
46
     *  If module requires an api key then return false if not valid
47
     *
48
     * @return bool
49
     * @throws HttpServerErrorException
50
     */
51
    public function hasApiKey(): bool
52
    {
53
        $api_key = $this->getPreference('api_key');
0 ignored issues
show
Bug introduced by
It seems like getPreference() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

53
        /** @scrutinizer ignore-call */ 
54
        $api_key = $this->getPreference('api_key');
Loading history...
54
55
        // Do the functions to manage the config page exist in the provider module?
56
        $function_diff = array_diff(get_class_methods(get_class($this)), get_class_methods((string) get_parent_class($this)));
57
58
        $error = in_array("getAdminAction", $function_diff) && $api_key === '';
59
        if ($error && Auth::isAdmin()) {
60
            $message = I18N::translate('<a href="%s">The %s service requires an API key.', e($this->getConfigLink()), $this->title());
0 ignored issues
show
Bug introduced by
It seems like title() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

60
            $message = I18N::translate('<a href="%s">The %s service requires an API key.', e($this->getConfigLink()), $this->/** @scrutinizer ignore-call */ title());
Loading history...
61
62
            throw new HttpServerErrorException($message);
63
        }
64
65
        return !$error;
66
    }
67
}
68