jonnybarnes.uk/app/Services/BookmarkService.php
Jonny Barnes e08763c526
Migrate outbound HTTP calls from Guzzle to the Http facade
Replaces every raw GuzzleHttp\Client usage (Nominatim, web.archive.org,
webmention fetch/discover/send, Bridgy syndication, IndieAuth client_id
lookup, contact avatar/h-card fetch, profile image download, and the
CloudConvert screenshot pipeline) with Illuminate\Support\Facades\Http,
and enables Http::preventStrayRequests() globally in tests so any
un-faked outbound call now fails loudly instead of silently hitting the
network.

The CloudConvert retry-until-finished middleware in AppServiceProvider
is replaced by a plain polling loop in SaveScreenshot, which also fixes
a latent bug where the old middleware decoded a response object instead
of its body. Bridgy syndication jobs keep their tries=1/no-retry
semantics unchanged to avoid duplicate publishes. Guzzle's PSR-7 helpers
(Header, UriResolver, Utils) stay in SendWebMentions since Http has no
equivalent for them.

Every affected test file's Guzzle MockHandler/HandlerStack boilerplate
is replaced with Http::fake()/Http::sequence().

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01P8j7cJhCiYDsGUgB7obKNQ
2026-07-26 09:12:01 +01:00

74 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services;
use App\Exceptions\InternetArchiveException;
use App\Jobs\ProcessBookmark;
use App\Models\Bookmark;
use App\Models\Tag;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
class BookmarkService
{
/**
* Create a new Bookmark.
*/
public function create(array $data): Bookmark
{
if (Arr::get($data, 'properties.bookmark-of.0')) {
// micropub request
$url = normalize_url(Arr::get($data, 'properties.bookmark-of.0'));
$name = Arr::get($data, 'properties.name.0');
$content = Arr::get($data, 'properties.content.0');
$categories = Arr::get($data, 'properties.category');
}
if (Arr::get($data, 'bookmark-of')) {
$url = normalize_url(Arr::get($data, 'bookmark-of'));
$name = Arr::get($data, 'name');
$content = Arr::get($data, 'content');
$categories = Arr::get($data, 'category');
}
$bookmark = Bookmark::create([
'url' => $url,
'name' => $name,
'content' => $content,
]);
foreach ((array) $categories as $category) {
$tag = Tag::firstOrCreate(['tag' => $category]);
$bookmark->tags()->save($tag);
}
ProcessBookmark::dispatch($bookmark);
return $bookmark;
}
/**
* Given a URL, attempt to save it to the Internet Archive.
*
* @throws InternetArchiveException
*/
public function getArchiveLink(string $url): string
{
$response = Http::get('https://web.archive.org/save/'.$url);
if ($response->clientError()) {
// throw an exception to be caught
throw new InternetArchiveException;
}
$contentLocation = $response->header('Content-Location');
if ($contentLocation !== '' && Str::startsWith($contentLocation, '/web')) {
return $contentLocation;
}
// throw an exception to be caught
throw new InternetArchiveException;
}
}