Renamed domain ActionManagement to Action

This commit is contained in:
Kevin Frantz
2019-05-30 16:03:44 +02:00
parent d2b0cba30b
commit 123df1147a
36 changed files with 112 additions and 112 deletions

File diff suppressed because it is too large Load Diff

After

Width:  |  Height:  |  Size: 191 KiB

View File

@@ -0,0 +1,57 @@
<?php
namespace Infinito\Domain\Action;
use Infinito\Entity\EntityInterface;
use Infinito\Exception\Validation\FormInvalidException;
use Infinito\Exception\Permission\NoPermissionException;
/**
* @author kevinfrantz
*/
abstract class AbstractAction extends AbstractActionConstructor implements ActionInterface
{
/**
* @return bool
*/
abstract protected function isSecure(): bool;
/**
* @return bool
*/
abstract protected function isValid(): bool;
/**
* Process the routine.
*
* @return EntityInterface|EntityInterface[]|null
*/
abstract protected function proccess();
/**
* This function can be implemented in the child classes for preparation.
*/
protected function prepare(): void
{
return;
}
/**
* @throws \Exception
*
* {@inheritdoc}
*
* @see \Infinito\Domain\Action\ActionInterface::execute()
*/
final public function execute(): ?EntityInterface
{
$this->prepare();
if ($this->isSecure()) {
if ($this->isValid()) {
return $this->proccess();
}
throw new FormInvalidException('The requested Entity is not valid!');
}
throw new NoPermissionException("You don't have the permission to execute this action!");
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace Infinito\Domain\Action;
/**
* This class just containes the constructor
* It is used by concrete actions and the factory.
*
* @author kevinfrantz
*/
abstract class AbstractActionConstructor
{
/**
* @var ActionDependenciesDAOServiceInterface
*/
protected $actionService;
/**
* @param ActionDependenciesDAOServiceInterface $actionService
*/
final public function __construct(ActionDependenciesDAOServiceInterface $actionService)
{
$this->actionService = $actionService;
}
}

View File

@@ -0,0 +1,123 @@
<?php
namespace Infinito\Domain\Action;
use Infinito\Domain\RequestManagement\Action\RequestedActionInterface;
use Infinito\Domain\RepositoryManagement\LayerRepositoryFactoryServiceInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Infinito\Repository\RepositoryInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Doctrine\ORM\EntityManagerInterface;
use Infinito\Domain\FormManagement\RequestedActionFormBuilderServiceInterface;
use Infinito\Domain\RequestManagement\Action\RequestedActionServiceInterface;
use Infinito\Domain\SecureManagement\SecureRequestedRightCheckerServiceInterface;
/**
* @author kevinfrantz
*/
final class ActionDependenciesDAOService implements ActionDependenciesDAOServiceInterface
{
/**
* @var RequestStack
*/
private $requestStack;
/**
* @var RequestedActionInterface
*/
private $requestedAction;
/**
* @var SecureRequestedRightCheckerServiceInterface
*/
private $secureRequestedRightCheckerService;
/**
* @var LayerRepositoryFactoryServiceInterface
*/
private $layerRepositoryFactoryService;
/**
* @var RequestedActionFormBuilderServiceInterface
*/
private $requestedActionFormBuilderService;
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @param RequestedActionInterface $requestedActionService
*/
public function __construct(RequestedActionServiceInterface $requestedActionService, SecureRequestedRightCheckerServiceInterface $secureRequestedRightChecker, RequestStack $requestStack, LayerRepositoryFactoryServiceInterface $layerRepositoryFactoryService, RequestedActionFormBuilderServiceInterface $requestedActionFormBuilderService, EntityManagerInterface $entityManager)
{
$this->requestedAction = $requestedActionService;
$this->secureRequestedRightCheckerService = $secureRequestedRightChecker;
$this->requestStack = $requestStack;
$this->layerRepositoryFactoryService = $layerRepositoryFactoryService;
$this->requestedActionFormBuilderService = $requestedActionFormBuilderService;
$this->entityManager = $entityManager;
}
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Action\ActionDependenciesDAOServiceInterface::getRequestedAction()
*/
public function getRequestedAction(): RequestedActionInterface
{
return $this->requestedAction;
}
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Action\ActionDependenciesDAOServiceInterface::isRequestedActionSecure()
*/
public function isRequestedActionSecure(): bool
{
return $this->secureRequestedRightCheckerService->check($this->requestedAction);
}
/**
* @return FormBuilderInterface
*/
public function getCurrentFormBuilder(): FormBuilderInterface
{
return $this->requestedActionFormBuilderService->createByService();
}
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Action\ActionDependenciesDAOServiceInterface::getRequest()
*/
public function getRequest(): Request
{
return $this->requestStack->getCurrentRequest();
}
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Action\ActionDependenciesDAOServiceInterface::getRepository()
*/
public function getRepository(): RepositoryInterface
{
$layer = $this->requestedAction->getLayer();
return $this->layerRepositoryFactoryService->getRepository($layer);
}
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Action\ActionDependenciesDAOServiceInterface::getEntityManager()
*/
public function getEntityManager(): EntityManagerInterface
{
return $this->entityManager;
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace Infinito\Domain\Action;
use Infinito\Domain\RequestManagement\Action\RequestedActionInterface;
use Symfony\Component\HttpFoundation\Request;
use Infinito\Repository\RepositoryInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Doctrine\ORM\EntityManagerInterface;
/**
* This interface offers all classes for managing an Action.
*
* @author kevinfrantz
*/
interface ActionDependenciesDAOServiceInterface
{
/**
* @return RequestedActionInterface Returns the requested action
*/
public function getRequestedAction(): RequestedActionInterface;
/**
* @return bool true if the action permissions are right
*/
public function isRequestedActionSecure(): bool;
/**
* @return Request
*/
public function getRequest(): Request;
/**
* @return RepositoryInterface
*/
public function getRepository(): RepositoryInterface;
/**
* @return FormBuilderInterface
*/
public function getCurrentFormBuilder(): FormBuilderInterface;
/**
* @return EntityManagerInterface
*/
public function getEntityManager(): EntityManagerInterface;
}

View File

@@ -0,0 +1,84 @@
<?php
namespace Infinito\Domain\Action;
use Infinito\Exception\NoDefaultClassException;
/**
* @author kevinfrantz
*/
final class ActionFactoryService extends AbstractActionConstructor implements ActionFactoryServiceInterface
{
/**
* @var string Namespace in which the actions will be found
*/
private const BASE_NAMESPACE = 'Infinito\\Domain\\Action\\';
/**
* @var string Suffix for action classes
*/
private const CLASS_SUFFIX = 'Action';
/**
* @param string $name
*
* @return string
*/
private function ucfirst(string $name): string
{
return ucfirst(strtolower($name));
}
/**
* @param string $action
* @param string $layer
*
* @return string
*/
private function getClassName(string $action, string $layer = ''): string
{
return $this->ucfirst($action).$this->ucfirst($layer).self::CLASS_SUFFIX;
}
/**
* @param string $layer
* @param string $action
*
* @return string
*/
private function getActionNamespace(string $action, string $layer = ''): string
{
return self::BASE_NAMESPACE.$this->ucfirst($action).'\\'.$this->getClassName($action, $layer);
}
/**
* @return string
*/
private function generateFullClassName(): string
{
$requestedAction = $this->actionService->getRequestedAction();
$action = $requestedAction->getActionType();
$layer = $requestedAction->getLayer();
$class = $this->getActionNamespace($action, $layer);
if (class_exists($class)) {
return $class;
}
$defaultClass = $this->getActionNamespace($action);
if (class_exists($defaultClass)) {
return $defaultClass;
}
throw new NoDefaultClassException("There is no default substitution class for $class with attributes {layer:\"$layer\",action:\"$action\"}");
}
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Action\ActionFactoryServiceInterface::create()
*/
public function create(): ActionInterface
{
$class = $this->generateFullClassName();
return new $class($this->actionService);
}
}

View File

@@ -0,0 +1,16 @@
<?php
namespace Infinito\Domain\Action;
/**
* Offers a function to create an action object by the RequestedActionService.
*
* @author kevinfrantz
*/
interface ActionFactoryServiceInterface
{
/**
* @return ActionInterface
*/
public function create(): ActionInterface;
}

View File

@@ -0,0 +1,34 @@
<?php
namespace Infinito\Domain\Action;
use Infinito\Entity\EntityInterface;
/**
* @author kevinfrantz
*/
final class ActionHandlerService implements ActionHandlerServiceInterface
{
/**
* @var ActionFactoryServiceInterface
*/
private $actionFactoryService;
/**
* @param ActionFactoryServiceInterface $actionFactoryService
*/
public function __construct(ActionFactoryServiceInterface $actionFactoryService)
{
$this->actionFactoryService = $actionFactoryService;
}
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Action\ActionHandlerServiceInterface::handle()
*/
public function handle(): ?EntityInterface
{
return $this->actionFactoryService->create()->execute();
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace Infinito\Domain\Action;
use Infinito\Entity\EntityInterface;
/**
* @author kevinfrantz
*/
interface ActionHandlerServiceInterface
{
/**
* Process an action an returns the results.
*
* @todo Implement that also results can be returned
*
* @return EntityInterface|null
*/
public function handle(): ?EntityInterface;
}

View File

@@ -0,0 +1,20 @@
<?php
namespace Infinito\Domain\Action;
use Infinito\Entity\EntityInterface;
/**
* @author kevinfrantz
*/
interface ActionInterface
{
/**
* Executes the action.
*
* @todo Implement that also results can be returned
*
* @return EntityInterface|null
*/
public function execute(): ?EntityInterface;
}

View File

@@ -0,0 +1,22 @@
<?php
namespace Infinito\Domain\Action\Create;
use Infinito\Domain\Action\AbstractAction;
/**
* @author kevinfrantz
*/
abstract class AbstractCreateAction extends AbstractAction implements CreateActionInterface
{
/**
* In general everybody should be allowed to create everything!
* {@inheritdoc}
*
* @see \Infinito\Domain\Action\AbstractAction::isSecure()
*/
protected function isSecure(): bool
{
return true;
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace Infinito\Domain\Action\Create;
use Infinito\Domain\Action\ActionInterface;
/**
* @author kevinfrantz
*/
interface CreateActionInterface extends ActionInterface
{
}

View File

@@ -0,0 +1,89 @@
<?php
namespace Infinito\Domain\Action\Create;
use Infinito\Domain\SourceManagement\SourceClassInformationService;
use Infinito\Entity\Source\AbstractSource;
use Symfony\Component\Form\Form;
use Infinito\Domain\ParameterManagement\Parameter\ClassParameter;
/**
* @author kevinfrantz
*/
final class CreateSourceAction extends AbstractCreateAction
{
/**
* @var string default class name, when no parameter is defined
*/
private const DEFAULT_CLASS = AbstractSource::class;
/**
* @see SourceClassInformationService
*
* @var string The source class which should be used
*/
private $sourceClass;
/**
* @var Form
*/
private $form;
private function setSourceClass(): void
{
$request = $this->actionService->getRequest();
$this->sourceClass = $request->get(ClassParameter::getKey(), self::DEFAULT_CLASS);
}
private function setForm(): void
{
$this->form = $this->actionService->getCurrentFormBuilder()->getForm();
}
private function setRequestedEntityClass(): void
{
$this->actionService->getRequestedAction()->getRequestedEntity()->setClass($this->sourceClass);
}
private function handleRequest(): void
{
$this->form->handleRequest($this->actionService->getRequest());
}
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Action\AbstractAction::prepare()
*/
protected function prepare(): void
{
$this->setSourceClass();
$this->setRequestedEntityClass();
$this->setForm();
$this->handleRequest();
}
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Action\AbstractAction::isValid()
*/
protected function isValid(): bool
{
//The following Exception just exists out of debuging reasons during the development process
if (!$this->form->isSubmitted()) {
throw new \Exception('The form is not submitted!');
}
return $this->form->isValid();
}
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Action\AbstractAction::proccess()
*/
protected function proccess()
{
}
}

View File

@@ -0,0 +1,45 @@
<?php
namespace Infinito\Domain\Action\Delete;
use Infinito\Domain\Action\AbstractAction;
/**
* @author kevinfrantz
* Declare as not final as soon as you need it!
*/
final class DeleteAction extends AbstractAction
{
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Action\AbstractAction::isSecure()
*/
protected function isSecure(): bool
{
return $this->isSecure();
}
/**
* @todo Implement!
* {@inheritdoc}
*
* @see \Infinito\Domain\Action\AbstractAction::isValid()
*/
protected function isValid(): bool
{
return true;
}
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Action\AbstractAction::proccess()
*/
protected function proccess()
{
$entityManager = $this->actionService->getEntityManager();
$entity = $this->actionService->getRequestedAction()->getRequestedEntity()->getEntity();
$entityManager->remove($entity);
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace Infinito\Domain\Action\Execute;
use Infinito\Domain\Action\AbstractAction;
/**
* @author kevinfrantz
*/
abstract class AbstractExecuteAction extends AbstractAction
{
}

View File

@@ -0,0 +1,36 @@
<?php
namespace Infinito\Domain\Action\Execute;
/**
* @author kevinfrantz
*/
final class ExecuteAction extends AbstractExecuteAction
{
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Action\AbstractAction::isSecure()
*/
protected function isSecure(): bool
{
}
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Action\AbstractAction::isValid()
*/
protected function isValid(): bool
{
}
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Action\AbstractAction::proccess()
*/
protected function proccess()
{
}
}

View File

@@ -0,0 +1,3 @@
# Action
## Brainstorming
![Brainstorming Class UML Diagram](.meta/uml-class-brainstorming.svg)

View File

@@ -0,0 +1,42 @@
<?php
namespace Infinito\Domain\Action\Read;
use Infinito\Domain\Action\AbstractAction;
/**
* @author kevinfrantz
*/
final class ReadAction extends AbstractAction implements ReadActionInterface
{
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Action\AbstractAction::isSecure()
*/
protected function isSecure(): bool
{
return $this->actionService->isRequestedActionSecure();
}
/**
* @todo Implement!
* {@inheritdoc}
*
* @see \Infinito\Domain\Action\AbstractAction::isValid()
*/
protected function isValid(): bool
{
return true;
}
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Action\AbstractAction::proccess()
*/
protected function proccess()
{
return $this->actionService->getRequestedAction()->getRequestedEntity()->getEntity();
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Infinito\Domain\Action\Read;
use Infinito\Domain\Action\ActionInterface;
/**
* Needed for mocking with PHPUnit!
*
* @author kevinfrantz
*/
interface ReadActionInterface extends ActionInterface
{
}

View File

@@ -0,0 +1,12 @@
<?php
namespace Infinito\Domain\Action\Update;
use Infinito\Domain\Action\AbstractAction;
/**
* @author kevinfrantz
*/
abstract class AbstractUpdateAction extends AbstractAction
{
}

View File

@@ -0,0 +1,36 @@
<?php
namespace Infinito\Domain\Action\Update;
/**
* @author kevinfrantz
*/
final class UpdateSourceAction extends AbstractUpdateAction
{
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Action\AbstractAction::isSecure()
*/
protected function isSecure(): bool
{
}
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Action\AbstractAction::isValid()
*/
protected function isValid(): bool
{
}
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\Action\AbstractAction::proccess()
*/
protected function proccess()
{
}
}