Skip to content

Commit

Permalink
Dev 3.0.0 (#5)
Browse files Browse the repository at this point in the history
Add Form Storage for getting and setting data depending on form session
Update session storage contract
Add infection tests
Update tests
  • Loading branch information
lexalium authored Jan 19, 2024
1 parent 9be1ce8 commit 7ef6077
Show file tree
Hide file tree
Showing 16 changed files with 152 additions and 154 deletions.
31 changes: 15 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,10 @@ composer require lexal/stepped-form
$builder = new CustomBuilder(new StepsBuilder(/* StepControlInterface */, /* DataControlInterface */));
```

3. Create a Storage and data controllers.
```php
use Lexal\SteppedForm\Form\DataControl;
use Lexal\SteppedForm\Form\StepControl;
use Lexal\SteppedForm\Form\Storage\DataStorage;

$storage = new InMemoryStorage(); // can use any other storage (session, database, redis, etc.)
$stepControl = new StepControl($storage);
$dataControl = new DataControl(new DataStorage($storage));
```

4. Create a Session storage to save current form session key and have ability to split one form into different sessions
depending on initial user input (e.g. customer id). Use default `SessionControl` when there is no need to split
3. Create a Session storage to save current form session key and have ability to split one form into different sessions
depending on initial user input (e.g. customer id). Use default `NullSessionStorage` when there is no need to split
form sessions or there is no dependency on initial user input.
```php
use Lexal\SteppedForm\Form\SessionControl;
use Lexal\SteppedForm\Form\Storage\SessionStorageInterface;

final class SessionStorage implements SessionStorageInterface
Expand All @@ -106,9 +94,21 @@ composer require lexal/stepped-form
}
}

$sessionControl = new SessionControl(new SessionStorage());
$sessionControl = new SessionStorage();
```

4. Create a Storage and data controllers.
```php
use Lexal\SteppedForm\Form\DataControl;
use Lexal\SteppedForm\Form\StepControl;
use Lexal\SteppedForm\Form\Storage\DataStorage;
use Lexal\SteppedForm\Form\Storage\FormStorage;

$storage = new FormStorage(new InMemoryStorage(), new InMemorySessionStorage()); // can use any other storage (session, database, redis, etc.)
$stepControl = new StepControl($storage);
$dataControl = new DataControl(new DataStorage($storage));
```

5. Create an Event Dispatcher.
```php
use Lexal\SteppedForm\EventDispatcher\EventDispatcherInterface;
Expand Down Expand Up @@ -138,7 +138,6 @@ composer require lexal/stepped-form
$builder,
$dispatcher,
new SimpleEntityCopy(),
$sessionControl, // default storage for session control is NullSessionStorage
);
```

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"infection": "infection -j4 --only-covered",
"tests": [
"@phpcs",
"phpstan",
"@phpstan",
"@phpunit",
"@infection"
]
Expand Down
27 changes: 0 additions & 27 deletions src/Form/SessionControl.php

This file was deleted.

24 changes: 0 additions & 24 deletions src/Form/SessionControlInterface.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Form/StepControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

use Lexal\SteppedForm\Exception\AlreadyStartedException;
use Lexal\SteppedForm\Exception\FormIsNotStartedException;
use Lexal\SteppedForm\Form\Storage\StorageInterface;
use Lexal\SteppedForm\Form\Storage\FormStorageInterface;
use Lexal\SteppedForm\Step\StepKey;

final class StepControl implements StepControlInterface
{
private const STORAGE_KEY = '__CURRENT_STEP__';

public function __construct(private readonly StorageInterface $storage)
public function __construct(private readonly FormStorageInterface $storage)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/Form/Storage/DataStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class DataStorage implements DataStorageInterface
{
private const STORAGE_KEY = '__STEPS__';

public function __construct(private readonly StorageInterface $storage)
public function __construct(private readonly FormStorageInterface $storage)
{
}

Expand Down
61 changes: 61 additions & 0 deletions src/Form/Storage/FormStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Lexal\SteppedForm\Form\Storage;

use Lexal\SteppedForm\Exception\ReadSessionKeyException;

final class FormStorage implements FormStorageInterface
{
private const STORAGE_KEY = '__CURRENT_SESSION_KEY__';

public function __construct(
private readonly StorageInterface $storage,
private readonly SessionStorageInterface $sessionStorage = new NullSessionStorage(),
) {
}

public function setCurrentSession(string $sessionKey): void
{
$this->sessionStorage->put(self::STORAGE_KEY, $sessionKey);
}

/**
* @inheritDoc
*
* @throws ReadSessionKeyException
*/
public function get(string $key, mixed $default = null): mixed
{
return $this->storage->get($key, $this->getCurrentSession(), $default);
}

/**
* @inheritDoc
*
* @throws ReadSessionKeyException
*/
public function put(string $key, mixed $data): void
{
$this->storage->put($key, $this->getCurrentSession(), $data);
}

/**
* @inheritDoc
*
* @throws ReadSessionKeyException
*/
public function clear(): void
{
$this->storage->clear($this->getCurrentSession());
}

/**
* @throws ReadSessionKeyException
*/
private function getCurrentSession(): string
{
return $this->sessionStorage->get(self::STORAGE_KEY) ?? self::DEFAULT_SESSION_KEY;
}
}
30 changes: 30 additions & 0 deletions src/Form/Storage/FormStorageInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Lexal\SteppedForm\Form\Storage;

interface FormStorageInterface
{
public const DEFAULT_SESSION_KEY = '__MAIN__';

/**
* Sets current session key for the form.
*/
public function setCurrentSession(string $sessionKey): void;

/**
* Returns data from the form storage by key.
*/
public function get(string $key, mixed $default = null): mixed;

/**
* Sets data to the form storage.
*/
public function put(string $key, mixed $data): void;

/**
* Removes all form data from the storage.
*/
public function clear(): void;
}
11 changes: 3 additions & 8 deletions src/Form/Storage/StorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,18 @@

interface StorageInterface
{
/**
* Checks if the storage contains data with the given key.
*/
public function has(string $key): bool;

/**
* Returns data from the storage by key.
*/
public function get(string $key, mixed $default = null): mixed;
public function get(string $key, string $sessionKey, mixed $default = null): mixed;

/**
* Sets data to the storage.
*/
public function put(string $key, mixed $data): void;
public function put(string $key, string $sessionKey, mixed $data): void;

/**
* Removes all form data from the storage.
*/
public function clear(): void;
public function clear(string $sessionKey): void;
}
11 changes: 4 additions & 7 deletions src/SteppedForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
use Lexal\SteppedForm\Exception\SteppedFormErrorsException;
use Lexal\SteppedForm\Form\Builder\FormBuilderInterface;
use Lexal\SteppedForm\Form\DataControlInterface;
use Lexal\SteppedForm\Form\SessionControl;
use Lexal\SteppedForm\Form\SessionControlInterface;
use Lexal\SteppedForm\Form\StepControlInterface;
use Lexal\SteppedForm\Form\Storage\StorageInterface;
use Lexal\SteppedForm\Form\Storage\FormStorageInterface;
use Lexal\SteppedForm\Step\RenderStepInterface;
use Lexal\SteppedForm\Step\Step;
use Lexal\SteppedForm\Step\StepKey;
Expand All @@ -34,11 +32,10 @@ final class SteppedForm implements SteppedFormInterface
public function __construct(
private readonly DataControlInterface $dataControl,
private readonly StepControlInterface $stepControl,
private readonly StorageInterface $storage,
private readonly FormStorageInterface $storage,
private readonly FormBuilderInterface $builder,
private readonly EventDispatcherInterface $dispatcher,
private readonly EntityCopyInterface $entityCopy,
private readonly SessionControlInterface $sessionControl = new SessionControl(),
) {
$this->steps = new Steps();
}
Expand All @@ -57,9 +54,9 @@ public function getEntity(): mixed
* @throws EntityNotFoundException
* @throws StepNotFoundException
*/
public function start(mixed $entity, string $sessionKey = SessionControlInterface::DEFAULT_SESSION_KEY): ?StepKey
public function start(mixed $entity, string $sessionKey = FormStorageInterface::DEFAULT_SESSION_KEY): ?StepKey
{
$this->sessionControl->setCurrent($sessionKey);
$this->storage->setCurrentSession($sessionKey);

$this->stepControl->throwIfAlreadyStarted();

Expand Down
4 changes: 2 additions & 2 deletions src/SteppedFormInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Lexal\SteppedForm\Exception\StepNotFoundException;
use Lexal\SteppedForm\Exception\StepNotRenderableException;
use Lexal\SteppedForm\Exception\SteppedFormErrorsException;
use Lexal\SteppedForm\Form\SessionControlInterface;
use Lexal\SteppedForm\Form\Storage\FormStorageInterface;
use Lexal\SteppedForm\Step\StepKey;
use Lexal\SteppedForm\Step\TemplateDefinition;

Expand All @@ -33,7 +33,7 @@ public function getEntity(): mixed;
* @throws AlreadyStartedException
* @throws SteppedFormErrorsException
*/
public function start(mixed $entity, string $sessionKey = SessionControlInterface::DEFAULT_SESSION_KEY): ?StepKey;
public function start(mixed $entity, string $sessionKey = FormStorageInterface::DEFAULT_SESSION_KEY): ?StepKey;

/**
* Returns a Template Definition for given step.
Expand Down
3 changes: 2 additions & 1 deletion tests/Form/DataControlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Lexal\SteppedForm\Exception\EntityNotFoundException;
use Lexal\SteppedForm\Form\DataControl;
use Lexal\SteppedForm\Form\Storage\DataStorage;
use Lexal\SteppedForm\Form\Storage\FormStorage;
use Lexal\SteppedForm\Step\Step;
use Lexal\SteppedForm\Step\StepBehaviourInterface;
use Lexal\SteppedForm\Step\StepInterface;
Expand All @@ -22,7 +23,7 @@ final class DataControlTest extends TestCase

protected function setUp(): void
{
$this->dataStorage = new DataStorage(new InMemoryStorage());
$this->dataStorage = new DataStorage(new FormStorage(new InMemoryStorage()));
$this->dataControl = new DataControl($this->dataStorage);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Form/StepControlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Lexal\SteppedForm\Exception\AlreadyStartedException;
use Lexal\SteppedForm\Exception\FormIsNotStartedException;
use Lexal\SteppedForm\Form\StepControl;
use Lexal\SteppedForm\Form\Storage\FormStorage;
use Lexal\SteppedForm\Step\StepKey;
use Lexal\SteppedForm\Tests\InMemoryStorage;
use PHPUnit\Framework\TestCase;
Expand All @@ -17,7 +18,7 @@ final class StepControlTest extends TestCase

protected function setUp(): void
{
$this->stepControl = new StepControl(new InMemoryStorage());
$this->stepControl = new StepControl(new FormStorage(new InMemoryStorage()));
}

public function testSetAndGetCurrent(): void
Expand Down
3 changes: 2 additions & 1 deletion tests/Form/Storage/DataStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Lexal\SteppedForm\Exception\KeysNotFoundInStorageException;
use Lexal\SteppedForm\Form\Storage\DataStorage;
use Lexal\SteppedForm\Form\Storage\FormStorage;
use Lexal\SteppedForm\Step\StepKey;
use Lexal\SteppedForm\Tests\InMemoryStorage;
use PHPUnit\Framework\TestCase;
Expand All @@ -16,7 +17,7 @@ final class DataStorageTest extends TestCase

protected function setUp(): void
{
$this->dataStorage = new DataStorage(new InMemoryStorage());
$this->dataStorage = new DataStorage(new FormStorage(new InMemoryStorage()));
}

/**
Expand Down
Loading

0 comments on commit 7ef6077

Please sign in to comment.