74 lines
1.6 KiB
PHP
74 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class Participant extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'user_id',
|
|
'giving_id',
|
|
'prompt',
|
|
'submission_url',
|
|
'desperate',
|
|
'token',
|
|
];
|
|
|
|
protected $casts = [
|
|
'desperate' => 'boolean',
|
|
];
|
|
|
|
public function receiver()
|
|
{
|
|
return $this->belongsTo(Participant::class, 'giving_id');
|
|
}
|
|
|
|
public function giver()
|
|
{
|
|
return $this->hasOne(Participant::class, 'giving_id', 'id');
|
|
}
|
|
|
|
public static function findByToken(string $token): ?self
|
|
{
|
|
return self::where('token', $token)->first();
|
|
}
|
|
|
|
public function getUserData(): ?array
|
|
{
|
|
|
|
if (!$this->token) return null;
|
|
|
|
$url = "https://sketchersunited.org/users/{$this->user_id}";
|
|
|
|
$ch = curl_init($url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
'Accept: application/json'
|
|
]);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
|
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
if ($response !== false && $httpCode === 200) {
|
|
|
|
return json_decode($response, true);
|
|
}
|
|
Log::info($this->user_id);
|
|
return null;
|
|
}
|
|
|
|
public static function withoutReceivers()
|
|
{
|
|
return self::whereNotNull('giving_id')
|
|
->whereDoesntHave('receiver');
|
|
}
|
|
|
|
}
|