| Conditions | 12 |
| Paths | 432 |
| Total Lines | 110 |
| Code Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 70 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 71 | { |
||
| 72 | $parent_id = $request->getAttribute('parent_id'); |
||
| 73 | |||
| 74 | if ($parent_id === null) { |
||
| 75 | $parent = new PlaceLocation(''); |
||
| 76 | } else { |
||
| 77 | $parent = $this->map_data_service->findById((int) $parent_id); |
||
| 78 | } |
||
| 79 | |||
| 80 | for ($tmp = $parent, $hierarchy = []; $tmp->id() !== null; $tmp = $tmp->parent()) { |
||
| 81 | $hierarchy[] = $tmp->locationName(); |
||
| 82 | } |
||
| 83 | |||
| 84 | // Create the file name |
||
| 85 | $filename = preg_replace('/[^\p{L}]+/u', '-', $hierarchy[0] ?? 'Global') . '.csv'; |
||
| 86 | |||
| 87 | // Recursively search for child places |
||
| 88 | $places = []; |
||
| 89 | $queue = [[ |
||
| 90 | $parent->id(), array_reverse($hierarchy), $parent->latitude(), $parent->longitude() |
||
| 91 | ]]; |
||
| 92 | |||
| 93 | while ($queue !== []) { |
||
| 94 | [$id, $hierarchy, $latitude, $longitude] = array_shift($queue); |
||
| 95 | |||
| 96 | if ($latitude !== null && !$longitude !== null) { |
||
| 97 | $places[] = (object) [ |
||
| 98 | 'hierarchy' => $hierarchy, |
||
| 99 | 'latitude' => $latitude, |
||
| 100 | 'longitude' => $longitude, |
||
| 101 | ]; |
||
| 102 | } |
||
| 103 | |||
| 104 | $query = DB::table('place_location'); |
||
| 105 | // Data for the next level. |
||
| 106 | |||
| 107 | if ($id === null) { |
||
| 108 | $query->whereNull('parent_id'); |
||
| 109 | } else { |
||
| 110 | $query->where('parent_id', '=', $id); |
||
| 111 | } |
||
| 112 | |||
| 113 | $rows = $query |
||
| 114 | ->orderBy('place', 'DESC') |
||
| 115 | ->select(['id', 'place', 'latitude', 'longitude']) |
||
| 116 | ->get(); |
||
| 117 | |||
| 118 | $next_level = count($hierarchy); |
||
| 119 | |||
| 120 | foreach ($rows as $row) { |
||
| 121 | $hierarchy[$next_level] = $row->place; |
||
| 122 | array_unshift($queue, [$row->id, $hierarchy, $row->latitude, $row->longitude]); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | // Pad all locations to the length of the longest. |
||
| 127 | $max_level = 0; |
||
| 128 | foreach ($places as $place) { |
||
| 129 | $max_level = max($max_level, count($place->hierarchy)); |
||
| 130 | } |
||
| 131 | |||
| 132 | $places = array_map(function (stdClass $place) use ($max_level): array { |
||
| 133 | return array_merge( |
||
| 134 | [ |
||
| 135 | count($place->hierarchy) - 1, |
||
| 136 | ], |
||
| 137 | array_pad($place->hierarchy, $max_level, ''), |
||
| 138 | [ |
||
| 139 | $this->map_data_service->writeLongitude((float) $place->longitude), |
||
| 140 | $this->map_data_service->writeLatitude((float) $place->latitude), |
||
| 141 | '', |
||
| 142 | '', |
||
| 143 | ] |
||
| 144 | ); |
||
| 145 | }, $places); |
||
| 146 | |||
| 147 | // Create the header line for the output file (always English) |
||
| 148 | $header = [ |
||
| 149 | 'Level', |
||
| 150 | ]; |
||
| 151 | |||
| 152 | for ($i = 0; $i < $max_level; $i++) { |
||
| 153 | $header[] = 'Place' . $i; |
||
| 154 | } |
||
| 155 | |||
| 156 | $header[] = 'Longitude'; |
||
| 157 | $header[] = 'Latitude'; |
||
| 158 | $header[] = 'Zoom'; |
||
| 159 | $header[] = 'Icon'; |
||
| 160 | |||
| 161 | $resource = fopen('php://temp', 'wb+'); |
||
| 162 | |||
| 163 | if ($resource === false) { |
||
| 164 | throw new RuntimeException('Failed to create temporary stream'); |
||
| 165 | } |
||
| 166 | |||
| 167 | fputcsv($resource, $header, MapDataService::CSV_SEPARATOR); |
||
| 168 | |||
| 169 | foreach ($places as $place) { |
||
| 170 | fputcsv($resource, $place, MapDataService::CSV_SEPARATOR); |
||
| 171 | } |
||
| 172 | |||
| 173 | rewind($resource); |
||
| 174 | |||
| 175 | $filename = addcslashes($filename, '"'); |
||
| 176 | |||
| 177 | return response(stream_get_contents($resource)) |
||
| 178 | ->withHeader('Content-Type', 'text/csv; charset=UTF-8') |
||
| 179 | ->withHeader('Content-Disposition', 'attachment; filename="' . $filename . '"'); |
||
| 180 | } |
||
| 182 |