Passed
Branch feature/2.1-geodispersion-dev (38d49e)
by Jonathan
04:17
created

MapAdapterEditAction::handle()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 56
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 36
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 56
rs 8.0995

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-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\FlashMessages;
18
use Fisharebest\Webtrees\I18N;
19
use Fisharebest\Webtrees\Log;
20
use Fisharebest\Webtrees\Tree;
21
use Fisharebest\Webtrees\Services\ModuleService;
22
use Illuminate\Contracts\Container\BindingResolutionException;
23
use MyArtJaub\Webtrees\Contracts\GeoDispersion\MapDefinitionInterface;
24
use MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface;
25
use MyArtJaub\Webtrees\Module\GeoDispersion\GeoDispersionModule;
26
use MyArtJaub\Webtrees\Module\GeoDispersion\Model\GeoAnalysisMapAdapter;
27
use MyArtJaub\Webtrees\Module\GeoDispersion\Services\MapAdapterDataService;
28
use MyArtJaub\Webtrees\Module\GeoDispersion\Services\MapDefinitionsService;
29
use Psr\Http\Message\ResponseInterface;
30
use Psr\Http\Message\ServerRequestInterface;
31
use Psr\Http\Server\RequestHandlerInterface;
32
33
/**
34
 * Request handler for editing a a geographical analysis map adapter.
35
 */
36
class MapAdapterEditAction implements RequestHandlerInterface
37
{
38
    private ?GeoDispersionModule $module;
39
    private MapAdapterDataService $mapadapter_data_service;
40
    private MapDefinitionsService $map_definition_service;
41
42
    /**
43
     * Constructor for MapAdapterEditAction Request Handler
44
     *
45
     * @param ModuleService $module_service
46
     * @param MapAdapterDataService $mapadapter_data_service
47
     * @param MapDefinitionsService $map_definition_service
48
     */
49
    public function __construct(
50
        ModuleService $module_service,
51
        MapAdapterDataService $mapadapter_data_service,
52
        MapDefinitionsService $map_definition_service
53
    ) {
54
        $this->module = $module_service->findByInterface(GeoDispersionModule::class)->first();
55
        $this->mapadapter_data_service = $mapadapter_data_service;
56
        $this->map_definition_service = $map_definition_service;
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
62
     */
63
    public function handle(ServerRequestInterface $request): ResponseInterface
64
    {
65
        $tree = $request->getAttribute('tree');
66
        assert($tree instanceof Tree);
67
68
        if ($this->module === null) {
69
            FlashMessages::addMessage(
70
                I18N::translate('The attached module could not be found.'),
71
                'danger'
72
            );
73
            return redirect(route(AdminConfigPage::class, ['tree' => $tree]));
74
        }
75
76
        $adapter_id = (int) $request->getAttribute('adapter_id');
77
        $map_adapter = $this->mapadapter_data_service->find($adapter_id);
78
79
        $params = (array) $request->getParsedBody();
80
81
        $map = $this->map_definition_service->find($params['map_adapter_map'] ?? '');
82
        $mapping_property   = $params['map_adapter_property_selected'] ?? '';
83
84
        $mapper = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $mapper is dead and can be removed.
Loading history...
85
        try {
86
            $mapper = app($params['map_adapter_mapper'] ?? '');
87
        } catch (BindingResolutionException $ex) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
88
        }
89
90
        if ($map_adapter === null || $map === null || $mapper === null || !($mapper instanceof PlaceMapperInterface)) {
91
            FlashMessages::addMessage(
92
                I18N::translate('The parameters for the map configuration are not valid.'),
93
                'danger'
94
            );
95
            return redirect(route(AdminConfigPage::class, ['tree' => $tree]));
96
        }
97
98
        $mapper->setConfig($mapper->config()->withConfigUpdate($request));
99
        $new_map_adapter = $map_adapter->with($map, $mapper, $mapping_property);
100
        if ($this->mapadapter_data_service->update($new_map_adapter) > 0) {
101
            FlashMessages::addMessage(
102
                I18N::translate('The map configuration has been successfully updated'),
103
                'success'
104
            );
105
            //phpcs:ignore Generic.Files.LineLength.TooLong
106
            Log::addConfigurationLog('Module ' . $this->module->title() . ' : Map Adapter “' . $map_adapter->id() . '” has been updated.');
107
        } else {
108
            FlashMessages::addMessage(
109
                I18N::translate('An error occured while updating the map configuration'),
110
                'danger'
111
            );
112
            //phpcs:ignore Generic.Files.LineLength.TooLong
113
            Log::addConfigurationLog('Module ' . $this->module->title() . ' : Map Adapter “' . $map_adapter->id() . '” could not be updated. See error log.');
114
        }
115
116
        return redirect(route(GeoAnalysisViewEditPage::class, [
117
            'tree' => $tree->name(),
118
            'view_id' => $map_adapter->geoAnalysisViewId()
119
        ]));
120
    }
121
}
122