Skip to content

Commit 90978bc

Browse files
committed
exploring address pinging
1 parent 54185dc commit 90978bc

File tree

8 files changed

+262
-0
lines changed

8 files changed

+262
-0
lines changed

app/Jobs/PingAddressCommand.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use App\Models\PingAddress;
6+
use Illuminate\Bus\Queueable;
7+
use Illuminate\Contracts\Queue\ShouldBeUnique;
8+
use Illuminate\Contracts\Queue\ShouldQueue;
9+
use Illuminate\Foundation\Bus\Dispatchable;
10+
use Illuminate\Queue\InteractsWithQueue;
11+
use Illuminate\Queue\SerializesModels;
12+
13+
class PingAddressCommand implements ShouldQueue
14+
{
15+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
16+
17+
/**
18+
* Create a new job instance.
19+
*
20+
* @return void
21+
*/
22+
public function __construct(
23+
public PingAddress $pingAddress
24+
) {}
25+
26+
/**
27+
* Execute the job.
28+
*
29+
* @return void
30+
*/
31+
public function handle()
32+
{
33+
// https://github.com/striebwj/laravel-ping/blob/master/src/LaravelPing.php
34+
35+
// create a new cURL resource
36+
$ch = curl_init($this->pingAddress->url);
37+
curl_setopt($ch, CURLOPT_USERAGENT, 'Speedtest Tracker (https://docs.speedtest-tracker.dev)');
38+
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
39+
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
40+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
41+
42+
// grab URL and pass it to the browser
43+
curl_exec($ch);
44+
45+
$data = curl_getinfo($ch);
46+
47+
// close cURL resource, and free up system resources
48+
curl_close($ch);
49+
}
50+
}

app/Models/Ping.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Ping extends Model
9+
{
10+
use HasFactory;
11+
12+
/**
13+
* Indicates if the model should be timestamped.
14+
*
15+
* @var bool
16+
*/
17+
public $timestamps = false;
18+
19+
/**
20+
* The attributes that are mass assignable.
21+
*
22+
* @var array
23+
*/
24+
protected $fillable = [
25+
'http_code',
26+
'total_time',
27+
'data',
28+
'is_successful',
29+
];
30+
31+
/**
32+
* The attributes that should be cast.
33+
*
34+
* @var array
35+
*/
36+
protected $casts = [
37+
'is_scheduled' => 'boolean',
38+
'data' => 'array',
39+
'created_at' => 'datetime',
40+
];
41+
42+
public function address()
43+
{
44+
return $this->belongsTo(PingAddress::class, 'address_id');
45+
}
46+
}

app/Models/PingAddress.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class PingAddress extends Model
9+
{
10+
use HasFactory;
11+
12+
/**
13+
* The attributes that are mass assignable.
14+
*
15+
* @var array
16+
*/
17+
protected $fillable = [
18+
'url',
19+
'max_redirects',
20+
'timeout',
21+
];
22+
23+
public function pings()
24+
{
25+
return $this->hasMany(Ping::class, 'address_id');
26+
}
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
7+
/**
8+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\PingAddress>
9+
*/
10+
class PingAddressFactory extends Factory
11+
{
12+
/**
13+
* Define the model's default state.
14+
*
15+
* @return array<string, mixed>
16+
*/
17+
public function definition()
18+
{
19+
return [
20+
//
21+
];
22+
}
23+
}

database/factories/PingFactory.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
7+
/**
8+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Ping>
9+
*/
10+
class PingFactory extends Factory
11+
{
12+
/**
13+
* Define the model's default state.
14+
*
15+
* @return array<string, mixed>
16+
*/
17+
public function definition()
18+
{
19+
return [
20+
//
21+
];
22+
}
23+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('ping_addresses', function (Blueprint $table) {
17+
$table->id();
18+
$table->string('url');
19+
$table->integer('max_redirects')->default(5);
20+
$table->integer('timeout')->default(5);
21+
$table->timestamps();
22+
});
23+
}
24+
25+
/**
26+
* Reverse the migrations.
27+
*
28+
* @return void
29+
*/
30+
public function down()
31+
{
32+
Schema::dropIfExists('ping_addresses');
33+
}
34+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('pings', function (Blueprint $table) {
17+
$table->id();
18+
$table->foreignId('address_id');
19+
$table->integer('http_code')->nullable();
20+
$table->float('total_time')->nullable();
21+
$table->boolean('is_successful')->default(false);
22+
$table->json('data')->nullable(); // is a dump of the curl_getinfo() function
23+
$table->timestamp('created_at')->useCurrent();
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::dropIfExists('pings');
35+
}
36+
};

routes/test.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Illuminate\Support\Facades\Http;
34
use Illuminate\Support\Facades\Route;
45

56
Route::group(
@@ -8,5 +9,27 @@
89
],
910
function () {
1011
// silence is golden
12+
13+
Route::get('/', function () {
14+
15+
// https://github.com/striebwj/laravel-ping/blob/master/src/LaravelPing.php
16+
17+
// create a new cURL resource
18+
$ch = curl_init('https://alexjustesen.com/');
19+
curl_setopt($ch, CURLOPT_USERAGENT, 'Speedtest Tracker (https://docs.speedtest-tracker.dev)');
20+
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
21+
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
22+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
23+
24+
// grab URL and pass it to the browser
25+
curl_exec($ch);
26+
27+
$data = curl_getinfo($ch);
28+
29+
// close cURL resource, and free up system resources
30+
curl_close($ch);
31+
32+
dd($data);
33+
});
1134
}
1235
);

0 commit comments

Comments
 (0)