tissue/app/MetadataResolver/DeviantArtResolver.php

40 lines
941 B
PHP
Raw Normal View History

2019-01-19 16:19:34 +09:00
<?php
namespace App\MetadataResolver;
use GuzzleHttp\Client;
2019-01-19 16:19:34 +09:00
class DeviantArtResolver implements Resolver
{
/**
* @var Client
*/
private $client;
/**
* @var OGPResolver
*/
private $ogpResolver;
public function __construct(Client $client, OGPResolver $ogpResolver)
{
$this->client = $client;
$this->ogpResolver = $ogpResolver;
}
2019-01-19 16:19:34 +09:00
public function resolve(string $url): Metadata
{
$res = $this->client->get('https://backend.deviantart.com/oembed?url=' . $url);
2019-09-10 01:27:35 +09:00
$data = json_decode($res->getBody()->getContents(), true);
$metadata = new Metadata();
2019-01-19 16:19:34 +09:00
2019-09-10 01:27:35 +09:00
$metadata->title = $data['title'] ?? '';
$metadata->description = 'By ' . $data['author_name'];
$metadata->image = $data['url'];
if (isset($data['tags'])) {
$metadata->tags = explode(', ', $data['tags']);
}
2019-01-19 16:19:34 +09:00
2019-09-10 01:27:35 +09:00
return $metadata;
2019-01-19 16:19:34 +09:00
}
}