Passed
Push — main ( f9aaf7...4197a4 )
by Jonathan
14:34
created
app/Module/IsSourced/Services/SourceStatusService.php 1 patch
Indentation   +122 added lines, -122 removed lines patch added patch discarded remove patch
@@ -30,126 +30,126 @@
 block discarded – undo
30 30
 class SourceStatusService
31 31
 {
32 32
 
33
-    /**
34
-     * Maximum timespan between the date of a source and the date of the event to consider the source precise.
35
-     * Arbitrally set to approximately a year around the event date.
36
-     *
37
-     * @var int DATE_PRECISION_MARGIN
38
-     */
39
-    private const DATE_PRECISION_MARGIN = 180;
40
-
41
-    /**
42
-     * Return the status of source citations for a fact.
43
-     *
44
-     * @param Fact $fact
45
-     * @return FactSourceStatus
46
-     */
47
-    public function sourceStatusForFact(Fact $fact): FactSourceStatus
48
-    {
49
-        $source_status = new FactSourceStatus();
50
-
51
-        $date = $fact->date();
52
-        $source_status
53
-            ->setFactHasDate($date->isOK())
54
-            ->setFactHasPreciseDate($date->qual1 === '' && $date->minimumJulianDay() === $date->maximumJulianDay());
55
-
56
-        foreach ($fact->getCitations() as $citation) {
57
-            $source_status
58
-                ->setHasSource(true)
59
-                ->addHasSupportingDocument(preg_match('/\n3 _ACT (?:.*)/', $citation) === 1);
60
-
61
-            preg_match_all("/\n3 DATA(?:\n[4-9] .*)*\n4 DATE (.*)/", $citation, $date_matches, PREG_SET_ORDER);
62
-            foreach ($date_matches as $date_match) {
63
-                $source_date = new Date($date_match[1]);
64
-                $source_status
65
-                    ->addSourceHasDate($source_date->isOK())
66
-                    ->addSourceMatchesFactDate($date->isOK() && $source_date->isOK()
67
-                        && abs($source_date->julianDay() - $date->julianDay()) < self::DATE_PRECISION_MARGIN);
68
-            }
69
-
70
-            if ($source_status->isFullySourced()) {
71
-                return $source_status;
72
-            }
73
-        }
74
-
75
-        return $source_status;
76
-    }
77
-
78
-    /**
79
-     * Return the status of sources for a Gedcom record.
80
-     *
81
-     * @param GedcomRecord $record
82
-     * @return SourceStatus
83
-     */
84
-    public function sourceStatusForRecord(GedcomRecord $record): SourceStatus
85
-    {
86
-        $source_status = new SourceStatus();
87
-
88
-        foreach ($record->facts(['SOUR']) as $source) {
89
-            $source_status
90
-                ->setHasSource(true)
91
-                ->addHasSupportingDocument($source->attribute('_ACT') !== '');
92
-
93
-            if ($source_status->isFullySourced()) {
94
-                return $source_status;
95
-            }
96
-        }
97
-
98
-        return $source_status;
99
-    }
100
-
101
-    /**
102
-     * Return the status of source citations for a list of fact types associated with a record.
103
-     *
104
-     * @param GedcomRecord $record
105
-     * @param string[] $tags
106
-     * @return FactSourceStatus
107
-     */
108
-    public function sourceStatusForFactsWithTags(GedcomRecord $record, array $tags): FactSourceStatus
109
-    {
110
-        $source_status = new NullFactSourceStatus();
111
-
112
-        foreach ($record->facts($tags) as $fact) {
113
-            $source_status = $source_status->combineWith($this->sourceStatusForFact($fact));
114
-            if ($source_status->isFullySourced()) {
115
-                return $source_status;
116
-            }
117
-        }
118
-
119
-        return $source_status;
120
-    }
121
-
122
-    /**
123
-     * Return the status of source citations for an individual's birth events.
124
-     *
125
-     * @param Individual $individual
126
-     * @return FactSourceStatus
127
-     */
128
-    public function sourceStatusForBirth(Individual $individual): FactSourceStatus
129
-    {
130
-        return $this->sourceStatusForFactsWithTags($individual, Gedcom::BIRTH_EVENTS);
131
-    }
132
-
133
-    /**
134
-     * Return the status of source citations for an individual's death events.
135
-     *
136
-     * @param Individual $individual
137
-     * @return FactSourceStatus
138
-     */
139
-    public function sourceStatusForDeath(Individual $individual): FactSourceStatus
140
-    {
141
-        return $this->sourceStatusForFactsWithTags($individual, Gedcom::DEATH_EVENTS);
142
-    }
143
-
144
-    /**
145
-     * Return the status of source citations for a family's marriage events.
146
-     *
147
-     * @param Family $family
148
-     * @return FactSourceStatus
149
-     */
150
-    public function sourceStatusForMarriage(Family $family): FactSourceStatus
151
-    {
152
-        $marr_events = array_merge(Gedcom::MARRIAGE_EVENTS, ['MARC', 'MARL', 'MARS']);
153
-        return $this->sourceStatusForFactsWithTags($family, $marr_events);
154
-    }
33
+	/**
34
+	 * Maximum timespan between the date of a source and the date of the event to consider the source precise.
35
+	 * Arbitrally set to approximately a year around the event date.
36
+	 *
37
+	 * @var int DATE_PRECISION_MARGIN
38
+	 */
39
+	private const DATE_PRECISION_MARGIN = 180;
40
+
41
+	/**
42
+	 * Return the status of source citations for a fact.
43
+	 *
44
+	 * @param Fact $fact
45
+	 * @return FactSourceStatus
46
+	 */
47
+	public function sourceStatusForFact(Fact $fact): FactSourceStatus
48
+	{
49
+		$source_status = new FactSourceStatus();
50
+
51
+		$date = $fact->date();
52
+		$source_status
53
+			->setFactHasDate($date->isOK())
54
+			->setFactHasPreciseDate($date->qual1 === '' && $date->minimumJulianDay() === $date->maximumJulianDay());
55
+
56
+		foreach ($fact->getCitations() as $citation) {
57
+			$source_status
58
+				->setHasSource(true)
59
+				->addHasSupportingDocument(preg_match('/\n3 _ACT (?:.*)/', $citation) === 1);
60
+
61
+			preg_match_all("/\n3 DATA(?:\n[4-9] .*)*\n4 DATE (.*)/", $citation, $date_matches, PREG_SET_ORDER);
62
+			foreach ($date_matches as $date_match) {
63
+				$source_date = new Date($date_match[1]);
64
+				$source_status
65
+					->addSourceHasDate($source_date->isOK())
66
+					->addSourceMatchesFactDate($date->isOK() && $source_date->isOK()
67
+						&& abs($source_date->julianDay() - $date->julianDay()) < self::DATE_PRECISION_MARGIN);
68
+			}
69
+
70
+			if ($source_status->isFullySourced()) {
71
+				return $source_status;
72
+			}
73
+		}
74
+
75
+		return $source_status;
76
+	}
77
+
78
+	/**
79
+	 * Return the status of sources for a Gedcom record.
80
+	 *
81
+	 * @param GedcomRecord $record
82
+	 * @return SourceStatus
83
+	 */
84
+	public function sourceStatusForRecord(GedcomRecord $record): SourceStatus
85
+	{
86
+		$source_status = new SourceStatus();
87
+
88
+		foreach ($record->facts(['SOUR']) as $source) {
89
+			$source_status
90
+				->setHasSource(true)
91
+				->addHasSupportingDocument($source->attribute('_ACT') !== '');
92
+
93
+			if ($source_status->isFullySourced()) {
94
+				return $source_status;
95
+			}
96
+		}
97
+
98
+		return $source_status;
99
+	}
100
+
101
+	/**
102
+	 * Return the status of source citations for a list of fact types associated with a record.
103
+	 *
104
+	 * @param GedcomRecord $record
105
+	 * @param string[] $tags
106
+	 * @return FactSourceStatus
107
+	 */
108
+	public function sourceStatusForFactsWithTags(GedcomRecord $record, array $tags): FactSourceStatus
109
+	{
110
+		$source_status = new NullFactSourceStatus();
111
+
112
+		foreach ($record->facts($tags) as $fact) {
113
+			$source_status = $source_status->combineWith($this->sourceStatusForFact($fact));
114
+			if ($source_status->isFullySourced()) {
115
+				return $source_status;
116
+			}
117
+		}
118
+
119
+		return $source_status;
120
+	}
121
+
122
+	/**
123
+	 * Return the status of source citations for an individual's birth events.
124
+	 *
125
+	 * @param Individual $individual
126
+	 * @return FactSourceStatus
127
+	 */
128
+	public function sourceStatusForBirth(Individual $individual): FactSourceStatus
129
+	{
130
+		return $this->sourceStatusForFactsWithTags($individual, Gedcom::BIRTH_EVENTS);
131
+	}
132
+
133
+	/**
134
+	 * Return the status of source citations for an individual's death events.
135
+	 *
136
+	 * @param Individual $individual
137
+	 * @return FactSourceStatus
138
+	 */
139
+	public function sourceStatusForDeath(Individual $individual): FactSourceStatus
140
+	{
141
+		return $this->sourceStatusForFactsWithTags($individual, Gedcom::DEATH_EVENTS);
142
+	}
143
+
144
+	/**
145
+	 * Return the status of source citations for a family's marriage events.
146
+	 *
147
+	 * @param Family $family
148
+	 * @return FactSourceStatus
149
+	 */
150
+	public function sourceStatusForMarriage(Family $family): FactSourceStatus
151
+	{
152
+		$marr_events = array_merge(Gedcom::MARRIAGE_EVENTS, ['MARC', 'MARL', 'MARS']);
153
+		return $this->sourceStatusForFactsWithTags($family, $marr_events);
154
+	}
155 155
 }
Please login to merge, or discard this patch.
app/Module/GeoDispersion/Schema/Migration2.php 1 patch
Indentation   +209 added lines, -209 removed lines patch added patch discarded remove patch
@@ -39,213 +39,213 @@
 block discarded – undo
39 39
  */
40 40
 class Migration2 implements MigrationInterface
41 41
 {
42
-    /**
43
-     * Mapping from old map definitions to new ones
44
-     * @var array<string,mixed> MAPS_XML_MAPPING
45
-     */
46
-    private const MAPS_XML_MAPPING = [
47
-        'aubracmargeridebycommunes.xml' =>  'fr-area-aubrac-lot-margeride-planeze-communes',
48
-        'calvadosbycommunes.xml'        =>  'fr-dpt-14-communes',
49
-        'cantalbycommunes.xml'          =>  'fr-dpt-15-communes',
50
-        'cotesdarmorbycommunes.xml'     =>  'fr-dpt-22-communes',
51
-        'essonnebycommunes.xml'         =>  'fr-dpt-91-communes',
52
-        'eurebycommunes.xml'            =>  'fr-dpt-27-communes',
53
-        'eureetloirbycommunes.xml'      =>  'fr-dpt-28-communes',
54
-        'francebydepartements.xml'      =>  'fr-metropole-departements',
55
-        'francebyregions1970.xml'       =>  'fr-metropole-regions-1970',
56
-        'francebyregions2016.xml'       =>  'fr-metropole-regions-2016',
57
-        'hauteloirebycommunes.xml'      =>  'fr-dpt-43-communes',
58
-        'illeetvilainebycommunes.xml'   =>  'fr-dpt-35-communes',
59
-        'loiretbycommunes.xml'          =>  'fr-dpt-45-communes',
60
-        'lozerebycodepostaux.xml'       =>  'fr-dpt-48-codespostaux',
61
-        'lozerebycommunes.xml'          =>  'fr-dpt-48-communes',
62
-        'mayennebycommunes.xml'         =>  'fr-dpt-53-communes',
63
-        'oisebycommunes.xml'            =>  'fr-dpt-60-communes',
64
-        'ornebycommunes.xml'            =>  'fr-dpt-61-communes',
65
-        'puydedomebycommunes.xml'       =>  'fr-dpt-63-communes',
66
-        'sarthebycommunes.xml'          =>  'fr-dpt-72-communes',
67
-        'seinemaritimebycommunes.xml'   =>  'fr-dpt-76-communes',
68
-        'seinesommeoisebycommunes.xml'  =>  ['fr-dpt-60-communes', 'fr-dpt-76-communes', 'fr-dpt-80-communes'],
69
-        'valdoisebycommunes.xml'        =>  'fr-dpt-95-communes',
70
-        'yvelinesbycommunes.xml'        =>  'fr-dpt-78-communes'
71
-    ];
72
-
73
-    /**
74
-     * {@inheritDoc}
75
-     * @see \Fisharebest\Webtrees\Schema\MigrationInterface::upgrade()
76
-     */
77
-    public function upgrade(): void
78
-    {
79
-        if (!DB::schema()->hasTable('maj_geodispersion')) {
80
-            return;
81
-        }
82
-
83
-        /** @var TreeService $tree_service */
84
-        $tree_service = app(TreeService::class);
85
-        /** @var GeoAnalysisViewDataService $geoview_data_service */
86
-        $geoview_data_service = app(GeoAnalysisViewDataService::class);
87
-
88
-        $existing_views = DB::table('maj_geodispersion')
89
-            ->select()
90
-            ->get();
91
-
92
-        foreach ($existing_views as $old_view) {
93
-            try {
94
-                $tree = $tree_service->find((int) $old_view->majgd_file);
95
-            } catch (RuntimeException $ex) {
96
-                continue;
97
-            }
98
-
99
-            if ($old_view->majgd_map === null) {
100
-                $this->migrateGeoAnalysisTable($old_view, $tree, $geoview_data_service);
101
-            } else {
102
-                DB::connection()->beginTransaction();
103
-                if ($this->migrateGeoAnalysisMap($old_view, $tree, $geoview_data_service)) {
104
-                    DB::connection()->commit();
105
-                } else {
106
-                    DB::connection()->rollBack();
107
-                }
108
-            }
109
-        }
110
-
111
-        DB::schema()->drop('maj_geodispersion');
112
-
113
-        FlashMessages::addMessage(I18N::translate(
114
-            'The geographical dispersion analyses have been migrated for webtrees 2. Please review their settings.'
115
-        ));
116
-    }
117
-
118
-    /**
119
-     * Create a Table geographical analysis view from a migrated item.
120
-     *
121
-     * @param stdClass $old_view
122
-     * @param Tree $tree
123
-     * @param GeoAnalysisViewDataService $geoview_data_service
124
-     * @return bool
125
-     */
126
-    private function migrateGeoAnalysisTable(
127
-        stdClass $old_view,
128
-        Tree $tree,
129
-        GeoAnalysisViewDataService $geoview_data_service
130
-    ): bool {
131
-        $new_view = new GeoAnalysisTable(
132
-            0,
133
-            $tree,
134
-            $old_view->majgd_status === 'enabled',
135
-            $old_view->majgd_descr,
136
-            app(SosaByGenerationGeoAnalysis::class),
137
-            (int) $old_view->majgd_sublevel,
138
-            (int) $old_view->majgd_detailsgen
139
-        );
140
-
141
-        return $geoview_data_service->insertGetId($new_view) > 0;
142
-    }
143
-
144
-    /**
145
-     * Create a Map geographical analysis view from a migrated item.
146
-     *
147
-     * @param stdClass $old_view
148
-     * @param Tree $tree
149
-     * @param GeoAnalysisViewDataService $geoview_data_service
150
-     * @return bool
151
-     */
152
-    private function migrateGeoAnalysisMap(
153
-        stdClass $old_view,
154
-        Tree $tree,
155
-        GeoAnalysisViewDataService $geoview_data_service
156
-    ): bool {
157
-        /** @var MapDefinitionsService $map_definition_service */
158
-        $map_definition_service = app(MapDefinitionsService::class);
159
-        /** @var MapAdapterDataService $mapadapter_data_service */
160
-        $mapadapter_data_service = app(MapAdapterDataService::class);
161
-
162
-        $new_view = new GeoAnalysisMap(
163
-            0,
164
-            $tree,
165
-            $old_view->majgd_status === 'enabled',
166
-            $old_view->majgd_descr,
167
-            app(SosaByGenerationGeoAnalysis::class),
168
-            (int) $old_view->majgd_sublevel,
169
-            (int) $old_view->majgd_detailsgen
170
-        );
171
-
172
-        $view_id = $geoview_data_service->insertGetId($new_view);
173
-        if ($view_id === 0) {
174
-            return false;
175
-        }
176
-        $new_view = $new_view->withId($view_id);
177
-
178
-        $colors = $new_view->colors();
179
-        foreach ($this->mapIdsFromOld($old_view->majgd_map) as $new_map_id) {
180
-            $map = $map_definition_service->find($new_map_id);
181
-            if ($map === null) {
182
-                return false;
183
-            }
184
-            $colors = $this->colorsFromMap($new_map_id);
185
-
186
-            /** @var SimplePlaceMapper $mapper */
187
-            $mapper = app(SimplePlaceMapper::class);
188
-            $mapview_config = new MapViewConfig($this->mappingPropertyForMap($new_map_id), $mapper->config());
189
-            $map_adapter = new GeoAnalysisMapAdapter(0, $view_id, $map, $mapper, $mapview_config);
190
-
191
-            $mapadapter_data_service->insertGetId($map_adapter);
192
-        }
193
-
194
-        return $geoview_data_service->update($new_view->withColors($colors)) > 0;
195
-    }
196
-
197
-    /**
198
-     * Get all new map definitions IDs representing an old map definition
199
-     *
200
-     * @param string $map_xml
201
-     * @return string[]
202
-     */
203
-    private function mapIdsFromOld(string $map_xml): array
204
-    {
205
-        $mapping = self::MAPS_XML_MAPPING[$map_xml] ?? [];
206
-        return is_array($mapping) ? $mapping : [ $mapping ];
207
-    }
208
-
209
-    /**
210
-     * Get the mapping property to be used for the migrated map adapter
211
-     *
212
-     * @param string $map_id
213
-     * @return string
214
-     */
215
-    private function mappingPropertyForMap(string $map_id): string
216
-    {
217
-        switch ($map_id) {
218
-            case 'fr-metropole-regions-1970':
219
-            case 'fr-metropole-regions-2016':
220
-                return 'region_insee_libelle';
221
-            case 'fr-metropole-departements':
222
-                return 'dpt_insee_libelle';
223
-            case 'fr-dpt-48-codespostaux':
224
-                return 'code_postal';
225
-            default:
226
-                return 'commune_insee_libelle';
227
-        }
228
-    }
229
-
230
-    /**
231
-     * Get the color configuration to be used for the migrated map view
232
-     *
233
-     * @param string $map_id
234
-     * @return MapColorsConfig
235
-     */
236
-    private function colorsFromMap(string $map_id): MapColorsConfig
237
-    {
238
-        $default = Hex::fromString('#f5f5f5');
239
-        $stroke = Hex::fromString('#d5d5d5');
240
-        $hover = Hex::fromString('#ff6600');
241
-
242
-        switch ($map_id) {
243
-            case 'fr-metropole-departements':
244
-                return new MapColorsConfig($default, $stroke, Hex::fromString('#0493ab'), $hover);
245
-            case 'fr-dpt-48-codespostaux':
246
-                return new MapColorsConfig($default, $stroke, Hex::fromString('#44aa00'), $hover);
247
-            default:
248
-                return new MapColorsConfig($default, $stroke, Hex::fromString('#e2a61d'), $hover);
249
-        }
250
-    }
42
+	/**
43
+	 * Mapping from old map definitions to new ones
44
+	 * @var array<string,mixed> MAPS_XML_MAPPING
45
+	 */
46
+	private const MAPS_XML_MAPPING = [
47
+		'aubracmargeridebycommunes.xml' =>  'fr-area-aubrac-lot-margeride-planeze-communes',
48
+		'calvadosbycommunes.xml'        =>  'fr-dpt-14-communes',
49
+		'cantalbycommunes.xml'          =>  'fr-dpt-15-communes',
50
+		'cotesdarmorbycommunes.xml'     =>  'fr-dpt-22-communes',
51
+		'essonnebycommunes.xml'         =>  'fr-dpt-91-communes',
52
+		'eurebycommunes.xml'            =>  'fr-dpt-27-communes',
53
+		'eureetloirbycommunes.xml'      =>  'fr-dpt-28-communes',
54
+		'francebydepartements.xml'      =>  'fr-metropole-departements',
55
+		'francebyregions1970.xml'       =>  'fr-metropole-regions-1970',
56
+		'francebyregions2016.xml'       =>  'fr-metropole-regions-2016',
57
+		'hauteloirebycommunes.xml'      =>  'fr-dpt-43-communes',
58
+		'illeetvilainebycommunes.xml'   =>  'fr-dpt-35-communes',
59
+		'loiretbycommunes.xml'          =>  'fr-dpt-45-communes',
60
+		'lozerebycodepostaux.xml'       =>  'fr-dpt-48-codespostaux',
61
+		'lozerebycommunes.xml'          =>  'fr-dpt-48-communes',
62
+		'mayennebycommunes.xml'         =>  'fr-dpt-53-communes',
63
+		'oisebycommunes.xml'            =>  'fr-dpt-60-communes',
64
+		'ornebycommunes.xml'            =>  'fr-dpt-61-communes',
65
+		'puydedomebycommunes.xml'       =>  'fr-dpt-63-communes',
66
+		'sarthebycommunes.xml'          =>  'fr-dpt-72-communes',
67
+		'seinemaritimebycommunes.xml'   =>  'fr-dpt-76-communes',
68
+		'seinesommeoisebycommunes.xml'  =>  ['fr-dpt-60-communes', 'fr-dpt-76-communes', 'fr-dpt-80-communes'],
69
+		'valdoisebycommunes.xml'        =>  'fr-dpt-95-communes',
70
+		'yvelinesbycommunes.xml'        =>  'fr-dpt-78-communes'
71
+	];
72
+
73
+	/**
74
+	 * {@inheritDoc}
75
+	 * @see \Fisharebest\Webtrees\Schema\MigrationInterface::upgrade()
76
+	 */
77
+	public function upgrade(): void
78
+	{
79
+		if (!DB::schema()->hasTable('maj_geodispersion')) {
80
+			return;
81
+		}
82
+
83
+		/** @var TreeService $tree_service */
84
+		$tree_service = app(TreeService::class);
85
+		/** @var GeoAnalysisViewDataService $geoview_data_service */
86
+		$geoview_data_service = app(GeoAnalysisViewDataService::class);
87
+
88
+		$existing_views = DB::table('maj_geodispersion')
89
+			->select()
90
+			->get();
91
+
92
+		foreach ($existing_views as $old_view) {
93
+			try {
94
+				$tree = $tree_service->find((int) $old_view->majgd_file);
95
+			} catch (RuntimeException $ex) {
96
+				continue;
97
+			}
98
+
99
+			if ($old_view->majgd_map === null) {
100
+				$this->migrateGeoAnalysisTable($old_view, $tree, $geoview_data_service);
101
+			} else {
102
+				DB::connection()->beginTransaction();
103
+				if ($this->migrateGeoAnalysisMap($old_view, $tree, $geoview_data_service)) {
104
+					DB::connection()->commit();
105
+				} else {
106
+					DB::connection()->rollBack();
107
+				}
108
+			}
109
+		}
110
+
111
+		DB::schema()->drop('maj_geodispersion');
112
+
113
+		FlashMessages::addMessage(I18N::translate(
114
+			'The geographical dispersion analyses have been migrated for webtrees 2. Please review their settings.'
115
+		));
116
+	}
117
+
118
+	/**
119
+	 * Create a Table geographical analysis view from a migrated item.
120
+	 *
121
+	 * @param stdClass $old_view
122
+	 * @param Tree $tree
123
+	 * @param GeoAnalysisViewDataService $geoview_data_service
124
+	 * @return bool
125
+	 */
126
+	private function migrateGeoAnalysisTable(
127
+		stdClass $old_view,
128
+		Tree $tree,
129
+		GeoAnalysisViewDataService $geoview_data_service
130
+	): bool {
131
+		$new_view = new GeoAnalysisTable(
132
+			0,
133
+			$tree,
134
+			$old_view->majgd_status === 'enabled',
135
+			$old_view->majgd_descr,
136
+			app(SosaByGenerationGeoAnalysis::class),
137
+			(int) $old_view->majgd_sublevel,
138
+			(int) $old_view->majgd_detailsgen
139
+		);
140
+
141
+		return $geoview_data_service->insertGetId($new_view) > 0;
142
+	}
143
+
144
+	/**
145
+	 * Create a Map geographical analysis view from a migrated item.
146
+	 *
147
+	 * @param stdClass $old_view
148
+	 * @param Tree $tree
149
+	 * @param GeoAnalysisViewDataService $geoview_data_service
150
+	 * @return bool
151
+	 */
152
+	private function migrateGeoAnalysisMap(
153
+		stdClass $old_view,
154
+		Tree $tree,
155
+		GeoAnalysisViewDataService $geoview_data_service
156
+	): bool {
157
+		/** @var MapDefinitionsService $map_definition_service */
158
+		$map_definition_service = app(MapDefinitionsService::class);
159
+		/** @var MapAdapterDataService $mapadapter_data_service */
160
+		$mapadapter_data_service = app(MapAdapterDataService::class);
161
+
162
+		$new_view = new GeoAnalysisMap(
163
+			0,
164
+			$tree,
165
+			$old_view->majgd_status === 'enabled',
166
+			$old_view->majgd_descr,
167
+			app(SosaByGenerationGeoAnalysis::class),
168
+			(int) $old_view->majgd_sublevel,
169
+			(int) $old_view->majgd_detailsgen
170
+		);
171
+
172
+		$view_id = $geoview_data_service->insertGetId($new_view);
173
+		if ($view_id === 0) {
174
+			return false;
175
+		}
176
+		$new_view = $new_view->withId($view_id);
177
+
178
+		$colors = $new_view->colors();
179
+		foreach ($this->mapIdsFromOld($old_view->majgd_map) as $new_map_id) {
180
+			$map = $map_definition_service->find($new_map_id);
181
+			if ($map === null) {
182
+				return false;
183
+			}
184
+			$colors = $this->colorsFromMap($new_map_id);
185
+
186
+			/** @var SimplePlaceMapper $mapper */
187
+			$mapper = app(SimplePlaceMapper::class);
188
+			$mapview_config = new MapViewConfig($this->mappingPropertyForMap($new_map_id), $mapper->config());
189
+			$map_adapter = new GeoAnalysisMapAdapter(0, $view_id, $map, $mapper, $mapview_config);
190
+
191
+			$mapadapter_data_service->insertGetId($map_adapter);
192
+		}
193
+
194
+		return $geoview_data_service->update($new_view->withColors($colors)) > 0;
195
+	}
196
+
197
+	/**
198
+	 * Get all new map definitions IDs representing an old map definition
199
+	 *
200
+	 * @param string $map_xml
201
+	 * @return string[]
202
+	 */
203
+	private function mapIdsFromOld(string $map_xml): array
204
+	{
205
+		$mapping = self::MAPS_XML_MAPPING[$map_xml] ?? [];
206
+		return is_array($mapping) ? $mapping : [ $mapping ];
207
+	}
208
+
209
+	/**
210
+	 * Get the mapping property to be used for the migrated map adapter
211
+	 *
212
+	 * @param string $map_id
213
+	 * @return string
214
+	 */
215
+	private function mappingPropertyForMap(string $map_id): string
216
+	{
217
+		switch ($map_id) {
218
+			case 'fr-metropole-regions-1970':
219
+			case 'fr-metropole-regions-2016':
220
+				return 'region_insee_libelle';
221
+			case 'fr-metropole-departements':
222
+				return 'dpt_insee_libelle';
223
+			case 'fr-dpt-48-codespostaux':
224
+				return 'code_postal';
225
+			default:
226
+				return 'commune_insee_libelle';
227
+		}
228
+	}
229
+
230
+	/**
231
+	 * Get the color configuration to be used for the migrated map view
232
+	 *
233
+	 * @param string $map_id
234
+	 * @return MapColorsConfig
235
+	 */
236
+	private function colorsFromMap(string $map_id): MapColorsConfig
237
+	{
238
+		$default = Hex::fromString('#f5f5f5');
239
+		$stroke = Hex::fromString('#d5d5d5');
240
+		$hover = Hex::fromString('#ff6600');
241
+
242
+		switch ($map_id) {
243
+			case 'fr-metropole-departements':
244
+				return new MapColorsConfig($default, $stroke, Hex::fromString('#0493ab'), $hover);
245
+			case 'fr-dpt-48-codespostaux':
246
+				return new MapColorsConfig($default, $stroke, Hex::fromString('#44aa00'), $hover);
247
+			default:
248
+				return new MapColorsConfig($default, $stroke, Hex::fromString('#e2a61d'), $hover);
249
+		}
250
+	}
251 251
 }
Please login to merge, or discard this patch.
Module/GeoDispersion/Http/RequestHandlers/MapAdapterDeleteInvalidAction.php 1 patch
Indentation   +66 added lines, -66 removed lines patch added patch discarded remove patch
@@ -34,78 +34,78 @@
 block discarded – undo
34 34
  */
35 35
 class MapAdapterDeleteInvalidAction implements RequestHandlerInterface
36 36
 {
37
-    private ?GeoDispersionModule $module;
38
-    private GeoAnalysisViewDataService $geoview_data_service;
39
-    private MapAdapterDataService $mapadapter_data_service;
37
+	private ?GeoDispersionModule $module;
38
+	private GeoAnalysisViewDataService $geoview_data_service;
39
+	private MapAdapterDataService $mapadapter_data_service;
40 40
 
41
-    /**
42
-     * Constructor for MapAdapterDeleteInvalidAction Request Handler
43
-     *
44
-     * @param ModuleService $module_service
45
-     * @param GeoAnalysisViewDataService $geoview_data_service
46
-     * @param MapAdapterDataService $mapadapter_data_service
47
-     */
48
-    public function __construct(
49
-        ModuleService $module_service,
50
-        GeoAnalysisViewDataService $geoview_data_service,
51
-        MapAdapterDataService $mapadapter_data_service
52
-    ) {
53
-        $this->module = $module_service->findByInterface(GeoDispersionModule::class)->first();
54
-        $this->geoview_data_service = $geoview_data_service;
55
-        $this->mapadapter_data_service = $mapadapter_data_service;
56
-    }
41
+	/**
42
+	 * Constructor for MapAdapterDeleteInvalidAction Request Handler
43
+	 *
44
+	 * @param ModuleService $module_service
45
+	 * @param GeoAnalysisViewDataService $geoview_data_service
46
+	 * @param MapAdapterDataService $mapadapter_data_service
47
+	 */
48
+	public function __construct(
49
+		ModuleService $module_service,
50
+		GeoAnalysisViewDataService $geoview_data_service,
51
+		MapAdapterDataService $mapadapter_data_service
52
+	) {
53
+		$this->module = $module_service->findByInterface(GeoDispersionModule::class)->first();
54
+		$this->geoview_data_service = $geoview_data_service;
55
+		$this->mapadapter_data_service = $mapadapter_data_service;
56
+	}
57 57
 
58
-    /**
59
-     * {@inheritDoc}
60
-     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
61
-     */
62
-    public function handle(ServerRequestInterface $request): ResponseInterface
63
-    {
64
-        $tree = $request->getAttribute('tree');
65
-        assert($tree instanceof Tree);
58
+	/**
59
+	 * {@inheritDoc}
60
+	 * @see \Psr\Http\Server\RequestHandlerInterface::handle()
61
+	 */
62
+	public function handle(ServerRequestInterface $request): ResponseInterface
63
+	{
64
+		$tree = $request->getAttribute('tree');
65
+		assert($tree instanceof Tree);
66 66
 
67
-        if ($this->module === null) {
68
-            FlashMessages::addMessage(
69
-                I18N::translate('The attached module could not be found.'),
70
-                'danger'
71
-            );
72
-            return redirect(route(AdminConfigPage::class, ['tree' => $tree]));
73
-        }
67
+		if ($this->module === null) {
68
+			FlashMessages::addMessage(
69
+				I18N::translate('The attached module could not be found.'),
70
+				'danger'
71
+			);
72
+			return redirect(route(AdminConfigPage::class, ['tree' => $tree]));
73
+		}
74 74
 
75
-        $view_id = (int) $request->getAttribute('view_id');
76
-        $view = $this->geoview_data_service->find($tree, $view_id);
75
+		$view_id = (int) $request->getAttribute('view_id');
76
+		$view = $this->geoview_data_service->find($tree, $view_id);
77 77
 
78
-        if ($view === null || !($view instanceof GeoAnalysisMap)) {
79
-            FlashMessages::addMessage(
80
-                I18N::translate('The view with ID “%s” does not exist.', I18N::number($view_id)),
81
-                'danger'
82
-            );
83
-            return redirect(route(AdminConfigPage::class, ['tree' => $tree]));
84
-        }
78
+		if ($view === null || !($view instanceof GeoAnalysisMap)) {
79
+			FlashMessages::addMessage(
80
+				I18N::translate('The view with ID “%s” does not exist.', I18N::number($view_id)),
81
+				'danger'
82
+			);
83
+			return redirect(route(AdminConfigPage::class, ['tree' => $tree]));
84
+		}
85 85
 
86
-        /** @var \Illuminate\Support\Collection<int> $valid_map_adapters */
87
-        $valid_map_adapters = $this->mapadapter_data_service
88
-            ->allForView($view)
89
-            ->map(fn(GeoAnalysisMapAdapter $map_adapter): int => $map_adapter->id());
86
+		/** @var \Illuminate\Support\Collection<int> $valid_map_adapters */
87
+		$valid_map_adapters = $this->mapadapter_data_service
88
+			->allForView($view)
89
+			->map(fn(GeoAnalysisMapAdapter $map_adapter): int => $map_adapter->id());
90 90
 
91
-        try {
92
-            $this->mapadapter_data_service->deleteInvalid($view, $valid_map_adapters);
93
-            FlashMessages::addMessage(
94
-                I18N::translate('The invalid map configurations have been successfully deleted.'),
95
-                'success'
96
-            );
97
-        } catch (Throwable $ex) {
98
-            FlashMessages::addMessage(
99
-                I18N::translate('An error occured while deleting the invalid map configurations.'),
100
-                'danger'
101
-            );
102
-            //phpcs:ignore Generic.Files.LineLength.TooLong
103
-            Log::addConfigurationLog('Module ' . $this->module->title() . ' : Error when deleting invalid map configurations: ' . $ex->getMessage());
104
-        }
91
+		try {
92
+			$this->mapadapter_data_service->deleteInvalid($view, $valid_map_adapters);
93
+			FlashMessages::addMessage(
94
+				I18N::translate('The invalid map configurations have been successfully deleted.'),
95
+				'success'
96
+			);
97
+		} catch (Throwable $ex) {
98
+			FlashMessages::addMessage(
99
+				I18N::translate('An error occured while deleting the invalid map configurations.'),
100
+				'danger'
101
+			);
102
+			//phpcs:ignore Generic.Files.LineLength.TooLong
103
+			Log::addConfigurationLog('Module ' . $this->module->title() . ' : Error when deleting invalid map configurations: ' . $ex->getMessage());
104
+		}
105 105
 
106
-        return redirect(route(GeoAnalysisViewEditPage::class, [
107
-            'tree'      => $tree->name(),
108
-            'view_id'   => $view_id
109
-        ]));
110
-    }
106
+		return redirect(route(GeoAnalysisViewEditPage::class, [
107
+			'tree'      => $tree->name(),
108
+			'view_id'   => $view_id
109
+		]));
110
+	}
111 111
 }
Please login to merge, or discard this patch.
app/Module/GeoDispersion/Services/MapAdapterDataService.php 1 patch
Indentation   +157 added lines, -157 removed lines patch added patch discarded remove patch
@@ -31,171 +31,171 @@
 block discarded – undo
31 31
  */
32 32
 class MapAdapterDataService
33 33
 {
34
-    private MapDefinitionsService $mapdefinition_service;
34
+	private MapDefinitionsService $mapdefinition_service;
35 35
 
36
-    /**
37
-     * Constructor for MapAdapterDataService
38
-     *
39
-     * @param MapDefinitionsService $mapdefinition_service
40
-     */
41
-    public function __construct(MapDefinitionsService $mapdefinition_service)
42
-    {
43
-        $this->mapdefinition_service = $mapdefinition_service;
44
-    }
36
+	/**
37
+	 * Constructor for MapAdapterDataService
38
+	 *
39
+	 * @param MapDefinitionsService $mapdefinition_service
40
+	 */
41
+	public function __construct(MapDefinitionsService $mapdefinition_service)
42
+	{
43
+		$this->mapdefinition_service = $mapdefinition_service;
44
+	}
45 45
 
46
-    /**
47
-     * Find a GeoAnalysisMapAdapter by ID
48
-     *
49
-     * @param int $id
50
-     * @return GeoAnalysisMapAdapter|NULL
51
-     */
52
-    public function find(int $id): ?GeoAnalysisMapAdapter
53
-    {
54
-        return DB::table('maj_geodisp_mapviews')
55
-            ->select('maj_geodisp_mapviews.*')
56
-            ->where('majgm_id', '=', $id)
57
-            ->get()
58
-            ->map($this->mapAdapterMapper())
59
-            ->first();
60
-    }
46
+	/**
47
+	 * Find a GeoAnalysisMapAdapter by ID
48
+	 *
49
+	 * @param int $id
50
+	 * @return GeoAnalysisMapAdapter|NULL
51
+	 */
52
+	public function find(int $id): ?GeoAnalysisMapAdapter
53
+	{
54
+		return DB::table('maj_geodisp_mapviews')
55
+			->select('maj_geodisp_mapviews.*')
56
+			->where('majgm_id', '=', $id)
57
+			->get()
58
+			->map($this->mapAdapterMapper())
59
+			->first();
60
+	}
61 61
 
62
-    /**
63
-     * Get all GeoAnalysisMapAdapters linked to a Map View.
64
-     *
65
-     * @param GeoAnalysisMap $map_view
66
-     * @param bool $show_invalid
67
-     * @return Collection<GeoAnalysisMapAdapter|null>
68
-     */
69
-    public function allForView(GeoAnalysisMap $map_view, bool $show_invalid = false): Collection
70
-    {
71
-        $map_adapters = DB::table('maj_geodisp_mapviews')
72
-            ->select('maj_geodisp_mapviews.*')
73
-            ->where('majgm_majgv_id', '=', $map_view->id())
74
-            ->get()
75
-            ->map($this->mapAdapterMapper());
76
-        return $show_invalid ? $map_adapters : $map_adapters->filter();
77
-    }
62
+	/**
63
+	 * Get all GeoAnalysisMapAdapters linked to a Map View.
64
+	 *
65
+	 * @param GeoAnalysisMap $map_view
66
+	 * @param bool $show_invalid
67
+	 * @return Collection<GeoAnalysisMapAdapter|null>
68
+	 */
69
+	public function allForView(GeoAnalysisMap $map_view, bool $show_invalid = false): Collection
70
+	{
71
+		$map_adapters = DB::table('maj_geodisp_mapviews')
72
+			->select('maj_geodisp_mapviews.*')
73
+			->where('majgm_majgv_id', '=', $map_view->id())
74
+			->get()
75
+			->map($this->mapAdapterMapper());
76
+		return $show_invalid ? $map_adapters : $map_adapters->filter();
77
+	}
78 78
 
79
-    /**
80
-     * Insert a GeoAnalysisMapAdapter in the database.
81
-     *
82
-     * @param GeoAnalysisMapAdapter $map_adapter
83
-     * @return int
84
-     */
85
-    public function insertGetId(GeoAnalysisMapAdapter $map_adapter): int
86
-    {
87
-        return DB::table('maj_geodisp_mapviews')
88
-            ->insertGetId([
89
-                'majgm_majgv_id' => $map_adapter->geoAnalysisViewId(),
90
-                'majgm_map_id' => $map_adapter->map()->id(),
91
-                'majgm_mapper' => get_class($map_adapter->placeMapper()),
92
-                'majgm_feature_prop' => $map_adapter->viewConfig()->mapMappingProperty(),
93
-                'majgm_config' => json_encode($map_adapter->viewConfig()->mapperConfig())
94
-            ]);
95
-    }
79
+	/**
80
+	 * Insert a GeoAnalysisMapAdapter in the database.
81
+	 *
82
+	 * @param GeoAnalysisMapAdapter $map_adapter
83
+	 * @return int
84
+	 */
85
+	public function insertGetId(GeoAnalysisMapAdapter $map_adapter): int
86
+	{
87
+		return DB::table('maj_geodisp_mapviews')
88
+			->insertGetId([
89
+				'majgm_majgv_id' => $map_adapter->geoAnalysisViewId(),
90
+				'majgm_map_id' => $map_adapter->map()->id(),
91
+				'majgm_mapper' => get_class($map_adapter->placeMapper()),
92
+				'majgm_feature_prop' => $map_adapter->viewConfig()->mapMappingProperty(),
93
+				'majgm_config' => json_encode($map_adapter->viewConfig()->mapperConfig())
94
+			]);
95
+	}
96 96
 
97
-    /**
98
-     * Update a GeoAnalysisMapAdapter in the database.
99
-     *
100
-     * @param GeoAnalysisMapAdapter $map_adapter
101
-     * @return int
102
-     */
103
-    public function update(GeoAnalysisMapAdapter $map_adapter): int
104
-    {
105
-        return DB::table('maj_geodisp_mapviews')
106
-            ->where('majgm_id', '=', $map_adapter->id())
107
-            ->update([
108
-                'majgm_map_id' => $map_adapter->map()->id(),
109
-                'majgm_mapper' => get_class($map_adapter->placeMapper()),
110
-                'majgm_feature_prop' => $map_adapter->viewConfig()->mapMappingProperty(),
111
-                'majgm_config' => json_encode($map_adapter->placeMapper()->config())
112
-            ]);
113
-    }
97
+	/**
98
+	 * Update a GeoAnalysisMapAdapter in the database.
99
+	 *
100
+	 * @param GeoAnalysisMapAdapter $map_adapter
101
+	 * @return int
102
+	 */
103
+	public function update(GeoAnalysisMapAdapter $map_adapter): int
104
+	{
105
+		return DB::table('maj_geodisp_mapviews')
106
+			->where('majgm_id', '=', $map_adapter->id())
107
+			->update([
108
+				'majgm_map_id' => $map_adapter->map()->id(),
109
+				'majgm_mapper' => get_class($map_adapter->placeMapper()),
110
+				'majgm_feature_prop' => $map_adapter->viewConfig()->mapMappingProperty(),
111
+				'majgm_config' => json_encode($map_adapter->placeMapper()->config())
112
+			]);
113
+	}
114 114
 
115
-    /**
116
-     * Delete a GeoAnalysisMapAdapter from the database.
117
-     *
118
-     * @param GeoAnalysisMapAdapter $map_adapter
119
-     * @return int
120
-     */
121
-    public function delete(GeoAnalysisMapAdapter $map_adapter): int
122
-    {
123
-        return DB::table('maj_geodisp_mapviews')
124
-            ->where('majgm_id', '=', $map_adapter->id())
125
-            ->delete();
126
-    }
115
+	/**
116
+	 * Delete a GeoAnalysisMapAdapter from the database.
117
+	 *
118
+	 * @param GeoAnalysisMapAdapter $map_adapter
119
+	 * @return int
120
+	 */
121
+	public function delete(GeoAnalysisMapAdapter $map_adapter): int
122
+	{
123
+		return DB::table('maj_geodisp_mapviews')
124
+			->where('majgm_id', '=', $map_adapter->id())
125
+			->delete();
126
+	}
127 127
 
128
-    /**
129
-     * Delete invalid GeoAnalysisMapAdapters from the database.
130
-     *
131
-     * @param AbstractGeoAnalysisView $view
132
-     * @param Collection<int> $valid_map_adapters
133
-     * @return int
134
-     */
135
-    public function deleteInvalid(AbstractGeoAnalysisView $view, Collection $valid_map_adapters): int
136
-    {
137
-        return DB::table('maj_geodisp_mapviews')
138
-            ->where('majgm_majgv_id', '=', $view->id())
139
-            ->whereNotIn('majgm_id', $valid_map_adapters)
140
-            ->delete();
141
-    }
128
+	/**
129
+	 * Delete invalid GeoAnalysisMapAdapters from the database.
130
+	 *
131
+	 * @param AbstractGeoAnalysisView $view
132
+	 * @param Collection<int> $valid_map_adapters
133
+	 * @return int
134
+	 */
135
+	public function deleteInvalid(AbstractGeoAnalysisView $view, Collection $valid_map_adapters): int
136
+	{
137
+		return DB::table('maj_geodisp_mapviews')
138
+			->where('majgm_majgv_id', '=', $view->id())
139
+			->whereNotIn('majgm_id', $valid_map_adapters)
140
+			->delete();
141
+	}
142 142
 
143
-    /**
144
-     * Get the closure to create a GeoAnalysisMapAdapter object from a row in the database.
145
-     * It returns null if the classes stored in the DB cannot be loaded through the Laravel container,
146
-     * or if the types do not match with the ones expected.
147
-     *
148
-     * @return Closure(\stdClass $row):?GeoAnalysisMapAdapter
149
-     */
150
-    private function mapAdapterMapper(): Closure
151
-    {
152
-        return function (stdClass $row): ?GeoAnalysisMapAdapter {
153
-            if (null === $map = $this->mapdefinition_service->find($row->majgm_map_id)) {
154
-                return null;
155
-            }
156
-            try {
157
-                $mapper = app($row->majgm_mapper);
158
-                if (!($mapper instanceof PlaceMapperInterface)) {
159
-                    return null;
160
-                }
143
+	/**
144
+	 * Get the closure to create a GeoAnalysisMapAdapter object from a row in the database.
145
+	 * It returns null if the classes stored in the DB cannot be loaded through the Laravel container,
146
+	 * or if the types do not match with the ones expected.
147
+	 *
148
+	 * @return Closure(\stdClass $row):?GeoAnalysisMapAdapter
149
+	 */
150
+	private function mapAdapterMapper(): Closure
151
+	{
152
+		return function (stdClass $row): ?GeoAnalysisMapAdapter {
153
+			if (null === $map = $this->mapdefinition_service->find($row->majgm_map_id)) {
154
+				return null;
155
+			}
156
+			try {
157
+				$mapper = app($row->majgm_mapper);
158
+				if (!($mapper instanceof PlaceMapperInterface)) {
159
+					return null;
160
+				}
161 161
 
162
-                return new GeoAnalysisMapAdapter(
163
-                    (int) $row->majgm_id,
164
-                    (int) $row->majgm_majgv_id,
165
-                    $map,
166
-                    app($row->majgm_mapper),
167
-                    new MapViewConfig($row->majgm_feature_prop, $this->mapperConfigDecoder($row->majgm_config))
168
-                );
169
-            } catch (BindingResolutionException $ex) {
170
-                return null;
171
-            }
172
-        };
173
-    }
162
+				return new GeoAnalysisMapAdapter(
163
+					(int) $row->majgm_id,
164
+					(int) $row->majgm_majgv_id,
165
+					$map,
166
+					app($row->majgm_mapper),
167
+					new MapViewConfig($row->majgm_feature_prop, $this->mapperConfigDecoder($row->majgm_config))
168
+				);
169
+			} catch (BindingResolutionException $ex) {
170
+				return null;
171
+			}
172
+		};
173
+	}
174 174
 
175
-    /**
176
-     * Create a PlaceMapperConfigInterface object from a JSON column value.
177
-     * Returns null if the JSON string is invalid/empty or if the extracted mapper class cannot be loaded
178
-     * through the Laravel container or if the type do not match with the one expected.
179
-     *
180
-     * @param string $json_config
181
-     * @return PlaceMapperConfigInterface|NULL
182
-     */
183
-    private function mapperConfigDecoder(?string $json_config): ?PlaceMapperConfigInterface
184
-    {
185
-        $config = $json_config === null ? [] : json_decode($json_config, true);
186
-        $class = $config['class'] ?? null;
187
-        $json_mapper_config = $config['config'] ?? null;
188
-        if ($class === null || $json_mapper_config === null) {
189
-            return null;
190
-        }
191
-        try {
192
-            $mapper_config = app($class);
193
-            if (!$mapper_config instanceof PlaceMapperConfigInterface) {
194
-                return null;
195
-            }
196
-            return $mapper_config->jsonDeserialize($json_mapper_config);
197
-        } catch (BindingResolutionException $ex) {
198
-            return null;
199
-        }
200
-    }
175
+	/**
176
+	 * Create a PlaceMapperConfigInterface object from a JSON column value.
177
+	 * Returns null if the JSON string is invalid/empty or if the extracted mapper class cannot be loaded
178
+	 * through the Laravel container or if the type do not match with the one expected.
179
+	 *
180
+	 * @param string $json_config
181
+	 * @return PlaceMapperConfigInterface|NULL
182
+	 */
183
+	private function mapperConfigDecoder(?string $json_config): ?PlaceMapperConfigInterface
184
+	{
185
+		$config = $json_config === null ? [] : json_decode($json_config, true);
186
+		$class = $config['class'] ?? null;
187
+		$json_mapper_config = $config['config'] ?? null;
188
+		if ($class === null || $json_mapper_config === null) {
189
+			return null;
190
+		}
191
+		try {
192
+			$mapper_config = app($class);
193
+			if (!$mapper_config instanceof PlaceMapperConfigInterface) {
194
+				return null;
195
+			}
196
+			return $mapper_config->jsonDeserialize($json_mapper_config);
197
+		} catch (BindingResolutionException $ex) {
198
+			return null;
199
+		}
200
+	}
201 201
 }
Please login to merge, or discard this patch.
app/Module/GeoDispersion/Services/GeoAnalysisService.php 1 patch
Indentation   +34 added lines, -34 removed lines patch added patch discarded remove patch
@@ -25,41 +25,41 @@
 block discarded – undo
25 25
  */
26 26
 class GeoAnalysisService
27 27
 {
28
-    private ModuleService $module_service;
28
+	private ModuleService $module_service;
29 29
 
30
-    /**
31
-     * Constructor for MapDefinitionsService
32
-     *
33
-     * @param ModuleService $module_service
34
-     */
35
-    public function __construct(ModuleService $module_service)
36
-    {
37
-        $this->module_service = $module_service;
38
-    }
30
+	/**
31
+	 * Constructor for MapDefinitionsService
32
+	 *
33
+	 * @param ModuleService $module_service
34
+	 */
35
+	public function __construct(ModuleService $module_service)
36
+	{
37
+		$this->module_service = $module_service;
38
+	}
39 39
 
40
-    /**
41
-     * Get all available geographical dispersion analyses.
42
-     *
43
-     * {@internal The list is generated based on the modules exposing ModuleGeoAnalysisProviderInterface
44
-     *
45
-     * @param bool $include_disabled
46
-     * @return Collection<GeoAnalysisInterface>
47
-     */
48
-    public function all(bool $include_disabled = false): Collection
49
-    {
50
-        /** @var Collection<GeoAnalysisInterface> $geoanalyses */
51
-        $geoanalyses = $this->module_service
52
-            ->findByInterface(ModuleGeoAnalysisProviderInterface::class, $include_disabled)
53
-            ->flatMap(fn(ModuleGeoAnalysisProviderInterface $module) => $module->listGeoAnalyses())
54
-            ->map(static function (string $analysis_class): ?GeoAnalysisInterface {
55
-                try {
56
-                    $analysis = app($analysis_class);
57
-                    return $analysis instanceof GeoAnalysisInterface ? $analysis : null;
58
-                } catch (BindingResolutionException $ex) {
59
-                    return null;
60
-                }
61
-            })->filter();
40
+	/**
41
+	 * Get all available geographical dispersion analyses.
42
+	 *
43
+	 * {@internal The list is generated based on the modules exposing ModuleGeoAnalysisProviderInterface
44
+	 *
45
+	 * @param bool $include_disabled
46
+	 * @return Collection<GeoAnalysisInterface>
47
+	 */
48
+	public function all(bool $include_disabled = false): Collection
49
+	{
50
+		/** @var Collection<GeoAnalysisInterface> $geoanalyses */
51
+		$geoanalyses = $this->module_service
52
+			->findByInterface(ModuleGeoAnalysisProviderInterface::class, $include_disabled)
53
+			->flatMap(fn(ModuleGeoAnalysisProviderInterface $module) => $module->listGeoAnalyses())
54
+			->map(static function (string $analysis_class): ?GeoAnalysisInterface {
55
+				try {
56
+					$analysis = app($analysis_class);
57
+					return $analysis instanceof GeoAnalysisInterface ? $analysis : null;
58
+				} catch (BindingResolutionException $ex) {
59
+					return null;
60
+				}
61
+			})->filter();
62 62
 
63
-        return $geoanalyses;
64
-    }
63
+		return $geoanalyses;
64
+	}
65 65
 }
Please login to merge, or discard this patch.
app/Module/GeoDispersion/Services/PlaceMapperService.php 1 patch
Indentation   +34 added lines, -34 removed lines patch added patch discarded remove patch
@@ -25,41 +25,41 @@
 block discarded – undo
25 25
  */
26 26
 class PlaceMapperService
27 27
 {
28
-    private ModuleService $module_service;
28
+	private ModuleService $module_service;
29 29
 
30
-    /**
31
-     * Constructor for PlaceMapperService
32
-     *
33
-     * @param ModuleService $module_service
34
-     */
35
-    public function __construct(ModuleService $module_service)
36
-    {
37
-        $this->module_service = $module_service;
38
-    }
30
+	/**
31
+	 * Constructor for PlaceMapperService
32
+	 *
33
+	 * @param ModuleService $module_service
34
+	 */
35
+	public function __construct(ModuleService $module_service)
36
+	{
37
+		$this->module_service = $module_service;
38
+	}
39 39
 
40
-    /**
41
-     * Get all place mappers available.
42
-     *
43
-     * {@internal The list is generated based on the modules exposing ModulePlaceMapperProviderInterface}
44
-     *
45
-     * @param bool $include_disabled
46
-     * @return Collection<PlaceMapperInterface>
47
-     */
48
-    public function all(bool $include_disabled = false): Collection
49
-    {
50
-        /** @var Collection<PlaceMapperInterface> $place_mappers */
51
-        $place_mappers =  $this->module_service
52
-            ->findByInterface(ModulePlaceMapperProviderInterface::class, $include_disabled)
53
-            ->flatMap(fn(ModulePlaceMapperProviderInterface $module) => $module->listPlaceMappers())
54
-            ->map(static function (string $mapper_class): ?PlaceMapperInterface {
55
-                try {
56
-                    $mapper = app($mapper_class);
57
-                    return $mapper instanceof PlaceMapperInterface ? $mapper : null;
58
-                } catch (BindingResolutionException $ex) {
59
-                    return null;
60
-                }
61
-            })->filter();
40
+	/**
41
+	 * Get all place mappers available.
42
+	 *
43
+	 * {@internal The list is generated based on the modules exposing ModulePlaceMapperProviderInterface}
44
+	 *
45
+	 * @param bool $include_disabled
46
+	 * @return Collection<PlaceMapperInterface>
47
+	 */
48
+	public function all(bool $include_disabled = false): Collection
49
+	{
50
+		/** @var Collection<PlaceMapperInterface> $place_mappers */
51
+		$place_mappers =  $this->module_service
52
+			->findByInterface(ModulePlaceMapperProviderInterface::class, $include_disabled)
53
+			->flatMap(fn(ModulePlaceMapperProviderInterface $module) => $module->listPlaceMappers())
54
+			->map(static function (string $mapper_class): ?PlaceMapperInterface {
55
+				try {
56
+					$mapper = app($mapper_class);
57
+					return $mapper instanceof PlaceMapperInterface ? $mapper : null;
58
+				} catch (BindingResolutionException $ex) {
59
+					return null;
60
+				}
61
+			})->filter();
62 62
 
63
-        return $place_mappers;
64
-    }
63
+		return $place_mappers;
64
+	}
65 65
 }
Please login to merge, or discard this patch.
app/Module/GeoDispersion/Services/GeoAnalysisDataService.php 1 patch
Indentation   +56 added lines, -56 removed lines patch added patch discarded remove patch
@@ -26,64 +26,64 @@
 block discarded – undo
26 26
  */
27 27
 class GeoAnalysisDataService
28 28
 {
29
-    /**
30
-     * Yields indviduals and family records for a specified tree.
31
-     *
32
-     * @param Tree $tree
33
-     * @return \Generator<\Fisharebest\Webtrees\GedcomRecord>
34
-     */
35
-    public function individualsAndFamilies(Tree $tree): Generator
36
-    {
37
-        yield from DB::table('individuals')
38
-            ->where('i_file', '=', $tree->id())
39
-            ->select(['individuals.*'])
40
-            ->get()
41
-            ->map(Registry::individualFactory()->mapper($tree))
42
-            ->filter(GedcomRecord::accessFilter())
43
-            ->all();
29
+	/**
30
+	 * Yields indviduals and family records for a specified tree.
31
+	 *
32
+	 * @param Tree $tree
33
+	 * @return \Generator<\Fisharebest\Webtrees\GedcomRecord>
34
+	 */
35
+	public function individualsAndFamilies(Tree $tree): Generator
36
+	{
37
+		yield from DB::table('individuals')
38
+			->where('i_file', '=', $tree->id())
39
+			->select(['individuals.*'])
40
+			->get()
41
+			->map(Registry::individualFactory()->mapper($tree))
42
+			->filter(GedcomRecord::accessFilter())
43
+			->all();
44 44
 
45
-        yield from DB::table('families')
46
-            ->where('f_file', '=', $tree->id())
47
-            ->select(['families.*'])
48
-            ->get()
49
-            ->map(Registry::familyFactory()->mapper($tree))
50
-            ->filter(GedcomRecord::accessFilter())
51
-            ->all();
52
-    }
45
+		yield from DB::table('families')
46
+			->where('f_file', '=', $tree->id())
47
+			->select(['families.*'])
48
+			->get()
49
+			->map(Registry::familyFactory()->mapper($tree))
50
+			->filter(GedcomRecord::accessFilter())
51
+			->all();
52
+	}
53 53
 
54
-    /**
55
-     * Returns an example of the place hierarchy, from a place within the GEDCOM file, looking for the deepest
56
-     * hierarchy found. The part order is reversed compared to the normal GEDCOM structure (largest first).
57
-     *
58
-     * {@internal The places are taken only from the individuals and families records.}
59
-     *
60
-     * @param Tree $tree
61
-     * @return array<int, string[]>
62
-     */
63
-    public function placeHierarchyExample(Tree $tree): array
64
-    {
65
-        $query_individuals = DB::table('individuals')
66
-            ->select(['i_gedcom AS g_gedcom'])
67
-            ->where('i_file', '=', $tree->id())
68
-            ->where('i_gedcom', 'like', '%2 PLAC %');
54
+	/**
55
+	 * Returns an example of the place hierarchy, from a place within the GEDCOM file, looking for the deepest
56
+	 * hierarchy found. The part order is reversed compared to the normal GEDCOM structure (largest first).
57
+	 *
58
+	 * {@internal The places are taken only from the individuals and families records.}
59
+	 *
60
+	 * @param Tree $tree
61
+	 * @return array<int, string[]>
62
+	 */
63
+	public function placeHierarchyExample(Tree $tree): array
64
+	{
65
+		$query_individuals = DB::table('individuals')
66
+			->select(['i_gedcom AS g_gedcom'])
67
+			->where('i_file', '=', $tree->id())
68
+			->where('i_gedcom', 'like', '%2 PLAC %');
69 69
 
70
-        $query_families = DB::table('families')
71
-            ->select(['f_gedcom AS g_gedcom'])
72
-            ->where('f_file', '=', $tree->id())
73
-            ->where('f_gedcom', 'like', '%2 PLAC %');
70
+		$query_families = DB::table('families')
71
+			->select(['f_gedcom AS g_gedcom'])
72
+			->where('f_file', '=', $tree->id())
73
+			->where('f_gedcom', 'like', '%2 PLAC %');
74 74
 
75
-        return $query_individuals->unionAll($query_families)
76
-            ->get()->pluck('g_gedcom')
77
-            ->flatMap(static function (string $gedcom): array {
78
-                preg_match_all('/\n2 PLAC (.+)/', $gedcom, $matches);
79
-                return $matches[1] ?? [];
80
-            })
81
-            ->sort(I18N::comparator())->reverse()
82
-            ->mapWithKeys(static function (string $place): array {
83
-                $place_array = array_reverse(array_filter(array_map('trim', explode(",", $place))));
84
-                return [ count($place_array) => $place_array ];
85
-            })
86
-            ->sortKeys()
87
-            ->last();
88
-    }
75
+		return $query_individuals->unionAll($query_families)
76
+			->get()->pluck('g_gedcom')
77
+			->flatMap(static function (string $gedcom): array {
78
+				preg_match_all('/\n2 PLAC (.+)/', $gedcom, $matches);
79
+				return $matches[1] ?? [];
80
+			})
81
+			->sort(I18N::comparator())->reverse()
82
+			->mapWithKeys(static function (string $place): array {
83
+				$place_array = array_reverse(array_filter(array_map('trim', explode(",", $place))));
84
+				return [ count($place_array) => $place_array ];
85
+			})
86
+			->sortKeys()
87
+			->last();
88
+	}
89 89
 }
Please login to merge, or discard this patch.
app/Module/GeoDispersion/PlaceMappers/PlaceMapperTrait.php 1 patch
Indentation   +57 added lines, -57 removed lines patch added patch discarded remove patch
@@ -24,67 +24,67 @@
 block discarded – undo
24 24
  */
25 25
 trait PlaceMapperTrait
26 26
 {
27
-    private ?PlaceMapperConfigInterface $config = null;
27
+	private ?PlaceMapperConfigInterface $config = null;
28 28
 
29
-    /** @var array<string, mixed> $data */
30
-    private array $data = [];
29
+	/** @var array<string, mixed> $data */
30
+	private array $data = [];
31 31
 
32
-    /**
33
-     * Implementation of PlaceMapperInterface::boot
34
-     *
35
-     * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::boot()
36
-     */
37
-    public function boot(): void
38
-    {
39
-    }
32
+	/**
33
+	 * Implementation of PlaceMapperInterface::boot
34
+	 *
35
+	 * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::boot()
36
+	 */
37
+	public function boot(): void
38
+	{
39
+	}
40 40
 
41
-    /**
42
-     * Implementation of PlaceMapperInterface::config
43
-     *
44
-     * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::config()
45
-     *
46
-     * @return PlaceMapperConfigInterface
47
-     */
48
-    public function config(): PlaceMapperConfigInterface
49
-    {
50
-        return $this->config ?? new NullPlaceMapperConfig();
51
-    }
41
+	/**
42
+	 * Implementation of PlaceMapperInterface::config
43
+	 *
44
+	 * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::config()
45
+	 *
46
+	 * @return PlaceMapperConfigInterface
47
+	 */
48
+	public function config(): PlaceMapperConfigInterface
49
+	{
50
+		return $this->config ?? new NullPlaceMapperConfig();
51
+	}
52 52
 
53
-    /**
54
-     * Implementation of PlaceMapperInterface::setConfig
55
-     *
56
-     * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::setConfig()
57
-     *
58
-     * @param PlaceMapperConfigInterface $config
59
-     */
60
-    public function setConfig(PlaceMapperConfigInterface $config): void
61
-    {
62
-        $this->config = $config;
63
-    }
53
+	/**
54
+	 * Implementation of PlaceMapperInterface::setConfig
55
+	 *
56
+	 * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::setConfig()
57
+	 *
58
+	 * @param PlaceMapperConfigInterface $config
59
+	 */
60
+	public function setConfig(PlaceMapperConfigInterface $config): void
61
+	{
62
+		$this->config = $config;
63
+	}
64 64
 
65
-    /**
66
-     * Implementation of PlaceMapperInterface::data
67
-     *
68
-     * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::data()
69
-     *
70
-     * @param string $key
71
-     * @return NULL|mixed
72
-     */
73
-    public function data(string $key)
74
-    {
75
-        return $this->data[$key] ?? null;
76
-    }
65
+	/**
66
+	 * Implementation of PlaceMapperInterface::data
67
+	 *
68
+	 * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::data()
69
+	 *
70
+	 * @param string $key
71
+	 * @return NULL|mixed
72
+	 */
73
+	public function data(string $key)
74
+	{
75
+		return $this->data[$key] ?? null;
76
+	}
77 77
 
78
-    /**
79
-     * Implementation of PlaceMapperInterface::setData
80
-     *
81
-     * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::setData()
82
-     *
83
-     * @param string $key
84
-     * @param mixed|null $data
85
-     */
86
-    public function setData(string $key, $data): void
87
-    {
88
-        $this->data[$key] = $data;
89
-    }
78
+	/**
79
+	 * Implementation of PlaceMapperInterface::setData
80
+	 *
81
+	 * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::setData()
82
+	 *
83
+	 * @param string $key
84
+	 * @param mixed|null $data
85
+	 */
86
+	public function setData(string $key, $data): void
87
+	{
88
+		$this->data[$key] = $data;
89
+	}
90 90
 }
Please login to merge, or discard this patch.
app/Module/GeoDispersion/PlaceMappers/TopFilteredPlaceMapperTrait.php 1 patch
Indentation   +57 added lines, -57 removed lines patch added patch discarded remove patch
@@ -21,65 +21,65 @@
 block discarded – undo
21 21
  */
22 22
 trait TopFilteredPlaceMapperTrait
23 23
 {
24
-    /**
25
-     * @var Place[] $top_places
26
-     */
27
-    private array $top_places = [];
24
+	/**
25
+	 * @var Place[] $top_places
26
+	 */
27
+	private array $top_places = [];
28 28
 
29
-    /**
30
-     * Get the list of top level places.
31
-     *
32
-     * @return Place[]
33
-     */
34
-    public function topPlaces(): array
35
-    {
36
-        return $this->top_places;
37
-    }
29
+	/**
30
+	 * Get the list of top level places.
31
+	 *
32
+	 * @return Place[]
33
+	 */
34
+	public function topPlaces(): array
35
+	{
36
+		return $this->top_places;
37
+	}
38 38
 
39
-    /**
40
-     * Set the list of defined top level places.
41
-     *
42
-     * @param Place[] $top_places
43
-     */
44
-    public function setTopPlaces(array $top_places): void
45
-    {
46
-        $this->top_places = collect($top_places)
47
-            ->filter(
48
-                /** @psalm-suppress MissingClosureParamType */
49
-                fn($top_places): bool => $top_places instanceof Place
50
-            )->toArray();
51
-    }
39
+	/**
40
+	 * Set the list of defined top level places.
41
+	 *
42
+	 * @param Place[] $top_places
43
+	 */
44
+	public function setTopPlaces(array $top_places): void
45
+	{
46
+		$this->top_places = collect($top_places)
47
+			->filter(
48
+				/** @psalm-suppress MissingClosureParamType */
49
+				fn($top_places): bool => $top_places instanceof Place
50
+			)->toArray();
51
+	}
52 52
 
53
-    /**
54
-     * Check whether a string ($haystack) ends with another string ($needle)
55
-     *
56
-     * {@internal This is redundant with the function str_ends_with in PHP8}
57
-     *
58
-     * @param string $haystack
59
-     * @param string $needle
60
-     * @return bool
61
-     */
62
-    private function endsWith(string $haystack, string $needle): bool
63
-    {
64
-        return substr_compare($haystack, $needle, -strlen($needle)) === 0;
65
-    }
53
+	/**
54
+	 * Check whether a string ($haystack) ends with another string ($needle)
55
+	 *
56
+	 * {@internal This is redundant with the function str_ends_with in PHP8}
57
+	 *
58
+	 * @param string $haystack
59
+	 * @param string $needle
60
+	 * @return bool
61
+	 */
62
+	private function endsWith(string $haystack, string $needle): bool
63
+	{
64
+		return substr_compare($haystack, $needle, -strlen($needle)) === 0;
65
+	}
66 66
 
67
-    /**
68
-     * Check whether a Place belongs to one of the defined top places.
69
-     *
70
-     * @param Place $place
71
-     * @return bool
72
-     */
73
-    protected function belongsToTopLevels(Place $place): bool
74
-    {
75
-        foreach ($this->top_places as $top_place) {
76
-            if (
77
-                $top_place->tree()->id() === $place->tree()->id() &&
78
-                $this->endsWith($place->gedcomName(), $top_place->gedcomName())
79
-            ) {
80
-                    return true;
81
-            }
82
-        }
83
-        return false;
84
-    }
67
+	/**
68
+	 * Check whether a Place belongs to one of the defined top places.
69
+	 *
70
+	 * @param Place $place
71
+	 * @return bool
72
+	 */
73
+	protected function belongsToTopLevels(Place $place): bool
74
+	{
75
+		foreach ($this->top_places as $top_place) {
76
+			if (
77
+				$top_place->tree()->id() === $place->tree()->id() &&
78
+				$this->endsWith($place->gedcomName(), $top_place->gedcomName())
79
+			) {
80
+					return true;
81
+			}
82
+		}
83
+		return false;
84
+	}
85 85
 }
Please login to merge, or discard this patch.