GeoAnalysisMap::withColors()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
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\Views;
16
17
use Fisharebest\Webtrees\I18N;
18
use Fisharebest\Webtrees\Validator;
19
use Fisharebest\Webtrees\Module\ModuleInterface;
20
use Fisharebest\Webtrees\Services\LeafletJsService;
21
use MyArtJaub\Webtrees\Common\GeoDispersion\Config\MapColorsConfig;
22
use MyArtJaub\Webtrees\Common\GeoDispersion\GeoAnalysis\GeoAnalysisResult;
23
use MyArtJaub\Webtrees\Module\GeoDispersion\GeoDispersionModule;
24
use MyArtJaub\Webtrees\Module\GeoDispersion\Services\MapAdapterDataService;
25
use Psr\Http\Message\ServerRequestInterface;
26
use Spatie\Color\Hex;
27
use Spatie\Color\Rgb;
28
use Spatie\Color\Exceptions\InvalidColorValue;
29
30
/**
31
 * A geographical dispersion analysis view displaying on a map for its global result.
32
 */
33
class GeoAnalysisMap extends AbstractGeoAnalysisView
34
{
35
    private ?MapColorsConfig $colors_config = null;
36
37
    public function type(): string
38
    {
39
        return I18N::translateContext('GEODISPERSION', 'Map');
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     * @see \MyArtJaub\Webtrees\Module\GeoDispersion\Views\AbstractGeoAnalysisView::icon()
45
     */
46
    public function icon(ModuleInterface $module): string
47
    {
48
        return view($module->name() . '::icons/view-map', ['type' => $this->type()]);
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     * @see \MyArtJaub\Webtrees\Module\GeoDispersion\Views\AbstractGeoAnalysisView::globalSettingsContent()
54
     */
55
    public function globalSettingsContent(ModuleInterface $module): string
56
    {
57
        return view($module->name() . '::admin/view-edit-map', [
58
            'module_name'   =>  $module->name(),
59
            'view'          =>  $this,
60
            'colors'        =>  $this->colors(),
61
            'map_adapters'  =>  app(MapAdapterDataService::class)->allForView($this, true)
62
        ]);
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     * @see \MyArtJaub\Webtrees\Module\GeoDispersion\Views\AbstractGeoAnalysisView::withGlobalSettingsUpdate()
68
     * @return static
69
     */
70
    public function withGlobalSettingsUpdate(ServerRequestInterface $request): self
71
    {
72
        $default_color  = Validator::parsedBody($request)->string('view_map_color_default', '');
73
        $stroke_color   = Validator::parsedBody($request)->string('view_map_color_stroke', '');
74
        $maxvalue_color  = Validator::parsedBody($request)->string('view_map_color_maxvalue', '');
75
        $hover_color  = Validator::parsedBody($request)->string('view_map_color_hover', '');
76
77
        try {
78
            return $this->withColors(new MapColorsConfig(
79
                Hex::fromString($default_color),
80
                Hex::fromString($stroke_color),
81
                Hex::fromString($maxvalue_color),
82
                Hex::fromString($hover_color)
83
            ));
84
        } catch (InvalidColorValue $ex) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
85
        }
86
87
        return $this;
88
    }
89
90
    /**
91
     * {@inheritDoc}
92
     * @see \MyArtJaub\Webtrees\Module\GeoDispersion\Views\AbstractGeoAnalysisView::globalTabContent()
93
     */
94
    public function globalTabContent(GeoDispersionModule $module, GeoAnalysisResult $result, array $params): string
95
    {
96
        $map_adapters = app(MapAdapterDataService::class)->allForView($this);
97
98
        $adapter_result = null;
99
        foreach ($map_adapters as $map_adapter) {
100
            $adapter_result_tmp = $map_adapter->convert($result);
101
            $adapter_result = $adapter_result === null ?
102
                $adapter_result_tmp :
103
                $adapter_result->merge($adapter_result_tmp);
0 ignored issues
show
Bug introduced by
The method merge() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
                $adapter_result->/** @scrutinizer ignore-call */ 
104
                                 merge($adapter_result_tmp);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
104
        }
105
106
        if ($adapter_result === null) {
107
            return view($module->name() . '::errors/tab-error', [
108
                'message'   =>  I18N::translate('The map could not be loaded.'),
109
            ]);
110
        }
111
112
        return view($module->name() . '::geoanalysisview-tab-glb-map', $params + [
113
            'result'            =>  $adapter_result->geoAnalysisResult(),
114
            'features'          =>  $adapter_result->features(),
115
            'colors'            =>  $this->colors(),
116
            'leaflet_config'    =>  app(LeafletJsService::class)->config(),
117
            'js_script_url'     =>  $module->assetUrl('js/geodispersion.min.js')
118
        ]);
119
    }
120
121
    /**
122
     * Get the color scheme configuration for the map view
123
     *
124
     * @return MapColorsConfig
125
     */
126
    public function colors(): MapColorsConfig
127
    {
128
        return $this->colors_config ?? new MapColorsConfig(
129
            new Rgb(245, 245, 245),
130
            new Rgb(213, 213, 213),
131
            new Rgb(4, 147, 171),
132
            new Rgb(255, 102, 0)
133
        );
134
    }
135
136
    /**
137
     * Returns a map view with a new color scheme configuration
138
     *
139
     * @param MapColorsConfig $config
140
     * @return static
141
     */
142
    public function withColors(?MapColorsConfig $config): self
143
    {
144
        $new = clone $this;
145
        $new->colors_config = $config;
146
        return $new;
147
    }
148
}
149