2019-01-14 04:22:40 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\MetadataResolver;
|
|
|
|
|
2019-02-17 02:58:36 +09:00
|
|
|
use GuzzleHttp\Client;
|
|
|
|
|
2019-01-14 04:22:40 +09:00
|
|
|
class DLsiteResolver 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-01-14 04:22:40 +09:00
|
|
|
public function resolve(string $url): Metadata
|
|
|
|
{
|
2019-02-17 02:58:36 +09:00
|
|
|
$res = $this->client->get($url);
|
2019-01-14 04:22:40 +09:00
|
|
|
if ($res->getStatusCode() === 200) {
|
2019-02-17 02:58:36 +09:00
|
|
|
$metadata = $this->ogpResolver->parse($res->getBody());
|
|
|
|
$metadata->image = str_replace('img_sam.jpg', 'img_main.jpg', $metadata->image);
|
2019-01-15 00:05:01 +09:00
|
|
|
|
2019-01-14 04:22:40 +09:00
|
|
|
return $metadata;
|
|
|
|
} else {
|
|
|
|
throw new \RuntimeException("{$res->getStatusCode()}: $url");
|
|
|
|
}
|
|
|
|
}
|
2019-01-15 00:05:01 +09:00
|
|
|
}
|