2019-04-13 17:25:24 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Infinito\Domain\DataAccessManagement;
|
|
|
|
|
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
2019-04-14 23:37:30 +02:00
|
|
|
use Doctrine\Common\Collections\Collection;
|
2019-04-13 17:25:24 +02:00
|
|
|
use Infinito\DBAL\Types\ActionType;
|
|
|
|
use Infinito\Entity\EntityInterface;
|
2019-04-14 23:37:30 +02:00
|
|
|
use Infinito\Exception\Collection\ContainsElementException;
|
|
|
|
use Infinito\Exception\Collection\NotSetElementException;
|
|
|
|
use Infinito\Exception\Type\InvalidChoiceTypeException;
|
|
|
|
use Infinito\Exception\Validation\ValueInvalidException;
|
2019-04-13 17:25:24 +02:00
|
|
|
use Infinito\Logic\Result\ResultInterface;
|
2019-04-13 20:12:32 +02:00
|
|
|
use Symfony\Component\Intl\Exception\NotImplementedException;
|
2019-04-13 17:25:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author kevinfrantz
|
|
|
|
*/
|
|
|
|
final class ActionsResultsDAOService extends AbstractActionsDAO implements ActionsResultsDAOServiceInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var Collection|mixed[]
|
|
|
|
*/
|
|
|
|
private $processedData;
|
|
|
|
|
2019-04-13 20:12:32 +02:00
|
|
|
/**
|
|
|
|
* @param string $actionType
|
|
|
|
* @param EntityInterface|ResultInterface|null $data
|
|
|
|
*
|
|
|
|
* @return bool True if the data is valid
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function isValidActionData(string $actionType, $data): bool
|
|
|
|
{
|
|
|
|
switch ($actionType) {
|
|
|
|
case ActionType::CREATE:
|
2019-04-13 21:22:16 +02:00
|
|
|
return $data instanceof EntityInterface | null === $data;
|
|
|
|
case ActionType::READ:
|
2019-04-13 20:12:32 +02:00
|
|
|
case ActionType::UPDATE:
|
|
|
|
return $data instanceof EntityInterface;
|
|
|
|
case ActionType::DELETE:
|
|
|
|
return null === $data;
|
|
|
|
case ActionType::EXECUTE:
|
|
|
|
return $data instanceof ResultInterface;
|
|
|
|
}
|
|
|
|
throw new NotImplementedException("The ActionType <<$actionType>> is not implemented in <<".__CLASS__.':'.__FUNCTION__.'>>');
|
|
|
|
}
|
|
|
|
|
2019-04-13 17:25:24 +02:00
|
|
|
/**
|
|
|
|
* @param string $actionType
|
2019-04-15 01:37:17 +02:00
|
|
|
*
|
2019-04-14 23:37:30 +02:00
|
|
|
* @throws InvalidChoiceTypeException
|
2019-04-13 17:25:24 +02:00
|
|
|
*/
|
|
|
|
private function throwNoValidActionTypeException(string $actionType): void
|
|
|
|
{
|
2019-04-14 23:37:30 +02:00
|
|
|
throw new InvalidChoiceTypeException("The action type <<$actionType>> is not defined and not valid!");
|
2019-04-13 17:25:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $actionType
|
|
|
|
*
|
2019-04-13 20:12:32 +02:00
|
|
|
* @return bool
|
2019-04-13 17:25:24 +02:00
|
|
|
*/
|
|
|
|
private function isValidActionType(string $actionType): bool
|
|
|
|
{
|
2019-04-13 20:12:32 +02:00
|
|
|
return in_array($actionType, ActionType::getValues());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $actionType
|
|
|
|
*/
|
|
|
|
private function validateActionType(string $actionType): void
|
|
|
|
{
|
|
|
|
if (!$this->isValidActionType($actionType)) {
|
|
|
|
$this->throwNoValidActionTypeException($actionType);
|
2019-04-13 17:25:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function describes which data is expected.
|
|
|
|
*
|
|
|
|
* @param string $actionType
|
|
|
|
* @param mixed $data
|
|
|
|
*
|
2019-04-14 23:37:30 +02:00
|
|
|
* @throws ValueInvalidException For false a exception is thrown
|
2019-04-13 20:12:32 +02:00
|
|
|
*/
|
|
|
|
private function validateActionData(string $actionType, $data): void
|
|
|
|
{
|
|
|
|
if (!$this->isValidActionData($actionType, $data)) {
|
2019-04-14 23:37:30 +02:00
|
|
|
throw new ValueInvalidException('Data <<'.gettype($data).(is_object($data) ? ':'.get_class($data) : '').">> is not valid for action type <<$actionType>>!");
|
2019-04-13 20:12:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $actionType
|
2019-04-13 17:25:24 +02:00
|
|
|
*
|
2019-04-14 23:37:30 +02:00
|
|
|
* @throws ContainsElementException
|
2019-04-13 17:25:24 +02:00
|
|
|
*/
|
2019-04-13 20:12:32 +02:00
|
|
|
private function validateNotSet(string $actionType): void
|
2019-04-13 17:25:24 +02:00
|
|
|
{
|
2019-04-13 20:12:32 +02:00
|
|
|
if ($this->isDataStored($actionType)) {
|
2019-04-14 23:37:30 +02:00
|
|
|
throw new ContainsElementException("Data for <<$actionType>> is allready stored.");
|
2019-04-13 17:25:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-13 20:12:32 +02:00
|
|
|
* @param string $actionType
|
2019-04-13 17:25:24 +02:00
|
|
|
*
|
2019-04-14 23:37:30 +02:00
|
|
|
* @throws NotSetElementException
|
2019-04-13 17:25:24 +02:00
|
|
|
*/
|
2019-04-13 20:12:32 +02:00
|
|
|
private function validateSet(string $actionType): void
|
2019-04-13 17:25:24 +02:00
|
|
|
{
|
2019-04-13 20:12:32 +02:00
|
|
|
if (!$this->isDataStored($actionType)) {
|
2019-04-14 23:37:30 +02:00
|
|
|
throw new NotSetElementException("No data for <<$actionType>> is stored.");
|
2019-04-13 17:25:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->processedData = new ArrayCollection();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @see \Infinito\Domain\DataAccessManagement\ActionsDAOInterface::getAllStoredData()
|
|
|
|
*/
|
|
|
|
public function getAllStoredData(): Collection
|
|
|
|
{
|
|
|
|
return $this->processedData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @see \Infinito\Domain\DataAccessManagement\ActionsResultsDAOServiceInterface::setData()
|
|
|
|
*/
|
|
|
|
public function setData(string $actionType, $data): void
|
|
|
|
{
|
2019-04-13 20:12:32 +02:00
|
|
|
$this->validateActionType($actionType);
|
|
|
|
$this->validateActionData($actionType, $data);
|
|
|
|
$this->validateNotSet($actionType);
|
2019-04-13 17:25:24 +02:00
|
|
|
$this->processedData->set($actionType, $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @see \Infinito\Domain\DataAccessManagement\ActionsDAOInterface::isDataStored()
|
|
|
|
*/
|
|
|
|
public function isDataStored(string $actionType): bool
|
|
|
|
{
|
|
|
|
return $this->processedData->containsKey($actionType);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @see \Infinito\Domain\DataAccessManagement\ActionsDAOInterface::getData()
|
|
|
|
*/
|
|
|
|
public function getData(string $actionType)
|
|
|
|
{
|
2019-04-13 20:12:32 +02:00
|
|
|
$this->validateActionType($actionType);
|
|
|
|
$this->validateSet($actionType);
|
|
|
|
|
|
|
|
return $this->processedData->get($actionType);
|
2019-04-13 17:25:24 +02:00
|
|
|
}
|
|
|
|
}
|