MapAdapterAddPage   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 30 3
A __construct() 0 10 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\I18N;
18
use Fisharebest\Webtrees\Tree;
19
use Fisharebest\Webtrees\Validator;
20
use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
21
use Fisharebest\Webtrees\Http\ViewResponseTrait;
22
use Fisharebest\Webtrees\Services\ModuleService;
23
use MyArtJaub\Webtrees\Module\GeoDispersion\GeoDispersionModule;
24
use MyArtJaub\Webtrees\Module\GeoDispersion\Services\GeoAnalysisViewDataService;
25
use MyArtJaub\Webtrees\Module\GeoDispersion\Services\MapDefinitionsService;
26
use MyArtJaub\Webtrees\Module\GeoDispersion\Services\PlaceMapperService;
27
use Psr\Http\Message\ResponseInterface;
28
use Psr\Http\Message\ServerRequestInterface;
29
use Psr\Http\Server\RequestHandlerInterface;
30
31
/**
32
 * Request handler for displaying configuration of a new geographical analysis map adapter.
33
 */
34
class MapAdapterAddPage implements RequestHandlerInterface
35
{
36
    use ViewResponseTrait;
37
38
    private ?GeoDispersionModule $module;
39
    private GeoAnalysisViewDataService $geoview_data_service;
40
    private MapDefinitionsService $map_definition_service;
41
    private PlaceMapperService $place_mapper_service;
42
43
    /**
44
     * Constructor for MapAdapterAddPage Request Handler
45
     *
46
     * @param ModuleService $module_service
47
     * @param GeoAnalysisViewDataService $geoview_data_service
48
     * @param MapDefinitionsService $map_definition_service
49
     * @param PlaceMapperService $place_mapper_service
50
     */
51
    public function __construct(
52
        ModuleService $module_service,
53
        GeoAnalysisViewDataService $geoview_data_service,
54
        MapDefinitionsService $map_definition_service,
55
        PlaceMapperService $place_mapper_service
56
    ) {
57
        $this->module = $module_service->findByInterface(GeoDispersionModule::class)->first();
58
        $this->geoview_data_service = $geoview_data_service;
59
        $this->map_definition_service = $map_definition_service;
60
        $this->place_mapper_service = $place_mapper_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
        $tree = Validator::attributes($request)->tree();
76
77
        $view_id = Validator::attributes($request)->integer('view_id', -1);
78
        $view = $this->geoview_data_service->find($tree, $view_id, true);
79
80
        if ($view === null) {
81
            throw new HttpNotFoundException(
82
                I18N::translate('The geographical dispersion analysis view could not be found.')
83
            );
84
        }
85
86
        return $this->viewResponse($this->module->name() . '::admin/map-adapter-edit', [
87
            'module'            =>  $this->module,
88
            'title'             =>  I18N::translate('Add a map configuration'),
89
            'tree'              =>  $tree,
90
            'view_id'           =>  $view_id,
91
            'map_adapter'       =>  null,
92
            'maps_list'         =>  $this->map_definition_service->all(),
93
            'mappers_list'      =>  $this->place_mapper_service->all(),
94
            'route_edit'        =>  route(MapAdapterAddAction::class, [
95
                                        'tree'      => $tree->name(),
96
                                        'view_id'   => $view_id
97
                                    ])
98
        ]);
99
    }
100
}
101