urlsnail/app/Jobs/ImportBookmark.php

52 lines
1.4 KiB
PHP
Raw Permalink Normal View History

2024-05-26 21:53:09 +00:00
<?php
namespace App\Jobs;
use App\Models\Bookmark;
use App\Models\BookmarkTag;
use App\Models\Tag;
2024-05-30 23:01:16 +00:00
use App\Models\User;
2024-05-26 21:53:09 +00:00
use DateTimeImmutable;
use DateTimeInterface;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class ImportBookmark implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected array $bookmarks_json;
/**
* Create a new job instance.
*/
2024-05-30 23:01:16 +00:00
public function __construct(protected User $user, array ...$bookmarks_json)
2024-05-26 21:53:09 +00:00
{
$this->bookmarks_json = $bookmarks_json;
}
/**
* Execute the job.
*/
public function handle(): void
{
foreach ($this->bookmarks_json as $bookmark_json) {
$created_at = DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601_EXPANDED, $bookmark_json['time']);
$bookmark = new Bookmark;
2024-05-30 23:01:16 +00:00
$bookmark->user()->associate($this->user);
2024-05-26 21:53:09 +00:00
$bookmark->href = $bookmark_json['href'];
$bookmark->title = $bookmark_json['description'];
$bookmark->description = $bookmark_json['extended'];
$bookmark->created_at = $created_at;
$bookmark->updated_at = $created_at;
$bookmark->save();
2024-05-30 23:01:16 +00:00
$bookmark->syncTagsFromString($bookmark_json['tags']);
2024-05-26 21:53:09 +00:00
}
}
}