2026-08-08 18:03:29 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2026-08-09 12:43:21 +03:00
|
|
|
use App\Jobs\MirrorToolLogo;
|
2026-08-08 18:03:29 +03:00
|
|
|
use Database\Factories\ToolFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
|
|
|
use Illuminate\Database\Eloquent\Attributes\RouteKey;
|
|
|
|
|
use Illuminate\Database\Eloquent\Attributes\Scope;
|
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
|
|
#[Fillable([
|
|
|
|
|
'name', 'slug', 'description', 'url', 'logo_url', 'category', 'subcategory',
|
|
|
|
|
'tags', 'pricing', 'features', 'featured', 'approved', 'user_id',
|
|
|
|
|
])]
|
|
|
|
|
#[RouteKey('slug')]
|
|
|
|
|
class Tool extends Model
|
|
|
|
|
{
|
|
|
|
|
/** @use HasFactory<ToolFactory> */
|
|
|
|
|
use HasFactory;
|
|
|
|
|
|
|
|
|
|
protected function casts(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'tags' => 'array',
|
|
|
|
|
'features' => 'array',
|
|
|
|
|
'featured' => 'boolean',
|
|
|
|
|
'approved' => 'boolean',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static function booted(): void
|
|
|
|
|
{
|
|
|
|
|
static::creating(function (Tool $tool) {
|
|
|
|
|
if (empty($tool->slug)) {
|
|
|
|
|
$tool->slug = Str::slug($tool->name);
|
|
|
|
|
}
|
|
|
|
|
});
|
2026-08-09 12:43:21 +03:00
|
|
|
|
|
|
|
|
static::created(function (Tool $tool) {
|
|
|
|
|
$tool->queueLogoMirrorIfNeeded();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
static::updated(function (Tool $tool) {
|
|
|
|
|
if ($tool->wasChanged('logo_url')) {
|
|
|
|
|
$tool->queueLogoMirrorIfNeeded();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function hasLocalLogo(): bool
|
|
|
|
|
{
|
|
|
|
|
return str_starts_with((string) $this->logo_url, rtrim(config('app.url'), '/').'/storage/tools/');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function queueLogoMirrorIfNeeded(): void
|
|
|
|
|
{
|
|
|
|
|
if ($this->logo_url && ! $this->hasLocalLogo()) {
|
|
|
|
|
MirrorToolLogo::dispatch($this, $this->logo_url);
|
|
|
|
|
}
|
2026-08-08 18:03:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return BelongsTo<User, $this>
|
|
|
|
|
*/
|
|
|
|
|
public function user(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Builder<Tool> $query
|
|
|
|
|
*/
|
|
|
|
|
#[Scope]
|
|
|
|
|
protected function approved(Builder $query): void
|
|
|
|
|
{
|
|
|
|
|
$query->where('approved', true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function categoryLabel(): Attribute
|
|
|
|
|
{
|
|
|
|
|
return Attribute::get(fn () => config("tools.categories.{$this->category}", $this->category));
|
|
|
|
|
}
|
2026-08-09 12:43:21 +03:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Structured-data-safe price, parsed from the free-text `pricing` field
|
|
|
|
|
* (e.g. "Free", "$29/month"). schema.org's Offer.price expects a plain
|
|
|
|
|
* number with a currency, so values we can't parse into that (e.g.
|
|
|
|
|
* "Freemium", "Contact for pricing") are left out rather than guessed.
|
|
|
|
|
*/
|
|
|
|
|
protected function offerData(): Attribute
|
|
|
|
|
{
|
|
|
|
|
return Attribute::get(function () {
|
|
|
|
|
if (! $this->pricing) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strcasecmp($this->pricing, 'free') === 0) {
|
|
|
|
|
return ['price' => '0', 'priceCurrency' => 'USD'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (preg_match('/^\$(\d+(?:\.\d+)?)/', $this->pricing, $matches)) {
|
|
|
|
|
return ['price' => $matches[1], 'priceCurrency' => 'USD'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-08-08 18:03:29 +03:00
|
|
|
}
|