2019-02-03 20:35:59 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\MetadataResolver;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2019-02-17 02:58:36 +09:00
|
|
|
use GuzzleHttp\Client;
|
2019-02-03 20:35:59 +09:00
|
|
|
|
|
|
|
class PatreonResolver implements Resolver
|
|
|
|
{
|
2019-02-17 02:58:36 +09:00
|
|
|
/**
|
|
|
|
* @var Client
|
|
|
|
*/
|
|
|
|
private $client;
|
|
|
|
/**
|
|
|
|
* @var OGPResolver
|
|
|
|
*/
|
|
|
|
private $ogpResolver;
|
|
|
|
|
|
|
|
public function __construct(Client $client, OGPResolver $ogpResolver)
|
|
|
|
{
|
|
|
|
$this->client = $client;
|
|
|
|
$this->ogpResolver = $ogpResolver;
|
|
|
|
}
|
|
|
|
|
2019-02-03 20:35:59 +09:00
|
|
|
public function resolve(string $url): Metadata
|
|
|
|
{
|
2019-02-17 02:58:36 +09:00
|
|
|
$res = $this->client->get($url);
|
2019-02-03 20:35:59 +09:00
|
|
|
if ($res->getStatusCode() === 200) {
|
2019-02-17 02:58:36 +09:00
|
|
|
$metadata = $this->ogpResolver->parse($res->getBody());
|
2019-02-03 20:35:59 +09:00
|
|
|
|
2019-03-08 03:41:04 +09:00
|
|
|
parse_str(parse_url($metadata->image, PHP_URL_QUERY), $query);
|
|
|
|
if(isset($query['token-time'])){
|
|
|
|
$expires_at_unixtime = $query['token-time'];
|
|
|
|
$metadata->expires_at = Carbon::createFromTimestamp($expires_at_unixtime);
|
|
|
|
}
|
2019-02-03 20:35:59 +09:00
|
|
|
|
|
|
|
return $metadata;
|
|
|
|
} else {
|
|
|
|
throw new \RuntimeException("{$res->getStatusCode()}: $url");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|