Passed
Push — develop ( 5f211d...930448 )
by Greg
13:36 queued 06:57
created

MapDataList::handle()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 53
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 31
nc 10
nop 1
dl 0
loc 53
rs 8.8017
c 2
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2021 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Fisharebest\Webtrees\Http\ViewResponseTrait;
23
use Fisharebest\Webtrees\I18N;
24
use Fisharebest\Webtrees\Module\PlaceHierarchyListModule;
25
use Fisharebest\Webtrees\PlaceLocation;
26
use Fisharebest\Webtrees\Services\MapDataService;
27
use Fisharebest\Webtrees\Services\ModuleService;
28
use Fisharebest\Webtrees\Services\TreeService;
29
use Psr\Http\Message\ResponseInterface;
30
use Psr\Http\Message\ServerRequestInterface;
31
use Psr\Http\Server\RequestHandlerInterface;
32
33
use function array_reverse;
34
use function e;
35
use function redirect;
36
use function route;
37
38
/**
39
 * Show a list of map data.
40
 */
41
class MapDataList implements RequestHandlerInterface
42
{
43
    use ViewResponseTrait;
44
45
    /** @var MapDataService */
46
    private $map_data_service;
47
48
    /** @var ModuleService */
49
    private $module_service;
50
51
    /** @var TreeService */
52
    private $tree_service;
53
54
    /**
55
     * Dependency injection.
56
     *
57
     * @param MapDataService $map_data_service
58
     * @param ModuleService  $module_service
59
     * @param TreeService    $tree_service
60
     */
61
    public function __construct(
62
        MapDataService $map_data_service,
63
        ModuleService $module_service,
64
        TreeService $tree_service
65
    ) {
66
        $this->map_data_service = $map_data_service;
67
        $this->module_service   = $module_service;
68
        $this->tree_service     = $tree_service;
69
    }
70
71
    /**
72
     * @param ServerRequestInterface $request
73
     *
74
     * @return ResponseInterface
75
     */
76
    public function handle(ServerRequestInterface $request): ResponseInterface
77
    {
78
        $parent_id = $request->getAttribute('parent_id');
79
80
        if ($parent_id === null) {
81
            $parent = new PlaceLocation('');
82
        } else {
83
            $parent_id = (int) $parent_id;
84
            $parent = $this->map_data_service->findById($parent_id);
85
        }
86
87
        // Request for a non-existent location?
88
        if ($parent_id !== null &&  $parent->id() === null) {
89
            return redirect(route(__CLASS__));
90
        }
91
92
        // Automatically import any new/missing places.
93
        $this->map_data_service->importMissingLocations();
94
95
        $breadcrumbs = [];
96
97
        if ($parent->id() !== null) {
98
            $breadcrumbs[] = e($parent->locationName());
99
        }
100
101
        $tmp = $parent->parent();
102
103
        while ($tmp->id() !== null) {
104
            $breadcrumbs[route(__CLASS__, ['parent_id' => $tmp->id()])] = $tmp->locationName();
105
106
            $tmp = $tmp->parent();
107
        }
108
109
        $title = I18N::translate('Geographic data');
110
111
        $breadcrumbs[route(__CLASS__)] = $title;
112
113
        $breadcrumbs[route(ControlPanel::class)] = I18N::translate('Control panel');
114
115
        $list_module = $this->module_service
116
            ->findByInterface(PlaceHierarchyListModule::class)
117
            ->first();
118
119
        $this->layout = 'layouts/administration';
120
121
        return $this->viewResponse('admin/locations', [
122
            'active'       => $this->map_data_service->activePlaces($parent),
123
            'all_trees'    => $this->tree_service->all(),
124
            'breadcrumbs'  => array_reverse($breadcrumbs),
125
            'parent_id'    => $parent_id,
126
            'placelist'    => $this->map_data_service->getPlaceListLocation($parent_id),
127
            'list_module'  => $list_module,
128
            'title'        => $title,
129
        ]);
130
    }
131
}
132