Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Wizard Repository SessionWizardRepository (closes #23) #24

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions src/Repository/SessionWizardRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php declare(strict_types=1);

namespace Arcanist\Repository;

use Arcanist\TTL;
use Illuminate\Support\Str;
use Arcanist\AbstractWizard;
use Illuminate\Support\Facades\Session;
use Arcanist\Contracts\WizardRepository;
use Arcanist\Exception\WizardNotFoundException;

class SessionWizardRepository implements WizardRepository
{
private string $keyPrefix = 'arcanist.';

public function __construct(private TTL $ttl)
{
}

public function saveData(AbstractWizard $wizard, array $data): void
{
if (!$wizard->exists()) {

$sessionLen = (is_countable(Session::get($this->getSessionPath($wizard)))) ? count(Session::get($this->getSessionPath($wizard))) : 0;
$wizard->setId($sessionLen + 1);

$this->store($wizard, $data);

return;
}

$sessionKey = $this->buildSessionKey($wizard);

if (!Session::has($sessionKey)) {
throw new WizardNotFoundException();
}

$this->store($wizard, array_merge(Session::get($sessionKey, []), $data));
}

public function deleteWizard(AbstractWizard $wizard): void
{
$sessionKey = $this->buildSessionKey($wizard);

if (!Session::has($sessionKey)) {
return;
}

Session::forget($sessionKey);
$wizard->setId(null);
}

public function loadData(AbstractWizard $wizard): array
{
return $this->loadWizard($wizard);
}

private function loadWizard(AbstractWizard $wizard): array
{
$sessionKey = $this->buildSessionKey($wizard);

if (!Session::has($sessionKey)) {
throw new WizardNotFoundException();
}

return Session::get($sessionKey);
}

private function getSessionPath(AbstractWizard $wizard): string
{
return $this->keyPrefix . $wizard::class;
}

private function buildSessionKey(AbstractWizard $wizard): string
{
return $this->getSessionPath($wizard) . '.' . $wizard->getId();
}

private function store(AbstractWizard $wizard, array $data): void
{
Session::put(
$this->buildSessionKey($wizard),
$data
);
}
}