Start adding SQLite database access
This commit is contained in:
		
							parent
							
								
									df226b2897
								
							
						
					
					
						commit
						adeb7a1b51
					
				
							
								
								
									
										9
									
								
								src/Endpoint.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/Endpoint.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,9 @@
 | 
			
		||||
<?php declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace Lewisdale\Webmentions;
 | 
			
		||||
 | 
			
		||||
use Lewisdale\Webmentions\Gateways\WebmentionGatewayInterface;
 | 
			
		||||
 | 
			
		||||
class Endpoint {
 | 
			
		||||
    private readonly WebmentionGatewayInterface $gateway;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										85
									
								
								src/Gateways/SqliteGateway.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										85
									
								
								src/Gateways/SqliteGateway.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,85 @@
 | 
			
		||||
<?php declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace Lewisdale\Webmentions\Gateways;
 | 
			
		||||
 | 
			
		||||
use Exception;
 | 
			
		||||
use Lewisdale\Webmentions\Models\Webmention;
 | 
			
		||||
use PDO;
 | 
			
		||||
 | 
			
		||||
class SqliteGateway extends WebmentionGatewayInterface {
 | 
			
		||||
    private readonly PDO $connection;
 | 
			
		||||
 | 
			
		||||
    function __construct(string $name)
 | 
			
		||||
    {
 | 
			
		||||
        $this->connection = new PDO("sqlite:$name");
 | 
			
		||||
        $this->up();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function up() : void
 | 
			
		||||
    {
 | 
			
		||||
        // Create Webmention table
 | 
			
		||||
        $sql = <<<SQL
 | 
			
		||||
        CREATE TABLE IF NOT EXISTS webmentions (
 | 
			
		||||
            id INTEGER PRIMARY KEY,
 | 
			
		||||
            target TEXT NOT NULL,
 | 
			
		||||
            source TEXT NOT NULL,
 | 
			
		||||
            content TEXT,
 | 
			
		||||
            author TEXT
 | 
			
		||||
        );
 | 
			
		||||
        SQL;
 | 
			
		||||
 | 
			
		||||
        $this->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");
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
?>
 | 
			
		||||
							
								
								
									
										15
									
								
								src/Gateways/WebmentionGatewayInterface.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								src/Gateways/WebmentionGatewayInterface.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,15 @@
 | 
			
		||||
<?php declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace Lewisdale\Webmentions\Gateways;
 | 
			
		||||
 | 
			
		||||
use Lewisdale\Webmentions\Models\Webmention;
 | 
			
		||||
 | 
			
		||||
abstract class WebmentionGatewayInterface {
 | 
			
		||||
    abstract protected function up() : void;
 | 
			
		||||
    abstract public function getByPost(string $post) : array;
 | 
			
		||||
    abstract public function save(Webmention $webmention) : ?int;
 | 
			
		||||
    abstract public function delete(Webmention $webmention) : void;
 | 
			
		||||
    abstract public function get(int $id) : ?Webmention;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
							
								
								
									
										19
									
								
								src/Models/Webmention.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								src/Models/Webmention.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,19 @@
 | 
			
		||||
<?php declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace Lewisdale\Webmentions\Models;
 | 
			
		||||
 | 
			
		||||
class Webmention {
 | 
			
		||||
    function __construct(
 | 
			
		||||
        public ?int $id,
 | 
			
		||||
        public string $target, // The target post
 | 
			
		||||
        public string $source,
 | 
			
		||||
        public ?string $content,
 | 
			
		||||
        public ?string $author, // TODO: Should be reference to another model
 | 
			
		||||
    )
 | 
			
		||||
    {}
 | 
			
		||||
 | 
			
		||||
    public function __toString() {
 | 
			
		||||
        return "Webmention (id: {$this->id}, target: {$this->target}, source: {$this->source}, content: {$this->content}, author: {$this->author})";
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
?>
 | 
			
		||||
@ -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;
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										47
									
								
								tests/Gateways/SqliteGatewayTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								tests/Gateways/SqliteGatewayTest.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,47 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
error_reporting(E_ALL); ini_set('display_errors',1);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
use Lewisdale\Webmentions\Gateways\SqliteGateway;
 | 
			
		||||
use Lewisdale\Webmentions\Models\Webmention;
 | 
			
		||||
use PHPUnit\Framework\TestCase;
 | 
			
		||||
 | 
			
		||||
class SqliteGatewayTest extends TestCase
 | 
			
		||||
{
 | 
			
		||||
    public function testCanInsertAWebmention()
 | 
			
		||||
    {
 | 
			
		||||
        $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);
 | 
			
		||||
        $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));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user