Add Session model
This commit is contained in:
parent
ee7d30b339
commit
67d5950926
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Database\Factories\Engelsystem\Models;
|
||||
|
||||
use Engelsystem\Models\Session;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
class SessionFactory extends Factory
|
||||
{
|
||||
/** @var string */
|
||||
protected $model = Session::class; // phpcs:ignore
|
||||
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->faker->lexify('??????????'),
|
||||
'payload' => $this->faker->text(100),
|
||||
];
|
||||
}
|
||||
}
|
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Engelsystem\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
|
||||
/**
|
||||
* @property string $id
|
||||
* @property string $payload
|
||||
* @property Carbon $last_activity
|
||||
*
|
||||
* @method static Builder|Session whereId($value)
|
||||
* @method static Builder|Session wherePayload($value)
|
||||
* @method static Builder|Session whereLastActivity($value)
|
||||
*/
|
||||
class Session extends BaseModel
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public $incrementing = false; // phpcs:ignore
|
||||
|
||||
protected $keyType = 'string'; // phpcs:ignore
|
||||
|
||||
/** @var array<string, string|null> default attributes */
|
||||
protected $attributes = [ // phpcs:ignore
|
||||
'payload' => '',
|
||||
];
|
||||
|
||||
/** @var array<string> */
|
||||
protected $dates = [ // phpcs:ignore
|
||||
'last_activity',
|
||||
];
|
||||
|
||||
/** @var array<string> */
|
||||
protected $fillable = [ // phpcs:ignore
|
||||
'id',
|
||||
'payload',
|
||||
];
|
||||
}
|
|
@ -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();
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Engelsystem\Test\Unit\Models;
|
||||
|
||||
use Engelsystem\Helpers\Carbon;
|
||||
use Engelsystem\Models\Session;
|
||||
|
||||
/**
|
||||
* This class provides tests for the Session model
|
||||
*/
|
||||
class SessionTest extends ModelTest
|
||||
{
|
||||
/**
|
||||
* Tests that a Session can be created and loaded
|
||||
*
|
||||
* @covers \Engelsystem\Models\Session
|
||||
*/
|
||||
public function testCreate(): void
|
||||
{
|
||||
Session::create([
|
||||
'id' => '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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue