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

MapDataAdd::handle()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 54
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 36
nc 8
nop 1
dl 0
loc 54
rs 9.344
c 1
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\PlaceLocation;
25
use Fisharebest\Webtrees\Services\MapDataService;
26
use Psr\Http\Message\ResponseInterface;
27
use Psr\Http\Message\ServerRequestInterface;
28
use Psr\Http\Server\RequestHandlerInterface;
29
30
use function e;
31
use function route;
32
33
/**
34
 * Edit location data.
35
 */
36
class MapDataAdd implements RequestHandlerInterface
37
{
38
    use ViewResponseTrait;
39
40
    /** @var MapDataService */
41
    private $map_data_service;
42
43
    /**
44
     * Dependency injection.
45
     *
46
     * @param MapDataService $map_data_service
47
     */
48
    public function __construct(MapDataService $map_data_service)
49
    {
50
        $this->map_data_service = $map_data_service;
51
    }
52
53
    /**
54
     * @param ServerRequestInterface $request
55
     *
56
     * @return ResponseInterface
57
     */
58
    public function handle(ServerRequestInterface $request): ResponseInterface
59
    {
60
        $this->layout = 'layouts/administration';
61
62
        $parent_id = $request->getAttribute('parent_id');
63
64
        if ($parent_id === null) {
65
            $parent = new PlaceLocation('');
66
        } else {
67
            $parent = $this->map_data_service->findById((int) $parent_id);
68
        }
69
70
        if ($parent->id() === null) {
71
            $title = I18N::translate('World');
72
        } else {
73
            $title = e($parent->locationName());
74
        }
75
76
        $title .= ' — ' . I18N::translate('Add');
77
78
        // Build the breadcrumbs in reverse order
79
        $breadcrumbs = [I18N::translate('Add')];
80
81
        $tmp = $parent;
82
        while ($tmp->id() !== null) {
83
            $breadcrumbs[route(MapDataList::class, ['parent_id' => $tmp->id()])] = e($tmp->locationName());
84
85
            $tmp = $tmp->parent();
86
        }
87
88
        $breadcrumbs[route(ControlPanel::class)] = I18N::translate('Control panel');
89
        $breadcrumbs[route(MapDataList::class)]  = I18N::translate('Geographic data');
90
91
        $map_bounds = $parent->boundingRectangle();
92
93
        $marker_position = [
94
            ($map_bounds[0][0] + $map_bounds[1][0]) / 2.0,
95
            ($map_bounds[0][1] + $map_bounds[1][1]) / 2.0,
96
        ];
97
98
        return $this->viewResponse('admin/location-edit', [
99
            'breadcrumbs'     => array_reverse($breadcrumbs),
100
            'title'           => $title,
101
            'location'        => new PlaceLocation(''),
102
            'latitude'        => '',
103
            'longitude'       => '',
104
            'map_bounds'      => $map_bounds,
105
            'marker_position' => $marker_position,
106
            'parent'          => $parent,
107
            'provider'        => [
108
                'url'     => 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
109
                'options' => [
110
                    'attribution' => '<a href="https://www.openstreetmap.org/copyright">&copy; OpenStreetMap</a> contributors',
111
                    'max_zoom'    => 19
112
                ]
113
            ],
114
        ]);
115
    }
116
}
117