mirror of
https://github.com/kevinveenbirkenbach/infinito.git
synced 2025-09-13 13:57:10 +02:00
Implemented ActionTemplateDataStoreService
This commit is contained in:
@@ -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;
|
||||
}
|
Reference in New Issue
Block a user