2019-03-12 03:30:38 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\MetadataResolver;
|
|
|
|
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
|
|
|
|
class FC2ContentsResolver implements Resolver
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var Client
|
|
|
|
*/
|
|
|
|
private $client;
|
|
|
|
/**
|
|
|
|
* @var OGPResolver
|
|
|
|
*/
|
|
|
|
private $ogpResolver;
|
|
|
|
|
|
|
|
public function __construct(Client $client, OGPResolver $ogpResolver)
|
|
|
|
{
|
|
|
|
$this->client = $client;
|
|
|
|
$this->ogpResolver = $ogpResolver;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function resolve(string $url): Metadata
|
|
|
|
{
|
|
|
|
$res = $this->client->get($url);
|
2019-09-10 07:03:46 +09:00
|
|
|
$metadata = $this->ogpResolver->parse($res->getBody());
|
2019-03-12 03:30:38 +09:00
|
|
|
|
2019-09-10 07:03:46 +09:00
|
|
|
$dom = new \DOMDocument();
|
|
|
|
@$dom->loadHTML(mb_convert_encoding($res->getBody(), 'HTML-ENTITIES', 'UTF-8'));
|
|
|
|
$xpath = new \DOMXPath($dom);
|
2019-03-12 03:30:38 +09:00
|
|
|
|
2019-09-10 07:03:46 +09:00
|
|
|
$thumbnailNode = $xpath->query('//*[@class="main_thum_img"]/a')->item(0);
|
|
|
|
if ($thumbnailNode) {
|
|
|
|
$metadata->image = preg_replace('~^http:~', 'https:', $thumbnailNode->getAttribute('href'));
|
2019-03-12 03:30:38 +09:00
|
|
|
}
|
2019-09-10 07:03:46 +09:00
|
|
|
|
|
|
|
return $metadata;
|
2019-03-12 03:30:38 +09:00
|
|
|
}
|
|
|
|
}
|