GeoAnalysisViewTabs   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 33 3
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\Registry;
19
use Fisharebest\Webtrees\Validator;
20
use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
21
use Fisharebest\Webtrees\Services\ModuleService;
22
use MyArtJaub\Webtrees\Module\GeoDispersion\GeoDispersionModule;
23
use MyArtJaub\Webtrees\Module\GeoDispersion\Services\GeoAnalysisViewDataService;
24
use Psr\Http\Message\ResponseInterface;
25
use Psr\Http\Message\ServerRequestInterface;
26
use Psr\Http\Server\RequestHandlerInterface;
27
28
/**
29
 * Request handler for tabs displaying a geographical dispersion analysis view.
30
 *
31
 */
32
class GeoAnalysisViewTabs implements RequestHandlerInterface
33
{
34
    private ?GeoDispersionModule $module;
35
    private GeoAnalysisViewDataService $geoviewdata_service;
36
37
    /**
38
     * Constructor for GeoAnalysisMapsList Request Handler
39
     *
40
     * @param ModuleService $module_service
41
     */
42
    public function __construct(
43
        ModuleService $module_service,
44
        GeoAnalysisViewDataService $geoviewdata_service
45
    ) {
46
        $this->module = $module_service->findByInterface(GeoDispersionModule::class)->first();
47
        $this->geoviewdata_service = $geoviewdata_service;
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
53
     */
54
    public function handle(ServerRequestInterface $request): ResponseInterface
55
    {
56
        if ($this->module === null) {
57
            throw new HttpNotFoundException(I18N::translate('The attached module could not be found.'));
58
        }
59
60
        $tree = Validator::attributes($request)->tree();
61
        $view_id = Validator::attributes($request)->integer('view_id', -1);
62
63
        $view = $this->geoviewdata_service->find($tree, $view_id);
64
65
        if ($view === null) {
66
            throw new HttpNotFoundException(I18N::translate('The requested dispersion analysis does not exist.'));
67
        }
68
69
        $results = $view->analysis()->results($tree, $view->placesDepth());
70
71
        $params = [
72
            'module_name'   =>  $this->module->name(),
73
            'tree'          =>  $tree,
74
            'view'          =>  $view,
75
            'items_descr'   =>  $view->analysis()->itemsDescription()
76
        ];
77
        $response = [
78
            'global'    =>  view('layouts/ajax', [
79
                'content' =>    $view->globalTabContent($this->module, $results->global(), $params)
80
            ]),
81
            'detailed'  => view('layouts/ajax', [
82
                'content' =>    $view->detailedTabContent($this->module, $results->sortedDetailed(), $params)
83
            ])
84
        ];
85
86
        return Registry::responseFactory()->response($response);
87
    }
88
}
89