From e966995deafd837f07316b764a8e1c04e2c1fd24 Mon Sep 17 00:00:00 2001 From: shibafu Date: Sun, 3 Mar 2019 00:29:43 +0900 Subject: [PATCH] fix style --- app/Http/Controllers/Api/CardController.php | 1 - app/Http/Middleware/TrustProxies.php | 2 +- app/MetadataResolver/ActivityPubResolver.php | 7 +++++-- app/MetadataResolver/FantiaResolver.php | 1 + app/MetadataResolver/MetadataResolver.php | 1 + app/MetadataResolver/Parser.php | 2 +- app/MetadataResolver/ToranoanaResolver.php | 1 - .../2019_02_06_235832_add_bio_and_url_to_users.php | 4 ++-- tests/Unit/MetadataResolver/CreateMockedResolver.php | 1 + 9 files changed, 12 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/Api/CardController.php b/app/Http/Controllers/Api/CardController.php index f8de0b3..87eb52d 100644 --- a/app/Http/Controllers/Api/CardController.php +++ b/app/Http/Controllers/Api/CardController.php @@ -2,7 +2,6 @@ namespace App\Http\Controllers\Api; - use App\Metadata; use App\MetadataResolver\MetadataResolver; use App\Utilities\Formatter; diff --git a/app/Http/Middleware/TrustProxies.php b/app/Http/Middleware/TrustProxies.php index 39d1770..9e84a36 100644 --- a/app/Http/Middleware/TrustProxies.php +++ b/app/Http/Middleware/TrustProxies.php @@ -2,8 +2,8 @@ namespace App\Http\Middleware; -use Illuminate\Http\Request; use Fideloper\Proxy\TrustProxies as Middleware; +use Illuminate\Http\Request; class TrustProxies extends Middleware { diff --git a/app/MetadataResolver/ActivityPubResolver.php b/app/MetadataResolver/ActivityPubResolver.php index 981cf9d..06328cb 100644 --- a/app/MetadataResolver/ActivityPubResolver.php +++ b/app/MetadataResolver/ActivityPubResolver.php @@ -2,9 +2,9 @@ namespace App\MetadataResolver; -use Psr\Http\Message\ResponseInterface; use GuzzleHttp\Exception\TransferException; use Illuminate\Support\Facades\Log; +use Psr\Http\Message\ResponseInterface; class ActivityPubResolver implements Resolver, Parser { @@ -40,7 +40,7 @@ class ActivityPubResolver implements Resolver, Parser $metadata = new Metadata(); $metadata->title = isset($object['attributedTo']) ? $this->getTitleFromActor($object['attributedTo']) : ''; - $metadata->description .= isset($object['summary']) ? $object['summary'] . " | " : ''; + $metadata->description .= isset($object['summary']) ? $object['summary'] . ' | ' : ''; $metadata->description .= isset($object['content']) ? $this->html2text($object['content']) : ''; $metadata->image = $object['attachment'][0]['url'] ?? ''; @@ -53,6 +53,7 @@ class ActivityPubResolver implements Resolver, Parser $res = $this->activityClient->get($url); if ($res->getStatusCode() !== 200) { Log::info(self::class . ': Actorの取得に失敗 URL=' . $url); + return ''; } @@ -65,6 +66,7 @@ class ActivityPubResolver implements Resolver, Parser return $title; } catch (TransferException $e) { Log::info(self::class . ': Actorの取得に失敗 URL=' . $url); + return ''; } } @@ -75,6 +77,7 @@ class ActivityPubResolver implements Resolver, Parser $html = preg_replace('~|

\s*]*>~i', "\n", $html); $dom = new \DOMDocument(); $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); + return $dom->textContent; } } diff --git a/app/MetadataResolver/FantiaResolver.php b/app/MetadataResolver/FantiaResolver.php index 63af366..4dec4c1 100644 --- a/app/MetadataResolver/FantiaResolver.php +++ b/app/MetadataResolver/FantiaResolver.php @@ -21,6 +21,7 @@ class FantiaResolver implements Resolver $this->client = $client; $this->ogpResolver = $ogpResolver; } + public function resolve(string $url): Metadata { preg_match("~\d+~", $url, $match); diff --git a/app/MetadataResolver/MetadataResolver.php b/app/MetadataResolver/MetadataResolver.php index ace71c2..5bf195f 100644 --- a/app/MetadataResolver/MetadataResolver.php +++ b/app/MetadataResolver/MetadataResolver.php @@ -53,6 +53,7 @@ class MetadataResolver implements Resolver if (isset($this->defaultResolver)) { /** @var Resolver $resolver */ $resolver = app($this->defaultResolver); + return $resolver->resolve($url); } diff --git a/app/MetadataResolver/Parser.php b/app/MetadataResolver/Parser.php index f9effde..75cccb3 100644 --- a/app/MetadataResolver/Parser.php +++ b/app/MetadataResolver/Parser.php @@ -5,4 +5,4 @@ namespace App\MetadataResolver; interface Parser { public function parse(string $body): Metadata; -} \ No newline at end of file +} diff --git a/app/MetadataResolver/ToranoanaResolver.php b/app/MetadataResolver/ToranoanaResolver.php index 27b900e..34559fe 100644 --- a/app/MetadataResolver/ToranoanaResolver.php +++ b/app/MetadataResolver/ToranoanaResolver.php @@ -28,7 +28,6 @@ class ToranoanaResolver implements Resolver $res = $this->client->get($url, ['cookies' => $cookieJar]); if ($res->getStatusCode() === 200) { - return $this->ogpResolver->parse($res->getBody()); } else { throw new \RuntimeException("{$res->getStatusCode()}: $url"); diff --git a/database/migrations/2019_02_06_235832_add_bio_and_url_to_users.php b/database/migrations/2019_02_06_235832_add_bio_and_url_to_users.php index d8988d4..251a379 100644 --- a/database/migrations/2019_02_06_235832_add_bio_and_url_to_users.php +++ b/database/migrations/2019_02_06_235832_add_bio_and_url_to_users.php @@ -1,8 +1,8 @@ handler = new MockHandler([$mockResponse]); $client = new Client(['handler' => $this->handler]); $this->resolver = app()->make($resolverClass, ['client' => $client]); + return $this->resolver; }