AdminConfigPage   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 60
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 29 4
A __construct() 0 8 1
1
<?php
2
3
/**
4
 * webtrees-lib: MyArtJaub library for webtrees
5
 *
6
 * @package MyArtJaub\Webtrees
7
 * @subpackage GeoDispersion
8
 * @author Jonathan Jaubart <[email protected]>
9
 * @copyright Copyright (c) 2009-2022, Jonathan Jaubart
10
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
11
 */
12
13
declare(strict_types=1);
14
15
namespace MyArtJaub\Webtrees\Module\GeoDispersion\Http\RequestHandlers;
16
17
use Fisharebest\Webtrees\Auth;
18
use Fisharebest\Webtrees\I18N;
19
use Fisharebest\Webtrees\Tree;
20
use Fisharebest\Webtrees\Validator;
21
use Fisharebest\Webtrees\Contracts\UserInterface;
22
use Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException;
23
use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
24
use Fisharebest\Webtrees\Http\ViewResponseTrait;
25
use Fisharebest\Webtrees\Services\ModuleService;
26
use Fisharebest\Webtrees\Services\TreeService;
27
use MyArtJaub\Webtrees\Module\GeoDispersion\GeoDispersionModule;
28
use MyArtJaub\Webtrees\Module\GeoDispersion\Services\GeoAnalysisDataService;
29
use Psr\Http\Message\ResponseInterface;
30
use Psr\Http\Message\ServerRequestInterface;
31
use Psr\Http\Server\RequestHandlerInterface;
32
33
/**
34
 * Request handler for displaying configuration of the module.
35
 */
36
class AdminConfigPage implements RequestHandlerInterface
37
{
38
    use ViewResponseTrait;
39
40
    private ?GeoDispersionModule $module;
41
42
    private TreeService $tree_service;
43
44
    private GeoAnalysisDataService $geoanalysis_data_service;
45
46
    /**
47
     * Constructor for the AdminConfigPage Request Handler
48
     *
49
     * @param ModuleService $module_service
50
     * @param TreeService $tree_service
51
     * @param GeoAnalysisDataService $geoanalysis_data_service
52
     */
53
    public function __construct(
54
        ModuleService $module_service,
55
        TreeService $tree_service,
56
        GeoAnalysisDataService $geoanalysis_data_service
57
    ) {
58
        $this->module = $module_service->findByInterface(GeoDispersionModule::class)->first();
59
        $this->tree_service = $tree_service;
60
        $this->geoanalysis_data_service = $geoanalysis_data_service;
61
    }
62
63
    /**
64
     * {@inheritDoc}
65
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
66
     */
67
    public function handle(ServerRequestInterface $request): ResponseInterface
68
    {
69
        $this->layout = 'layouts/administration';
70
71
        if ($this->module === null) {
72
            throw new HttpNotFoundException(I18N::translate('The attached module could not be found.'));
73
        }
74
75
        $user = Validator::attributes($request)->user();
76
77
        $all_trees = $this->tree_service->all()->filter(fn(Tree $tree) => Auth::isManager($tree, $user));
78
        if ($all_trees->count() === 0) {
79
            throw new HttpAccessDeniedException();
80
        }
81
82
        $tree = Validator::attributes($request)->treeOptional() ?? $all_trees->first();
83
84
        $same_tree = fn(Tree $tree_collection): bool => $tree->id() === $tree_collection->id();
85
        if (!$all_trees->contains($same_tree)) {
86
            throw new HttpAccessDeniedException();
87
        }
88
89
        return $this->viewResponse($this->module->name() . '::admin/config', [
90
            'module_name'       =>  $this->module->name(),
91
            'title'             =>  $this->module->title(),
92
            'tree'              =>  $tree,
93
            'other_trees'       =>  $all_trees->reject($same_tree),
94
            'place_example'     =>  $this->geoanalysis_data_service->placeHierarchyExample($tree),
95
            'js_script_url'     =>  $this->module->assetUrl('js/geodispersion.min.js')
96
        ]);
97
    }
98
}
99