Passed
Branch feature/2.1-geodispersion-dev (1d61a8)
by Jonathan
61:21
created

GeoAnalysisViewsList   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 43
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 19 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-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\Http\ViewResponseTrait;
18
use Fisharebest\Webtrees\Services\ModuleService;
19
use MyArtJaub\Webtrees\Module\GeoDispersion\GeoDispersionModule;
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\ServerRequestInterface;
22
use Psr\Http\Server\RequestHandlerInterface;
23
use MyArtJaub\Webtrees\Module\GeoDispersion\Services\GeoAnalysisViewDataService;
24
use MyArtJaub\Webtrees\Module\GeoDispersion\Views\AbstractGeoAnalysisView;
25
use Fisharebest\Webtrees\Tree;
26
use Fisharebest\Webtrees\I18N;
27
use Fisharebest\Webtrees\Exceptions\HttpNotFoundException;
28
29
/**
30
 * Request handler for listing geographical dispersion analysis views
31
 *
32
 */
33
class GeoAnalysisViewsList implements RequestHandlerInterface
34
{
35
    use ViewResponseTrait;
36
37
    private ?GeoDispersionModule $module;
38
    private GeoAnalysisViewDataService $geoviewdata_service;
39
40
    /**
41
     * Constructor for GeoAnalysisViewsList Request Handler
42
     *
43
     * @param ModuleService $module_service
44
     */
45
    public function __construct(
46
        ModuleService $module_service,
47
        GeoAnalysisViewDataService $geoviewdata_service
48
    ) {
49
        $this->module = $module_service->findByInterface(GeoDispersionModule::class)->first();
50
        $this->geoviewdata_service = $geoviewdata_service;
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
56
     */
57
    public function handle(ServerRequestInterface $request): ResponseInterface
58
    {
59
        if ($this->module === null) {
60
            throw new HttpNotFoundException(I18N::translate('The attached module could not be found.'));
61
        }
62
63
        $tree = $request->getAttribute('tree');
64
        assert($tree instanceof 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