GeoAnalysisViewsList   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 18 2
A __construct() 0 6 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\Views\AbstractGeoAnalysisView;
26
use Psr\Http\Message\ResponseInterface;
27
use Psr\Http\Message\ServerRequestInterface;
28
use Psr\Http\Server\RequestHandlerInterface;
29
30
/**
31
 * Request handler for listing geographical dispersion analysis views
32
 *
33
 */
34
class GeoAnalysisViewsList implements RequestHandlerInterface
35
{
36
    use ViewResponseTrait;
37
38
    private ?GeoDispersionModule $module;
39
    private GeoAnalysisViewDataService $geoviewdata_service;
40
41
    /**
42
     * Constructor for GeoAnalysisViewsList Request Handler
43
     *
44
     * @param ModuleService $module_service
45
     */
46
    public function __construct(
47
        ModuleService $module_service,
48
        GeoAnalysisViewDataService $geoviewdata_service
49
    ) {
50
        $this->module = $module_service->findByInterface(GeoDispersionModule::class)->first();
51
        $this->geoviewdata_service = $geoviewdata_service;
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
57
     */
58
    public function handle(ServerRequestInterface $request): ResponseInterface
59
    {
60
        if ($this->module === null) {
61
            throw new HttpNotFoundException(I18N::translate('The attached module could not be found.'));
62
        }
63
64
        $tree = Validator::attributes($request)->tree();
65
66
        $views_list = $this->geoviewdata_service
67
            ->all($tree)
68
            ->sortBy(fn(AbstractGeoAnalysisView $view) => $view->description());
69
70
        return $this->viewResponse($this->module->name() . '::geoanalysisviews-list', [
71
            'module'        =>  $this->module,
72
            'title'         =>  I18N::translate('Geographical dispersion'),
73
            'tree'          =>  $tree,
74
            'views_list'    =>  $views_list,
75
            'js_script_url' =>  $this->module->assetUrl('js/geodispersion.min.js')
76
        ]);
77
    }
78
}
79