2018-06-13 01:00:24 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\MetadataResolver;
|
|
|
|
|
2019-02-17 02:58:36 +09:00
|
|
|
use GuzzleHttp\Client;
|
2018-06-13 01:00:24 +09:00
|
|
|
use GuzzleHttp\Cookie\CookieJar;
|
|
|
|
|
|
|
|
class ToranoanaResolver 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;
|
|
|
|
}
|
|
|
|
|
2018-06-13 01:00:24 +09:00
|
|
|
public function resolve(string $url): Metadata
|
|
|
|
{
|
2019-08-03 00:26:12 +09:00
|
|
|
$res = $this->client->get($url);
|
2019-09-10 07:03:46 +09:00
|
|
|
$metadata = $this->ogpResolver->parse($res->getBody());
|
2019-08-02 23:48:56 +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);
|
|
|
|
$imgNode = $xpath->query('//*[@id="preview"]//img')->item(0);
|
|
|
|
if ($imgNode !== null) {
|
|
|
|
$metadata->image = $imgNode->getAttribute('src');
|
2018-06-13 01:00:24 +09:00
|
|
|
}
|
2019-09-10 07:03:46 +09:00
|
|
|
|
|
|
|
return $metadata;
|
2018-06-13 01:00:24 +09:00
|
|
|
}
|
2019-01-15 00:05:01 +09:00
|
|
|
}
|