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

Freegle / Iznik / 11933

10 May 2026 06:13AM UTC coverage: 72.889% (+0.05%) from 72.843%
11933

push

circleci

web-flow
Merge pull request #405 from Freegle/feat/batch-migrate-non-email-jobs

feat(batch): migrate non-email cron scripts to Laravel artisan commands

13854 of 20887 branches covered (66.33%)

Branch coverage included in aggregate %.

789 of 1004 new or added lines in 21 files covered. (78.59%)

36 existing lines in 3 files now uncovered.

102807 of 139167 relevant lines covered (73.87%)

22.45 hits per line

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

92.0
/iznik-batch/app/Services/VisualiseService.php
1
<?php
2

3
namespace App\Services;
4

5
use App\Models\Message;
6
use App\Models\User;
7
use Illuminate\Support\Facades\DB;
8
use Illuminate\Support\Facades\Log;
9

10
class VisualiseService
11
{
12
    private const MAX_DISTANCE_METRES = 30000;
13

14
    public function scan(string $ago = '30 minutes ago', bool $dryRun = false): int
6✔
15
    {
16
        $since = date('Y-m-d H:i:s', strtotime($ago));
6✔
17

18
        $rows = DB::select(
6✔
19
            "SELECT DISTINCT messages.id, a.id AS attid, messages.fromuser, messages_by.userid AS touser,
6✔
20
                    messages_by.timestamp
21
             FROM messages
22
             INNER JOIN messages_by ON messages.id = messages_by.msgid
23
             INNER JOIN messages_attachments a ON messages.id = a.msgid
24
             WHERE messages_by.timestamp > ? AND messages.type = ? AND messages_by.userid IS NOT NULL
25
             ORDER BY messages_by.timestamp DESC",
6✔
26
            [$since, Message::TYPE_OFFER]
6✔
27
        );
6✔
28

29
        $count = 0;
6✔
30

31
        foreach ($rows as $row) {
6✔
32
            $fromUser = User::find($row->fromuser);
4✔
33
            $toUser   = User::find($row->touser);
4✔
34

35
            if (!$fromUser || !$toUser) {
4✔
NEW
36
                continue;
×
37
            }
38

39
            if (!$this->allowsProfile($fromUser) || !$this->allowsProfile($toUser)) {
4✔
NEW
40
                continue;
×
41
            }
42

43
            [$flat, $flng] = $fromUser->getLatLng();
4✔
44
            [$tlat, $tlng] = $toUser->getLatLng();
4✔
45

46
            if (!$flat || !$flng || !$tlat || !$tlng) {
4✔
47
                continue;
1✔
48
            }
49

50
            $metres = $this->distanceMetres($flat, $flng, $tlat, $tlng);
3✔
51

52
            if ($metres >= self::MAX_DISTANCE_METRES) {
3✔
NEW
53
                continue;
×
54
            }
55

56
            if ($dryRun) {
3✔
57
                Log::debug('Visualise: dry run — would insert', [
1✔
58
                    'msgid' => $row->id,
1✔
59
                    'from' => $row->fromuser,
1✔
60
                    'to' => $row->touser,
1✔
61
                    'metres' => $metres,
1✔
62
                ]);
1✔
63
                $count++;
1✔
64
                continue;
1✔
65
            }
66

67
            DB::statement(
2✔
68
                "INSERT IGNORE INTO visualise (msgid, attid, timestamp, fromuser, touser, fromlat, fromlng, tolat, tolng, distance)
2✔
69
                 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
2✔
70
                [$row->id, $row->attid, $row->timestamp, $row->fromuser, $row->touser, $flat, $flng, $tlat, $tlng, $metres]
2✔
71
            );
2✔
72

73
            $count++;
2✔
74
        }
75

76
        return $count;
6✔
77
    }
78

79
    private function allowsProfile(User $user): bool
4✔
80
    {
81
        $settings = $user->settings;
4✔
82
        if (is_array($settings) && array_key_exists('useprofile', $settings)) {
4✔
NEW
83
            return (bool) $settings['useprofile'];
×
84
        }
85
        return true;
4✔
86
    }
87

88
    private function distanceMetres(float $lat1, float $lng1, float $lat2, float $lng2): int
3✔
89
    {
90
        $earthRadius = 6371000;
3✔
91
        $dLat = deg2rad($lat2 - $lat1);
3✔
92
        $dLng = deg2rad($lng2 - $lng1);
3✔
93
        $a = sin($dLat / 2) ** 2
3✔
94
            + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * sin($dLng / 2) ** 2;
3✔
95
        return (int) round($earthRadius * 2 * asin(sqrt($a)));
3✔
96
    }
97
}
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