MapAdapterMapperConfig::handle()   B
last analyzed

Complexity

Conditions 10
Paths 11

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 23
nc 11
nop 1
dl 0
loc 38
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

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-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\Tree;
19
use Fisharebest\Webtrees\Validator;
20
use Fisharebest\Webtrees\Http\ViewResponseTrait;
21
use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
22
use Fisharebest\Webtrees\Services\ModuleService;
23
use Illuminate\Contracts\Container\BindingResolutionException;
24
use MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface;
25
use MyArtJaub\Webtrees\Module\GeoDispersion\GeoDispersionModule;
26
use MyArtJaub\Webtrees\Module\GeoDispersion\Services\MapAdapterDataService;
27
use Psr\Http\Message\ResponseInterface;
28
use Psr\Http\Message\ServerRequestInterface;
29
use Psr\Http\Server\RequestHandlerInterface;
30
31
/**
32
 * Request handler for displaying configuration of a place mapper.
33
 */
34
class MapAdapterMapperConfig implements RequestHandlerInterface
35
{
36
    use ViewResponseTrait;
37
38
    private ?GeoDispersionModule $module;
39
    private MapAdapterDataService $mapadapter_data_service;
40
41
    /**
42
     * Constructor for MapAdapterMapperConfig Request Handler
43
     *
44
     * @param ModuleService $module_service
45
     * @param MapAdapterDataService $mapadapter_data_service
46
     */
47
    public function __construct(
48
        ModuleService $module_service,
49
        MapAdapterDataService $mapadapter_data_service
50
    ) {
51
        $this->module = $module_service->findByInterface(GeoDispersionModule::class)->first();
52
        $this->mapadapter_data_service = $mapadapter_data_service;
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
58
     */
59
    public function handle(ServerRequestInterface $request): ResponseInterface
60
    {
61
        $this->layout = 'layouts/ajax';
62
63
        if ($this->module === null) {
64
            throw new HttpNotFoundException(I18N::translate('The attached module could not be found.'));
65
        }
66
        $tree = Validator::attributes($request)->tree();
67
68
        $adapter_id = Validator::attributes($request)->integer('adapter_id', -1);
69
        $map_adapter = $this->mapadapter_data_service->find($adapter_id);
70
71
        $mapper_class = Validator::queryParams($request)->string('mapper', '');
72
        $mapper = null;
73
        if ($mapper_class === '' && $map_adapter !== null) {
74
            $mapper = $map_adapter->placeMapper();
75
        } else {
76
            try {
77
                $mapper = app($mapper_class);
78
            } catch (BindingResolutionException $ex) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
79
            }
80
81
            if (
82
                $mapper !== null && $map_adapter !== null &&
83
                get_class($map_adapter->placeMapper()) === get_class($mapper)
84
            ) {
85
                $mapper = $map_adapter->placeMapper();
86
            }
87
        }
88
89
        if ($mapper === null || !($mapper instanceof PlaceMapperInterface)) {
90
            throw new HttpNotFoundException(
91
                I18N::translate('The configuration for the place mapper could not be found.')
92
            );
93
        }
94
95
        return $this->viewResponse('layouts/ajax', [
96
            'content' => $mapper->config()->configContent($this->module, $tree)
97
        ]);
98
    }
99
}
100