Passed
Push — feature/code-analysis ( e964aa...4fe35d )
by Jonathan
14:33
created

GeoAnalysisViewAddAction::handle()   B

Complexity

Conditions 9
Paths 11

Size

Total Lines 57
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 37
nc 11
nop 1
dl 0
loc 57
rs 7.7724
c 0
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-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\FlashMessages;
18
use Fisharebest\Webtrees\I18N;
19
use Fisharebest\Webtrees\Log;
20
use Fisharebest\Webtrees\Tree;
21
use Fisharebest\Webtrees\Validator;
22
use Fisharebest\Webtrees\Services\ModuleService;
23
use Illuminate\Contracts\Container\BindingResolutionException;
24
use MyArtJaub\Webtrees\Contracts\GeoDispersion\GeoAnalysisInterface;
25
use MyArtJaub\Webtrees\Module\GeoDispersion\GeoDispersionModule;
26
use MyArtJaub\Webtrees\Module\GeoDispersion\Services\GeoAnalysisViewDataService;
27
use MyArtJaub\Webtrees\Module\GeoDispersion\Views\GeoAnalysisMap;
28
use MyArtJaub\Webtrees\Module\GeoDispersion\Views\GeoAnalysisTable;
29
use Psr\Http\Message\ResponseInterface;
30
use Psr\Http\Message\ServerRequestInterface;
31
use Psr\Http\Server\RequestHandlerInterface;
32
33
/**
34
 * Request handler for adding a geographical analysis view.
35
 */
36
class GeoAnalysisViewAddAction implements RequestHandlerInterface
37
{
38
    private ?GeoDispersionModule $module;
39
    private GeoAnalysisViewDataService $geoview_data_service;
40
41
    /**
42
     * Constructor for GeoAnalysisViewAddAction Request Handler
43
     *
44
     * @param ModuleService $module_service
45
     * @param GeoAnalysisViewDataService $geoview_data_service
46
     */
47
    public function __construct(ModuleService $module_service, GeoAnalysisViewDataService $geoview_data_service)
48
    {
49
        $this->module = $module_service->findByInterface(GeoDispersionModule::class)->first();
50
        $this->geoview_data_service = $geoview_data_service;
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
56
     */
57
    public function handle(ServerRequestInterface $request): ResponseInterface
58
    {
59
        $tree = Validator::attributes($request)->tree();
60
61
        $admin_config_route = route(AdminConfigPage::class, ['tree' => $tree->name()]);
62
63
        if ($this->module === null) {
64
            FlashMessages::addMessage(
65
                I18N::translate('The attached module could not be found.'),
66
                'danger'
67
            );
68
            return redirect($admin_config_route);
69
        }
70
71
        $type           = Validator::parsedBody($request)->isInArray(['table', 'map'])->string('view_type', '');
72
        $description    = Validator::parsedBody($request)->string('view_description', '');
73
        $place_depth    = Validator::parsedBody($request)->integer('view_depth', 1);
74
75
        $analysis = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $analysis is dead and can be removed.
Loading history...
76
        try {
77
            $analysis = app(Validator::parsedBody($request)->string('view_analysis', ''));
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 ($type === '' || $place_depth <= 0 || $analysis === null || !($analysis instanceof GeoAnalysisInterface)) {
82
            FlashMessages::addMessage(
83
                I18N::translate('The parameters for the new view are not valid.'),
84
                'danger'
85
            );
86
            return redirect($admin_config_route);
87
        }
88
89
        if ($type === 'map') {
90
            $new_view = new GeoAnalysisMap(0, $tree, true, $description, $analysis, $place_depth);
91
        } else {
92
            $new_view = new GeoAnalysisTable(0, $tree, true, $description, $analysis, $place_depth);
93
        }
94
95
        $new_view_id = $this->geoview_data_service->insertGetId($new_view);
96
        if ($new_view_id > 0) {
97
            FlashMessages::addMessage(
98
                I18N::translate('The geographical dispersion analysis view has been successfully added.'),
99
                'success'
100
            );
101
            //phpcs:ignore Generic.Files.LineLength.TooLong
102
            Log::addConfigurationLog('Module ' . $this->module->title() . ' : View “' . $new_view_id . '” has been added.');
103
            return redirect(
104
                route(GeoAnalysisViewEditPage::class, ['tree' => $tree->name(), 'view_id' => $new_view_id ])
105
            );
106
        } else {
107
            FlashMessages::addMessage(
108
                I18N::translate('An error occured while adding the geographical dispersion analysis view.'),
109
                'danger'
110
            );
111
            //phpcs:ignore Generic.Files.LineLength.TooLong
112
            Log::addConfigurationLog('Module ' . $this->module->title() . ' : A new View could not be added. See error log.');
113
            return redirect($admin_config_route);
114
        }
115
    }
116
}
117