File: /var/www/quadcode-jobs/app/Models/DataRequest.php
<?php
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
/**
* @property int $id
* @property string $email
* @property int $type
* @property string $token
* @property string $updated_at
* @property string $path
*/
class DataRequest extends Model
{
protected $table = 'data_requests';
public $timestamps = false;
const TYPE_REQUEST = 1;
const TYPE_REMOVE = 2;
const TYPE_NO_DATA = 3;
protected $fillable = [
'email',
'type',
'token',
'path',
'updated_at',
];
public static function createRequest(string $email, int $type = self::TYPE_REMOVE, ?string $path = null): self
{
$dataRequest = new self();
$dataRequest->email = strtolower($email);
$dataRequest->type = $type;
$dataRequest->token = Str::random(32);
$dataRequest->updated_at = Carbon::now();
if (isset($path)) {
$dataRequest->path = $path;
}
$dataRequest->save();
return $dataRequest;
}
public function clear(): void
{
if (!empty($this->path)) {
File::delete($this->path);
}
$this->delete();
}
public static function isLimitReached(string $email, int $type): bool
{
return self::whereRaw('LOWER(email) = \'' . strtolower($email) . '\'')
->where('type', $type)
->where('updated_at', '>', date('Y-m-d H:i:s', strtotime('-1 hour')))
->count() > 1;
}
}