Passed
Branch feature/2.1-geodispersion-dev (38d49e)
by Jonathan
04:17
created

AdminConfigPage::handle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 20
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 31
rs 9.6
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-2021, 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\Contracts\UserInterface;
21
use Fisharebest\Webtrees\Exceptions\HttpAccessDeniedException;
22
use Fisharebest\Webtrees\Exceptions\HttpNotFoundException;
23
use Fisharebest\Webtrees\Http\ViewResponseTrait;
24
use Fisharebest\Webtrees\Services\ModuleService;
25
use Fisharebest\Webtrees\Services\TreeService;
26
use MyArtJaub\Webtrees\Module\GeoDispersion\GeoDispersionModule;
27
use MyArtJaub\Webtrees\Module\GeoDispersion\Services\GeoAnalysisDataService;
28
use Psr\Http\Message\ResponseInterface;
29
use Psr\Http\Message\ServerRequestInterface;
30
use Psr\Http\Server\RequestHandlerInterface;
31
32
/**
33
 * Request handler for displaying configuration of the module.
34
 */
35
class AdminConfigPage implements RequestHandlerInterface
36
{
37
    use ViewResponseTrait;
38
39
    private ?GeoDispersionModule $module;
40
41
    private TreeService $tree_service;
42
43
    private GeoAnalysisDataService $geoanalysis_data_service;
44
45
    /**
46
     * Constructor for the AdminConfigPage Request Handler
47
     *
48
     * @param ModuleService $module_service
49
     * @param TreeService $tree_service
50
     * @param GeoAnalysisDataService $geoanalysis_data_service
51
     */
52
    public function __construct(
53
        ModuleService $module_service,
54
        TreeService $tree_service,
55
        GeoAnalysisDataService $geoanalysis_data_service
56
    ) {
57
        $this->module = $module_service->findByInterface(GeoDispersionModule::class)->first();
58
        $this->tree_service = $tree_service;
59
        $this->geoanalysis_data_service = $geoanalysis_data_service;
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
65
     */
66
    public function handle(ServerRequestInterface $request): ResponseInterface
67
    {
68
        $this->layout = 'layouts/administration';
69
70
        if ($this->module === null) {
71
            throw new HttpNotFoundException(I18N::translate('The attached module could not be found.'));
72
        }
73
74
        $user = $request->getAttribute('user');
75
        assert($user instanceof UserInterface);
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 = $request->getAttribute('tree') ?? $all_trees->first();
83
        assert($tree instanceof Tree);
84
85
        $same_tree = fn(Tree $tree_collection): bool => $tree->id() === $tree_collection->id();
86
        if (!$all_trees->contains($same_tree)) {
87
            throw new HttpAccessDeniedException();
88
        }
89
90
        return $this->viewResponse($this->module->name() . '::admin/config', [
91
            'module_name'       =>  $this->module->name(),
92
            'title'             =>  $this->module->title(),
93
            'tree'              =>  $tree,
94
            'other_trees'       =>  $all_trees->reject($same_tree),
95
            'place_example'     =>  $this->geoanalysis_data_service->placeHierarchyExample($tree),
96
            'js_script_url'     =>  $this->module->assetUrl('js/geodispersion.min.js')
97
        ]);
98
    }
99
}
100