tissue/app/MetadataResolver/CienResolver.php

40 lines
1016 B
PHP
Raw Normal View History

2019-02-16 00:06:51 +09:00
<?php
namespace App\MetadataResolver;
use Carbon\Carbon;
use GuzzleHttp\Client;
2019-02-16 00:06:51 +09:00
class CienResolver extends MetadataResolver
{
/**
* @var Client
*/
private $client;
/**
* @var OGPResolver
*/
private $ogpResolver;
public function __construct(Client $client, OGPResolver $ogpResolver)
{
$this->client = $client;
$this->ogpResolver = $ogpResolver;
}
2019-02-16 00:06:51 +09:00
public function resolve(string $url): Metadata
{
$res = $this->client->get($url);
2019-09-12 05:26:03 +09:00
$metadata = $this->ogpResolver->parse((string) $res->getBody());
2019-02-16 00:06:51 +09:00
// 画像URLから有効期限の起点を拾う
parse_str(parse_url($metadata->image, PHP_URL_QUERY), $params);
if (empty($params['px-time'])) {
throw new \RuntimeException('Parameter "px-time" not found. Image=' . $metadata->image . ' Source=' . $url);
2019-02-16 00:06:51 +09:00
}
$metadata->expires_at = Carbon::createFromTimestamp($params['px-time'])->addHour(1);
return $metadata;
2019-02-16 00:06:51 +09:00
}
}