webmentions/src/Endpoint.php

54 lines
1.6 KiB
PHP

<?php declare(strict_types=1);
namespace Lewisdale\Webmentions;
use Lewisdale\Webmentions\Exceptions\InvalidTargetException;
use Lewisdale\Webmentions\Exceptions\InvalidUrlException;
use Lewisdale\Webmentions\Gateways\WebmentionGatewayInterface;
use Lewisdale\Webmentions\Models\Webmention;
use Symfony\Component\HttpClient\HttpClient;
class Endpoint {
private readonly WebmentionGatewayInterface $gateway;
public function validateUrl(string $url) : bool {
return !!filter_var($url, FILTER_VALIDATE_URL);
}
public function receiveWebmention(string $source, string $target) : void
{
// Validate that both source and target are actual domains
if (!$this->validateUrl($source) || !$this->validateUrl($target))
{
throw new InvalidUrlException();
}
// Validate that the webmention target is a domain I care about
if (!str_contains($target, "https://lewisdale.dev")) {
throw new InvalidTargetException();
}
// Parse content from the source
$client = HttpClient::create();
$response = $client->request('GET', $source);
if ($response->getStatusCode() === 200)
{
$content = $this->parseContent($response->getContent());
$author = $this->parseAuthor($response->getContent());
$webmention = new Webmention(null, $target, $source, $content, $author);
$this->gateway->save($webmention);
}
}
private function parseContent(string $content) : ?string
{
return null;
}
private function parseAuthor(string $author) : ?string
{
return null;
}
}