diff --git a/db/factories/SessionFactory.php b/db/factories/SessionFactory.php new file mode 100644 index 00000000..08768512 --- /dev/null +++ b/db/factories/SessionFactory.php @@ -0,0 +1,22 @@ + $this->faker->lexify('??????????'), + 'payload' => $this->faker->text(100), + ]; + } +} diff --git a/src/Http/SessionHandlers/DatabaseHandler.php b/src/Http/SessionHandlers/DatabaseHandler.php index 277765c9..1a7525d5 100644 --- a/src/Http/SessionHandlers/DatabaseHandler.php +++ b/src/Http/SessionHandlers/DatabaseHandler.php @@ -6,7 +6,7 @@ namespace Engelsystem\Http\SessionHandlers; use Engelsystem\Database\Database; use Engelsystem\Helpers\Carbon; -use Illuminate\Database\Query\Builder as QueryBuilder; +use Engelsystem\Models\Session; class DatabaseHandler extends AbstractHandler { @@ -19,9 +19,7 @@ class DatabaseHandler extends AbstractHandler */ public function read(string $id): string { - $session = $this->getQuery() - ->where('id', '=', $id) - ->first(); + $session = Session::whereId($id)->first(); return $session ? $session->payload : ''; } @@ -31,27 +29,13 @@ class DatabaseHandler extends AbstractHandler */ public function write(string $id, string $data): bool { - $values = [ - 'payload' => $data, - 'last_activity' => Carbon::now(), - ]; + $session = Session::findOrNew($id); + $session->id = $id; + $session->payload = $data; + $session->last_activity = Carbon::now(); + $session->save(); - $session = $this->getQuery() - ->where('id', '=', $id) - ->first(); - - if (!$session) { - return $this->getQuery() - ->insert($values + [ - 'id' => $id, - ]); - } - - $this->getQuery() - ->where('id', '=', $id) - ->update($values); - - // The update return can't be used directly because it won't change if the second call is in the same second + // The save return can't be used directly as it won't change if the second call is in the same second return true; } @@ -60,9 +44,7 @@ class DatabaseHandler extends AbstractHandler */ public function destroy(string $id): bool { - $this->getQuery() - ->where('id', '=', $id) - ->delete(); + Session::whereId($id)->delete(); return true; } @@ -75,15 +57,7 @@ class DatabaseHandler extends AbstractHandler $sessionDays = config('session')['lifetime']; $deleteBefore = Carbon::now()->subDays($sessionDays); - return $this->getQuery() - ->where('last_activity', '<', $deleteBefore) + return Session::where('last_activity', '<', $deleteBefore) ->delete(); } - - protected function getQuery(): QueryBuilder - { - return $this->database - ->getConnection() - ->table('sessions'); - } } diff --git a/src/Models/Session.php b/src/Models/Session.php new file mode 100644 index 00000000..04a224f0 --- /dev/null +++ b/src/Models/Session.php @@ -0,0 +1,43 @@ + default attributes */ + protected $attributes = [ // phpcs:ignore + 'payload' => '', + ]; + + /** @var array */ + protected $dates = [ // phpcs:ignore + 'last_activity', + ]; + + /** @var array */ + protected $fillable = [ // phpcs:ignore + 'id', + 'payload', + ]; +} diff --git a/tests/Unit/Http/SessionHandlers/DatabaseHandlerTest.php b/tests/Unit/Http/SessionHandlers/DatabaseHandlerTest.php index 13af667d..9f6ed6b8 100644 --- a/tests/Unit/Http/SessionHandlers/DatabaseHandlerTest.php +++ b/tests/Unit/Http/SessionHandlers/DatabaseHandlerTest.php @@ -16,7 +16,6 @@ class DatabaseHandlerTest extends TestCase /** * @covers \Engelsystem\Http\SessionHandlers\DatabaseHandler::__construct - * @covers \Engelsystem\Http\SessionHandlers\DatabaseHandler::getQuery * @covers \Engelsystem\Http\SessionHandlers\DatabaseHandler::read */ public function testRead(): void @@ -42,9 +41,7 @@ class DatabaseHandlerTest extends TestCase foreach (['Lorem Ipsum', 'Dolor Sit!'] as $data) { $this->assertTrue($handler->write('id-foo', $data)); - $return = $this->database->getConnection()->table('sessions') - ->where('id', 'id-foo') - ->get(); + $return = Session::whereId('id-foo')->get(); $this->assertCount(1, $return); $return = $return->first(); diff --git a/tests/Unit/Models/SessionTest.php b/tests/Unit/Models/SessionTest.php new file mode 100644 index 00000000..d965d331 --- /dev/null +++ b/tests/Unit/Models/SessionTest.php @@ -0,0 +1,36 @@ + 'foo', + 'payload' => 'lorem ipsum', + 'last_activity' => Carbon::now(), + ]); + Session::create([ + 'id' => 'bar', + 'last_activity' => Carbon::now(), + ]); + + $session = Session::find('foo'); + $this->assertNotNull($session); + $this->assertEquals('lorem ipsum', $session->payload); + } +}