Something went wrong. Try again.
A barebones implementation of an atproto PDS in PHP and Slim Framework 4.
pds php atproto
Something went wrong. Try again.
2.6 kB · 88 lines
PHP
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889<?php
declare(strict_types=1);
namespace App\Infrastructure\Persistence\OAuth;
use App\Domain\OAuth\AccountDevice;use App\Domain\OAuth\AccountDeviceNotFoundException;use App\Domain\OAuth\AccountDeviceRepository;use App\Infrastructure\Database\Database;use App\Infrastructure\Database\Row;use DateTimeImmutable;
class SqliteAccountDeviceRepository implements AccountDeviceRepository{ public function __construct(private readonly Database $db) { }
public function findByDidAndDeviceId(string $did, string $deviceId): AccountDevice { $row = $this->db->fetchOne( 'SELECT * FROM account_device WHERE did = ? AND device_id = ?', [$did, $deviceId] );
if ($row === null) { throw new AccountDeviceNotFoundException(); }
return $this->hydrate($row); }
/** * @return AccountDevice[] */ public function findAllForDid(string $did): array { $rows = $this->db->fetchAll( 'SELECT * FROM account_device WHERE did = ? ORDER BY device_id', [$did] );
$result = []; foreach ($rows as $row) { $result[] = $this->hydrate($row); } return $result; }
public function save(AccountDevice $accountDevice): void { $this->db->execute( 'INSERT INTO account_device (did, device_id, created_at, updated_at) VALUES (:did, :device_id, :created_at, :updated_at) ON CONFLICT(did, device_id) DO UPDATE SET created_at = excluded.created_at, updated_at = excluded.updated_at', [ 'did' => $accountDevice->getDid(), 'device_id' => $accountDevice->getDeviceId(), 'created_at' => $accountDevice->getCreatedAt()->format(DATE_ATOM), 'updated_at' => $accountDevice->getUpdatedAt()->format(DATE_ATOM), ] ); }
public function deleteByDidAndDeviceId(string $did, string $deviceId): void { $this->db->execute( 'DELETE FROM account_device WHERE did = ? AND device_id = ?', [$did, $deviceId] ); }
/** * @param array<string, mixed> $row */ private function hydrate(array $row): AccountDevice { return new AccountDevice( did: Row::str($row, 'did'), deviceId: Row::str($row, 'device_id'), createdAt: new DateTimeImmutable(Row::str($row, 'created_at')), updatedAt: new DateTimeImmutable(Row::str($row, 'updated_at')), ); }}