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

MapAdapterAddPage   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

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