slink/laravel/app/Models/Shortlink.php

58 lines
1.6 KiB
PHP
Raw Permalink Normal View History

2024-06-10 03:31:51 +00:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Shortlink extends Model
{
use HasFactory;
protected $fillable = [
'id',
2024-06-10 03:31:51 +00:00
'shortid',
'destination',
'user_id',
'max_clicks',
'expires_at',
'deleted'
2024-06-10 03:31:51 +00:00
];
public function create(string $url, int $user_id): Shortlink {
$this->shortid = $this->generateNewId();
$this->destination = $url;
$this->user_id = $user_id;
$this->max_clicks = 0;
$this->expires_at = null;
$this->deleted = false;
2024-06-10 03:31:51 +00:00
$this->save();
return $this;
}
public function delete(): void {
Shortlink::where('shortid', $this->id)->delete();
}
public function modify(int $max_clicks, $expires_at): void {
$this->max_clicks = $max_clicks;
$this->expires_at = $expires_at;
$this->save();
}
function generateNewId(int $length = 6): string {
$characters = 'qwrtypsdfghjklzxcvbnmQWRTYPSDFGHJKLZXCVBNM256789_';
// try n x 2 times to generate a new id, if it finds an id, return it, otherwise, try to look for an id of length + 1
for ($i = 0; $i < $length * 2; $i++) {
$id = '';
for ($i = 0; $i < $length; $i++) {
$id .= $characters[rand(0, strlen($characters) - 1)];
}
if (Shortlink::where('shortid', $id)->count() == 0) {
return $id;
}
}
return $this->generateNewId($length + 1);
}
}