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

GeoAnalysisViewTabs   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 63
rs 10
wmc 5

2 Methods

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