| Total Complexity | 45 |
| Total Lines | 359 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PlaceHierarchyListModule often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PlaceHierarchyListModule, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 54 | class PlaceHierarchyListModule extends AbstractModule implements ModuleListInterface, RequestHandlerInterface |
||
| 55 | { |
||
| 56 | use ModuleListTrait; |
||
| 57 | |||
| 58 | protected const ROUTE_URL = '/tree/{tree}/place-list'; |
||
| 59 | |||
| 60 | /** @var int The default access level for this module. It can be changed in the control panel. */ |
||
| 61 | protected $access_level = Auth::PRIV_USER; |
||
| 62 | |||
| 63 | /** @var SearchService */ |
||
| 64 | private $search_service; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * PlaceHierarchy constructor. |
||
| 68 | * |
||
| 69 | * @param SearchService $search_service |
||
| 70 | */ |
||
| 71 | public function __construct(SearchService $search_service) |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Initialization. |
||
| 78 | * |
||
| 79 | * @return void |
||
| 80 | */ |
||
| 81 | public function boot(): void |
||
| 82 | { |
||
| 83 | $router_container = app(RouterContainer::class); |
||
| 84 | assert($router_container instanceof RouterContainer); |
||
| 85 | |||
| 86 | $router_container->getMap() |
||
| 87 | ->get(static::class, static::ROUTE_URL, $this); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * How should this module be identified in the control panel, etc.? |
||
| 92 | * |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | public function title(): string |
||
| 96 | { |
||
| 97 | /* I18N: Name of a module/list */ |
||
| 98 | return I18N::translate('Place hierarchy'); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * A sentence describing what this module does. |
||
| 103 | * |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | public function description(): string |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * CSS class for the URL. |
||
| 114 | * |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | public function listMenuClass(): string |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param Tree $tree |
||
| 124 | * @param mixed[] $parameters |
||
| 125 | * |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | public function listUrl(Tree $tree, array $parameters = []): string |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @return string[] |
||
| 137 | */ |
||
| 138 | public function listUrlAttributes(): array |
||
| 139 | { |
||
| 140 | return []; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param Tree $tree |
||
| 145 | * |
||
| 146 | * @return bool |
||
| 147 | */ |
||
| 148 | public function listIsEmpty(Tree $tree): bool |
||
| 149 | { |
||
| 150 | return !DB::table('places') |
||
| 151 | ->where('p_file', '=', $tree->id()) |
||
| 152 | ->exists(); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Handle URLs generated by older versions of webtrees |
||
| 157 | * |
||
| 158 | * @param ServerRequestInterface $request |
||
| 159 | * |
||
| 160 | * @return ResponseInterface |
||
| 161 | */ |
||
| 162 | public function getListAction(ServerRequestInterface $request): ResponseInterface |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param ServerRequestInterface $request |
||
| 169 | * |
||
| 170 | * @return ResponseInterface |
||
| 171 | */ |
||
| 172 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 173 | { |
||
| 174 | $tree = $request->getAttribute('tree'); |
||
| 175 | assert($tree instanceof Tree); |
||
| 176 | |||
| 177 | $user = $request->getAttribute('user'); |
||
| 178 | assert($user instanceof UserInterface); |
||
| 179 | |||
| 180 | Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user); |
||
| 181 | |||
| 182 | $action2 = $request->getQueryParams()['action2'] ?? 'hierarchy'; |
||
| 183 | $place_id = (int) ($request->getQueryParams()['place_id'] ?? 0); |
||
| 184 | $place = Place::find($place_id, $tree); |
||
| 185 | |||
| 186 | // Request for a non-existent place? |
||
| 187 | if ($place_id !== $place->id()) { |
||
| 188 | return redirect($place->url()); |
||
| 189 | } |
||
| 190 | |||
| 191 | $content = ''; |
||
| 192 | $showmap = Site::getPreference('map-provider') !== ''; |
||
| 193 | $data = null; |
||
| 194 | |||
| 195 | if ($showmap) { |
||
| 196 | $content .= view('modules/place-hierarchy/map', [ |
||
| 197 | 'data' => $this->mapData($tree, $place), |
||
| 198 | 'provider' => [ |
||
| 199 | 'url' => 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', |
||
| 200 | 'options' => [ |
||
| 201 | 'attribution' => '<a href="https://www.openstreetmap.org/copyright">© OpenStreetMap</a> contributors', |
||
| 202 | 'max_zoom' => 19 |
||
| 203 | ] |
||
| 204 | ] |
||
| 205 | ]); |
||
| 206 | } |
||
| 207 | |||
| 208 | switch ($action2) { |
||
| 209 | case 'list': |
||
| 210 | default: |
||
| 211 | $alt_link = I18N::translate('Show place hierarchy'); |
||
| 212 | $alt_url = $this->listUrl($tree, ['action2' => 'hierarchy', 'place_id' => $place_id]); |
||
| 213 | $content .= view('modules/place-hierarchy/list', ['columns' => $this->getList($tree)]); |
||
| 214 | break; |
||
| 215 | case 'hierarchy': |
||
| 216 | case 'hierarchy-e': |
||
| 217 | $alt_link = I18N::translate('Show all places in a list'); |
||
| 218 | $alt_url = $this->listUrl($tree, ['action2' => 'list', 'place_id' => 0]); |
||
| 219 | $data = $this->getHierarchy($place); |
||
| 220 | $content .= (null === $data || $showmap) ? '' : view('place-hierarchy', $data); |
||
| 221 | if (null === $data || $action2 === 'hierarchy-e') { |
||
| 222 | $content .= view('modules/place-hierarchy/events', [ |
||
| 223 | 'indilist' => $this->search_service->searchIndividualsInPlace($place), |
||
| 224 | 'famlist' => $this->search_service->searchFamiliesInPlace($place), |
||
| 225 | 'tree' => $place->tree(), |
||
| 226 | ]); |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | if ($data !== null && $action2 !== 'hierarchy-e' && $place->gedcomName() !== '') { |
||
| 231 | $events_link = $this->listUrl($tree, ['action2' => 'hierarchy-e', 'place_id' => $place_id]); |
||
| 232 | } else { |
||
| 233 | $events_link = ''; |
||
| 234 | } |
||
| 235 | |||
| 236 | $breadcrumbs = $this->breadcrumbs($place); |
||
| 237 | |||
| 238 | $geo_link = ''; |
||
| 239 | if (Auth::isAdmin() && $showmap) { |
||
| 240 | $location = new PlaceLocation($place->gedcomName()); |
||
| 241 | if ($location->id() !== 0 || $location->parent()->id() !== 0) { //Can't edit the world's coordinates |
||
| 242 | $geo_link = route('map-data-edit', ['place_id' => $location->id(), 'parent_id' => $location->parent()->id()]); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | return $this->viewResponse('modules/place-hierarchy/page', [ |
||
| 247 | 'geo_link' => $geo_link, |
||
| 248 | 'alt_link' => $alt_link, |
||
| 249 | 'alt_url' => $alt_url, |
||
| 250 | 'breadcrumbs' => $breadcrumbs['breadcrumbs'], |
||
| 251 | 'content' => $content, |
||
| 252 | 'current' => $breadcrumbs['current'], |
||
| 253 | 'events_link' => $events_link, |
||
| 254 | 'place' => $place, |
||
| 255 | 'title' => I18N::translate('Place hierarchy'), |
||
| 256 | 'tree' => $tree, |
||
| 257 | 'world_url' => $this->listUrl($tree) |
||
| 258 | ]); |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param Tree $tree |
||
| 263 | * |
||
| 264 | * @return array<array<Place>> |
||
| 265 | */ |
||
| 266 | private function getList(Tree $tree): array |
||
| 283 | } |
||
| 284 | |||
| 285 | |||
| 286 | /** |
||
| 287 | * @param Place $place |
||
| 288 | * |
||
| 289 | * @return array{'tree':Tree,'col_class':string,'columns':array<array<Place>>,'place':Place}|null |
||
| 290 | */ |
||
| 291 | private function getHierarchy(Place $place): ?array |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param Place $place |
||
| 312 | * |
||
| 313 | * @return array{'breadcrumbs':array<Place>,'current':Place|null} |
||
| 314 | */ |
||
| 315 | private function breadcrumbs(Place $place): array |
||
| 334 | ]; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param Tree $tree |
||
| 339 | * @param Place $placeObj |
||
| 340 | * |
||
| 341 | * @return array<mixed> |
||
| 342 | */ |
||
| 343 | protected function mapData(Tree $tree, Place $placeObj): array |
||
| 413 | ] |
||
| 414 | ]; |
||
| 417 |