• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Freegle / Iznik / 10872

07 May 2026 06:43PM UTC coverage: 72.691% (-0.02%) from 72.711%
10872

push

circleci

fnnbrr
Fixed bugs related to updating TN user location

13719 of 20696 branches covered (66.29%)

Branch coverage included in aggregate %.

0 of 1 new or added line in 1 file covered. (0.0%)

11 existing lines in 4 files now uncovered.

99532 of 135101 relevant lines covered (73.67%)

22.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.88
/iznik-batch/app/Models/Location.php
1
<?php
2

3
namespace App\Models;
4

5
use Illuminate\Database\Eloquent\Model;
6

7
class Location extends Model
8
{
9
    // Fields exposed by getPublic() - mirrors iznik-server Location::$publicatts.
10
    private const PUBLIC_ATTS = ['id', 'osm_id', 'name', 'type', 'popularity', 'gridid', 'postcodeid', 'areaid', 'lat', 'lng', 'maxdimension'];
11

12
    protected $table = 'locations';
13
    protected $guarded = ['id'];
14
    public $timestamps = FALSE;
15

16
    protected $casts = [
17
        'lat' => 'decimal:6',
18
        'lng' => 'decimal:6',
19
        'osm_place' => 'boolean',
20
        'osm_amenity' => 'boolean',
21
        'osm_shop' => 'boolean',
22
    ];
23

24
    /**
25
     * Find the closest full postcode to a given lat/lng.
26
     *
27
     * Uses an expanding spatial bounding box search for efficiency - starts small
28
     * and doubles until a result is found or the max scan radius is reached.
29
     *
30
     * @return array|null  Array with id, name, areaid, lat, lng, and optionally 'area' info, or null if not found
31
     */
32
    public static function closestPostcode(float $lat, float $lng): ?array
1✔
33
    {
34
        $srid = config('freegle.srid', 3857);
1✔
35
        $scan = 0.00001953125;
1✔
36

37
        while ($scan <= 0.2) {
1✔
38
            $swlat = $lat - $scan;
1✔
39
            $nelat = $lat + $scan;
1✔
40
            $swlng = $lng - $scan;
1✔
41
            $nelng = $lng + $scan;
1✔
42

43
            $poly = "POLYGON(($swlng $swlat, $swlng $nelat, $nelng $nelat, $nelng $swlat, $swlng $swlat))";
1✔
44
            $point = "POINT($lng $lat)";
1✔
45

46
            $result = LocationSpatial::query()
1✔
47
                ->join('locations', 'locations.id', '=', 'locations_spatial.locationid')
1✔
48
                ->selectRaw('locations.id, locations.name, locations.areaid, locations.lat, locations.lng')
1✔
49
                ->whereRaw("MBRContains(ST_Envelope(ST_GeomFromText(?, ?)), locations_spatial.geometry)", [$poly, $srid])
1✔
50
                ->where('locations.type', 'Postcode')
1✔
51
                ->whereRaw("LOCATE(' ', locations.name) > 0")
1✔
52
                ->orderByRaw("ST_distance(locations_spatial.geometry, ST_GeomFromText(?, ?)) ASC", [$point, $srid])
1✔
53
                ->orderByRaw("CASE WHEN ST_Dimension(locations_spatial.geometry) < 2 THEN 0 ELSE ST_AREA(locations_spatial.geometry) END ASC")
1✔
54
                ->limit(1)
1✔
55
                ->first();
1✔
56

57
            if ($result) {
1✔
NEW
58
                $ret = $result->toArray();
×
59

60
                if ($ret['areaid']) {
×
61
                    $area = self::find($ret['areaid']);
×
62

63
                    if ($area) {
×
64
                        $ret['area'] = $area->toArray();
×
65
                    }
66

67
                    unset($ret['areaid']);
×
68
                }
69

70
                return $ret;
×
71
            }
72

73
            $scan *= 2;
1✔
74
        }
75

76
        return null;
1✔
77
    }
78

79
    /**
80
     * Get the public representation of this location.
81
     *
82
     * Ported from iznik-server Location::$publicatts + Entity::getPublic().
83
     */
84
    public function getPublic(): array
×
85
    {
86
        return $this->only(self::PUBLIC_ATTS);
×
87
    }
88
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc