From adeb7a1b515212676a2ae09eb9087c35fb261977 Mon Sep 17 00:00:00 2001 From: Lewis Dale Date: Thu, 9 Mar 2023 09:48:08 +0000 Subject: [PATCH] Start adding SQLite database access --- src/Endpoint.php | 9 +++ src/Gateways/SqliteGateway.php | 85 +++++++++++++++++++++ src/Gateways/WebmentionGatewayInterface.php | 15 ++++ src/Models/Webmention.php | 19 +++++ src/Webmention.php | 1 + tests/Gateways/SqliteGatewayTest.php | 47 ++++++++++++ 6 files changed, 176 insertions(+) create mode 100644 src/Endpoint.php create mode 100644 src/Gateways/SqliteGateway.php create mode 100644 src/Gateways/WebmentionGatewayInterface.php create mode 100644 src/Models/Webmention.php create mode 100644 tests/Gateways/SqliteGatewayTest.php diff --git a/src/Endpoint.php b/src/Endpoint.php new file mode 100644 index 0000000..0686690 --- /dev/null +++ b/src/Endpoint.php @@ -0,0 +1,9 @@ +connection = new PDO("sqlite:$name"); + $this->up(); + } + + protected function up() : void + { + // Create Webmention table + $sql = <<connection->exec($sql); + } + + public function get(int $id): ?Webmention + { + $sql = "SELECT * FROM webmentions;"; + $statement = $this->connection->query($sql); + + if ($statement == false) { + return null; + } + + $row = $statement->fetch(PDO::FETCH_ASSOC); + + if ($row) { + return new Webmention( + $row["id"], + $row["target"], + $row["source"], + $row["content"], + $row["author"] + ); + } + + return null; + } + + public function getByPost(string $post): array + { + return []; + } + + public function save(Webmention $webmention): ?int + { + $sql = <<< SQL + INSERT INTO webmentions + (id, target, source, content, author) + VALUES + (:id, :target, :source, :content, :author); + SQL; + + $statement = $this->connection->prepare($sql); + $success = $statement->execute((array) $webmention); + $statement->closeCursor(); + + return $success ? (int) $this->connection->lastInsertId() : null; + } + + public function delete(Webmention $webmention): void + { + throw new Exception("Method " . SqliteGateway::class . "::delete not implemented"); + + } +} +?> \ No newline at end of file diff --git a/src/Gateways/WebmentionGatewayInterface.php b/src/Gateways/WebmentionGatewayInterface.php new file mode 100644 index 0000000..9d996a5 --- /dev/null +++ b/src/Gateways/WebmentionGatewayInterface.php @@ -0,0 +1,15 @@ + \ No newline at end of file diff --git a/src/Models/Webmention.php b/src/Models/Webmention.php new file mode 100644 index 0000000..7e49dc0 --- /dev/null +++ b/src/Models/Webmention.php @@ -0,0 +1,19 @@ +id}, target: {$this->target}, source: {$this->source}, content: {$this->content}, author: {$this->author})"; + } +} +?> diff --git a/src/Webmention.php b/src/Webmention.php index 078550a..9372fc3 100644 --- a/src/Webmention.php +++ b/src/Webmention.php @@ -2,6 +2,7 @@ namespace Lewisdale\Webmentions; +use Lewisdale\Webmentions\Gateways\WebmentionGatewayInterface; use Symfony\Component\DomCrawler\Crawler; use Symfony\Component\HttpClient\HttpClient; use Symfony\Contracts\HttpClient\HttpClientInterface; diff --git a/tests/Gateways/SqliteGatewayTest.php b/tests/Gateways/SqliteGatewayTest.php new file mode 100644 index 0000000..181685f --- /dev/null +++ b/tests/Gateways/SqliteGatewayTest.php @@ -0,0 +1,47 @@ +id = $gateway->save($webmention); + $this->assertNotNull($webmention->id); + } + + public function testCanRetrieveAWebmention() + { + $gateway = new SqliteGateway(":memory:"); + + $webmention = new Webmention( + null, + "https://lewisdale.dev/post/a-post", + "https://a-source.url", + "No content", + "Some Author Name" + ); + + $webmention->id = $gateway->save($webmention); + + $retrieved = $gateway->get($webmention->id); + + $this->assertEquals($webmention, $retrieved, ((string) $webmention). " " . ((string) $retrieved)); + } +}