GeoAnalysisViewListData   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 45 4
A __construct() 0 6 1
1
<?php
2
3
/**
4
 * webtrees-lib: MyArtJaub library for webtrees
5
 *
6
 * @package MyArtJaub\Webtrees
7
 * @subpackage AdminTasks
8
 * @author Jonathan Jaubart <[email protected]>
9
 * @copyright Copyright (c) 2012-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 MyArtJaub\Webtrees\Module\GeoDispersion\Views\AbstractGeoAnalysisView;
25
use Psr\Http\Message\ResponseInterface;
26
use Psr\Http\Message\ServerRequestInterface;
27
use Psr\Http\Server\RequestHandlerInterface;
28
29
/**
30
 * Request handler for listing geographical dispersion analysis views in JSON format.
31
 *
32
 */
33
class GeoAnalysisViewListData implements RequestHandlerInterface
34
{
35
    private ?GeoDispersionModule $module;
36
    private GeoAnalysisViewDataService $geoview_data_service;
37
38
    /**
39
     * Constructor for GeoAnalysisViewListData Request Handler
40
     *
41
     * @param ModuleService $module_service
42
     * @param GeoAnalysisViewDataService $geoview_data_service
43
     */
44
    public function __construct(
45
        ModuleService $module_service,
46
        GeoAnalysisViewDataService $geoview_data_service
47
    ) {
48
        $this->module = $module_service->findByInterface(GeoDispersionModule::class)->first();
49
        $this->geoview_data_service = $geoview_data_service;
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
55
     */
56
    public function handle(ServerRequestInterface $request): ResponseInterface
57
    {
58
        if ($this->module === null) {
59
            throw new HttpNotFoundException(I18N::translate('The attached module could not be found.'));
60
        }
61
62
        $tree = Validator::attributes($request)->tree();
63
64
        $module = $this->module;
65
        $module_name = $this->module->name();
66
        return Registry::responseFactory()->response(['data' => $this->geoview_data_service->all($tree, true)
67
            ->map(fn(AbstractGeoAnalysisView $view) => [
68
                'edit' => view($module_name . '::admin/view-table-options', [
69
                    'view_id' => $view->id(),
70
                    'view_enabled' => $view->isEnabled(),
71
                    'view_edit_route' => route(GeoAnalysisViewEditPage::class, [
72
                        'tree' => $tree->name(),
73
                        'view_id' => $view->id()
74
                    ]),
75
                    'view_delete_route' => route(GeoAnalysisViewDeleteAction::class, [
76
                        'tree' => $tree->name(),
77
                        'view_id' => $view->id()
78
                    ]),
79
                    'view_status_route' => route(GeoAnalysisViewStatusAction::class, [
80
                        'tree' => $tree->name(),
81
                        'view_id' => $view->id(),
82
                        'enable' => $view->isEnabled() ? 0 : 1
83
                    ]),
84
                ]),
85
                'enabled' =>  [
86
                    'display' => view($module_name . '::components/yes-no-icons', ['yes' => $view->isEnabled()]),
87
                    'raw' => $view->isEnabled() ? 0 : 1
88
                ],
89
                'type' =>  $view->icon($module),
90
                'description' => [
91
                    'display' => '<bdi>' . e($view->description()) . '</bdi>',
92
                    'raw' => e($view->description())
93
                ],
94
                'analysis' => [
95
                    'display' => '<bdi>' . e($view->analysis()->title()) . '</bdi>',
96
                    'raw' => e($view->analysis()->title())
97
                ],
98
                'place_depth' => [
99
                    'display' => I18N::number($view->placesDepth()),
100
                    'raw' => $view->placesDepth()
101
                ]
102
            ])
103
        ]);
104
    }
105
}
106