2020-05-10 18:50:03 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\Metadata;
|
2020-07-23 13:08:20 +09:00
|
|
|
use App\MetadataResolver\DeniedHostException;
|
2020-05-10 18:50:03 +09:00
|
|
|
use App\MetadataResolver\MetadataResolver;
|
|
|
|
use App\Tag;
|
|
|
|
use App\Utilities\Formatter;
|
2020-08-23 12:28:21 +09:00
|
|
|
use GuzzleHttp\Exception\TransferException;
|
2020-08-01 17:26:38 +09:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2020-08-11 00:17:10 +09:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2020-05-10 18:50:03 +09:00
|
|
|
|
|
|
|
class MetadataResolveService
|
|
|
|
{
|
|
|
|
/** @var MetadataResolver */
|
|
|
|
private $resolver;
|
|
|
|
/** @var Formatter */
|
|
|
|
private $formatter;
|
|
|
|
|
|
|
|
public function __construct(MetadataResolver $resolver, Formatter $formatter)
|
|
|
|
{
|
|
|
|
$this->resolver = $resolver;
|
|
|
|
$this->formatter = $formatter;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute(string $url): Metadata
|
|
|
|
{
|
|
|
|
// URLの正規化
|
|
|
|
$url = $this->formatter->normalizeUrl($url);
|
|
|
|
|
2020-07-21 23:39:48 +09:00
|
|
|
// 自分自身は解決しない
|
|
|
|
if (parse_url($url, PHP_URL_HOST) === parse_url(config('app.url'), PHP_URL_HOST)) {
|
2020-07-23 13:08:20 +09:00
|
|
|
throw new DeniedHostException($url);
|
2020-07-21 23:39:48 +09:00
|
|
|
}
|
|
|
|
|
2020-08-23 12:28:21 +09:00
|
|
|
return DB::transaction(function () use ($url) {
|
|
|
|
// 無かったら取得
|
|
|
|
// TODO: ある程度古かったら再取得とかありだと思う
|
|
|
|
$metadata = Metadata::find($url);
|
|
|
|
if ($metadata == null || ($metadata->expires_at !== null && $metadata->expires_at < now())) {
|
|
|
|
try {
|
|
|
|
$resolved = $this->resolver->resolve($url);
|
|
|
|
$metadata = Metadata::updateOrCreate(['url' => $url], [
|
|
|
|
'title' => $resolved->title,
|
|
|
|
'description' => $resolved->description,
|
|
|
|
'image' => $resolved->image,
|
|
|
|
'expires_at' => $resolved->expires_at
|
|
|
|
]);
|
|
|
|
|
|
|
|
$tagIds = [];
|
|
|
|
foreach ($resolved->normalizedTags() as $tagName) {
|
|
|
|
$tag = Tag::firstOrCreate(['name' => $tagName]);
|
|
|
|
$tagIds[] = $tag->id;
|
2020-08-11 00:17:10 +09:00
|
|
|
}
|
2020-08-23 12:28:21 +09:00
|
|
|
$metadata->tags()->sync($tagIds);
|
|
|
|
} catch (TransferException $e) {
|
|
|
|
// 何らかの通信エラーによってメタデータの取得に失敗した時、とりあえずエラーログにURLを残す
|
|
|
|
Log::error(self::class . ': メタデータの取得に失敗 URL=' . $url);
|
|
|
|
throw $e;
|
2020-05-10 18:50:03 +09:00
|
|
|
}
|
2020-08-11 00:17:10 +09:00
|
|
|
}
|
2020-08-10 13:32:47 +09:00
|
|
|
|
2020-08-01 17:26:38 +09:00
|
|
|
return $metadata;
|
2020-08-23 12:28:21 +09:00
|
|
|
});
|
2020-05-10 18:50:03 +09:00
|
|
|
}
|
|
|
|
}
|