mirror of
https://github.com/kevinveenbirkenbach/infinito.git
synced 2024-12-04 23:17:19 +01:00
Implemented ActionTemplateDataStoreService
This commit is contained in:
parent
15f9925e66
commit
cbb1c76640
@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
namespace Infinito\Domain\TemplateManagement;
|
||||
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Infinito\Exception\AllreadySetException;
|
||||
use Infinito\Exception\NotSetException;
|
||||
use Infinito\DBAL\Types\ActionType;
|
||||
use Infinito\Exception\NotDefinedException;
|
||||
use Infinito\Exception\NoValidChoiceException;
|
||||
use Infinito\Form\AbstractType;
|
||||
use Infinito\Entity\EntityInterface;
|
||||
use Infinito\Domain\ActionManagement\AbstractAction;
|
||||
use Infinito\Exception\NotCorrectInstanceException;
|
||||
|
||||
/**
|
||||
* @author kevinfrantz
|
||||
*/
|
||||
final class ActionTemplateDataStoreService implements ActionTemplateDataStoreServiceInterface
|
||||
{
|
||||
/**
|
||||
* @var array|string[] Maps the action to an return type
|
||||
*/
|
||||
const ACTION_DATA_MAPPING = [
|
||||
ActionType::CREATE => AbstractType::class,
|
||||
ActionType::READ => EntityInterface::class, // Mayber change this to refection later!
|
||||
ActionType::UPDATE => AbstractAction::class,
|
||||
ActionType::DELETE => EntityInterface::class,
|
||||
ActionType::EXECUTE => EntityInterface::class, // This is just a dummy value to pass tests. Substitute it!
|
||||
];
|
||||
|
||||
/**
|
||||
* @var ArrayCollection
|
||||
*/
|
||||
private $actionDataMap;
|
||||
|
||||
/**
|
||||
* @param string $actionType
|
||||
*
|
||||
* @throws NotDefinedException For false a exception is thrown
|
||||
*
|
||||
* @return bool Everytime True
|
||||
*/
|
||||
private function isValidActionType(string $actionType): bool
|
||||
{
|
||||
if (in_array($actionType, ActionType::getChoices())) {
|
||||
return true;
|
||||
}
|
||||
throw new NoValidChoiceException("The action type <<$actionType>> is not defined and not valid!");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $actionType
|
||||
* @param mixed $data
|
||||
*
|
||||
* @throws NotCorrectInstanceException For false a exception is thrown
|
||||
*
|
||||
* @return bool Everytime True
|
||||
*/
|
||||
private function isValidActionData(string $actionType, $data): bool
|
||||
{
|
||||
$instance = self::ACTION_DATA_MAPPING[$actionType];
|
||||
if ($data instanceof $instance) {
|
||||
return true;
|
||||
}
|
||||
throw new NotCorrectInstanceException("The intance doesn\'t map to <<$instance>>.");
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->actionDataMap = new ArrayCollection();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \Infinito\Domain\TemplateManagement\ActionTemplateDataStoreServiceInterface::setActionTemplateData()
|
||||
*/
|
||||
public function setData(string $actionType, $data): void
|
||||
{
|
||||
if ($this->isValidActionType($actionType) && $this->isValidActionData($actionType, $data) && $this->isDataStored($actionType)) {
|
||||
throw new AllreadySetException("The data for the action type <<$actionType>> is allready set!");
|
||||
}
|
||||
$this->actionDataMap->set($actionType, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \Infinito\Domain\TemplateManagement\ActionTemplateDataStoreServiceInterface::getActionTemplateData()
|
||||
*/
|
||||
public function getData(string $actionType)
|
||||
{
|
||||
if ($this->isValidActionType($actionType) && $this->isDataStored($actionType)) {
|
||||
return $this->actionDataMap->get($actionType);
|
||||
}
|
||||
throw new NotSetException("The data for the action type <<$actionType>> is not set!");
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \Infinito\Domain\TemplateManagement\ActionTemplateDataStoreServiceInterface::isActionTemplateDataSet()
|
||||
*/
|
||||
public function isDataStored(string $actionType): bool
|
||||
{
|
||||
return $this->actionDataMap->containsKey($actionType);
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Infinito\Domain\TemplateManagement;
|
||||
|
||||
/**
|
||||
* This class offers a temporary data store to pass data from the controller logic to the template.
|
||||
*
|
||||
* @author kevinfrantz
|
||||
*
|
||||
* @see https://en.wikipedia.org/wiki/Data_store
|
||||
*/
|
||||
interface ActionTemplateDataStoreServiceInterface
|
||||
{
|
||||
/**
|
||||
* @param string $actionType
|
||||
* @param mixed $data The data which a Template needs to be handled
|
||||
*/
|
||||
public function setData(string $actionType, $data): void;
|
||||
|
||||
/**
|
||||
* @param string $actionType
|
||||
*
|
||||
* @return mixed The needed data
|
||||
*/
|
||||
public function getData(string $actionType);
|
||||
|
||||
/**
|
||||
* @param string $actionType
|
||||
*
|
||||
* @return bool True if the data is set
|
||||
*/
|
||||
public function isDataStored(string $actionType): bool;
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace tests\Unit\Domain\TemplateManagement;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Infinito\Domain\TemplateManagement\ActionTemplateDataStoreServiceInterface;
|
||||
use Infinito\Domain\TemplateManagement\ActionTemplateDataStoreService;
|
||||
use Infinito\Exception\NoValidChoiceException;
|
||||
use Infinito\Exception\NotSetException;
|
||||
use Infinito\DBAL\Types\ActionType;
|
||||
use Infinito\Exception\AllreadySetException;
|
||||
use Infinito\Exception\NotCorrectInstanceException;
|
||||
|
||||
/**
|
||||
* @author kevinfrantz
|
||||
*/
|
||||
class ActionTemplateDataStoreServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var ActionTemplateDataStoreServiceInterface
|
||||
*/
|
||||
private $actionTemplateDataStoreService;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \PHPUnit\Framework\TestCase::setUp()
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->actionTemplateDataStoreService = new ActionTemplateDataStoreService();
|
||||
}
|
||||
|
||||
public function testNotValidChoiceSetException(): void
|
||||
{
|
||||
$this->expectException(NoValidChoiceException::class);
|
||||
$this->actionTemplateDataStoreService->setData('1231232N', ' ');
|
||||
}
|
||||
|
||||
public function testNotCorrectInstanceSetException(): void
|
||||
{
|
||||
$this->expectException(NotCorrectInstanceException::class);
|
||||
$data = new class() {
|
||||
};
|
||||
$this->actionTemplateDataStoreService->setData(ActionType::READ, $data);
|
||||
}
|
||||
|
||||
public function testNotValidChoiceGetException(): void
|
||||
{
|
||||
$this->expectException(NoValidChoiceException::class);
|
||||
$this->actionTemplateDataStoreService->getData('1231232N');
|
||||
}
|
||||
|
||||
public function testNotSetGetException(): void
|
||||
{
|
||||
$this->expectException(NotSetException::class);
|
||||
$this->actionTemplateDataStoreService->getData(ActionType::READ);
|
||||
}
|
||||
|
||||
public function testAccessors(): void
|
||||
{
|
||||
foreach (ActionType::getChoices() as $actionType) {
|
||||
$instance = ActionTemplateDataStoreService::ACTION_DATA_MAPPING[$actionType];
|
||||
$data = $this->createMock($instance);
|
||||
$this->assertFalse($this->actionTemplateDataStoreService->isDataStored($actionType));
|
||||
$this->assertNull($this->actionTemplateDataStoreService->setData($actionType, $data));
|
||||
$this->assertTrue($this->actionTemplateDataStoreService->isDataStored($actionType));
|
||||
$this->assertEquals($data, $this->actionTemplateDataStoreService->getData($actionType));
|
||||
}
|
||||
$this->expectException(AllreadySetException::class);
|
||||
$this->assertNull($this->actionTemplateDataStoreService->setData($actionType, $data));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user