Handle sourcenotfoundexception and targetnotmentionedexception

This commit is contained in:
Lewis Dale 2023-03-16 10:10:40 +00:00
parent 51f1269600
commit fca08de828

View File

@ -5,37 +5,45 @@ require_once __DIR__ . "/vendor/autoload.php";
use Lewisdale\Webmentions\Endpoint;
use Lewisdale\Webmentions\Exceptions\InvalidTargetException;
use Lewisdale\Webmentions\Exceptions\InvalidUrlException;
use Lewisdale\Webmentions\Gateways\SqliteGateway;
use Lewisdale\Webmentions\Router\Request;
use Lewisdale\Webmentions\Router\Router;
use Lewisdale\Webmentions\Router\Response;
use Lewisdale\Webmentions\Router\Router;
use Lewisdale\Webmentions\Router\StatusCode;
use Lewisdale\Webmentions\Webmention;
use Symfony\Component\HttpClient\HttpClient;
$router = new Router();
$router->post("/send", function(Request $req, Response $response) {
$router->post("/send", function (Request $req, Response $response) {
$mentioner = new Webmention();
$source = $req->query["source"];
$mentioner->sendForPage($source);
$mentioner->sendForPage($source);
});
$router->post("/endpoint", function(Request $req, Response $response) {
$source = $req->post['source'];
$target = $req->post['target'];
$router->post("/endpoint", function (Request $req, Response $response) {
$source = $req->post['source'];
$target = $req->post['target'];
$endpoint = new Endpoint();
try {
$endpoint->receiveWebmention($source, $target);
} catch (InvalidUrlException $e) {
$response->status_code = StatusCode::BadRequest;
return "Source and target must be valid URLs";
} catch (InvalidTargetException $e) {
$response->status_code = StatusCode::BadRequest;
return "Target must be on the domain lewisdale.dev";
}
$endpoint = new Endpoint(HttpClient::create(), new SqliteGateway("webmentions.db"));
$response->status_code = StatusCode::Created;
try {
$endpoint->receiveWebmention($source, $target);
} catch (InvalidUrlException $e) {
$response->status_code = StatusCode::BadRequest;
return "Source and target must be valid URLs";
} catch (InvalidTargetException $e) {
$response->status_code = StatusCode::BadRequest;
return "Target must be on the domain lewisdale.dev";
} catch (\Lewisdale\Webmentions\Exceptions\SourceNotFoundException $e) {
$response->status_code = StatusCode::BadRequest;
return "Source URL was unreachable";
} catch (\Lewisdale\Webmentions\Exceptions\TargetNotMentionedException) {
$response->status_code = StatusCode::BadRequest;
return "Source does not mention the target";
}
$response->status_code = StatusCode::Created;
});
$router->get('/', fn($req, $res) => "<h1>Webmention server</h1>");