webmentions/src/Endpoint.php

54 lines
1.6 KiB
PHP
Raw Normal View History

2023-03-09 09:48:08 +00:00
<?php declare(strict_types=1);
namespace Lewisdale\Webmentions;
use Lewisdale\Webmentions\Exceptions\InvalidTargetException;
use Lewisdale\Webmentions\Exceptions\InvalidUrlException;
2023-03-09 09:48:08 +00:00
use Lewisdale\Webmentions\Gateways\WebmentionGatewayInterface;
use Lewisdale\Webmentions\Models\Webmention;
use Symfony\Component\HttpClient\HttpClient;
2023-03-09 09:48:08 +00:00
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;
}
2023-03-09 09:48:08 +00:00
}