Passed
Branch main (f9aaf7)
by Jonathan
14:43
created

MapAdapterMapperConfig::handle()   B

Complexity

Conditions 10
Paths 11

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 24
nc 11
nop 1
dl 0
loc 39
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-2021, 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\Http\ViewResponseTrait;
21
use Fisharebest\Webtrees\Services\ModuleService;
22
use Illuminate\Contracts\Container\BindingResolutionException;
23
use MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface;
24
use MyArtJaub\Webtrees\Module\GeoDispersion\GeoDispersionModule;
25
use MyArtJaub\Webtrees\Module\GeoDispersion\Services\MapAdapterDataService;
26
use Psr\Http\Message\ResponseInterface;
27
use Psr\Http\Message\ServerRequestInterface;
28
use Psr\Http\Server\RequestHandlerInterface;
29
30
/**
31
 * Request handler for displaying configuration of a place mapper.
32
 */
33
class MapAdapterMapperConfig implements RequestHandlerInterface
34
{
35
    use ViewResponseTrait;
36
37
    private ?GeoDispersionModule $module;
38
    private MapAdapterDataService $mapadapter_data_service;
39
40
    /**
41
     * Constructor for MapAdapterMapperConfig Request Handler
42
     *
43
     * @param ModuleService $module_service
44
     * @param MapAdapterDataService $mapadapter_data_service
45
     */
46
    public function __construct(
47
        ModuleService $module_service,
48
        MapAdapterDataService $mapadapter_data_service
49
    ) {
50
        $this->module = $module_service->findByInterface(GeoDispersionModule::class)->first();
51
        $this->mapadapter_data_service = $mapadapter_data_service;
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
57
     */
58
    public function handle(ServerRequestInterface $request): ResponseInterface
59
    {
60
        $this->layout = 'layouts/ajax';
61
62
        if ($this->module === null) {
63
            throw new HttpNotFoundException(I18N::translate('The attached module could not be found.'));
64
        }
65
        $tree = $request->getAttribute('tree');
66
        assert($tree instanceof Tree);
67
68
        $adapter_id = (int) $request->getAttribute('adapter_id');
69
        $map_adapter = $this->mapadapter_data_service->find($adapter_id);
70
71
        $mapper_class = $request->getQueryParams()['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