Optimized RequestedEntityFormBuilderService and implemented form classes and logic

This commit is contained in:
Kevin Frantz
2019-02-03 15:21:45 +01:00
parent c3b8e1a92d
commit ed3062a203
18 changed files with 247 additions and 79 deletions

View File

@@ -30,11 +30,12 @@ abstract class AbstractAction extends AbstractActionConstructor implements Actio
/**
* @throws \Exception
* {@inheritdoc}
*
* {@inheritdoc}
*
* @see \App\Domain\ActionManagement\ActionInterface::execute()
*/
public function execute()
final public function execute()
{
if ($this->isSecure()) {
if ($this->isValidByForm()) {

View File

@@ -6,7 +6,7 @@ use App\Domain\RequestManagement\Action\RequestedActionInterface;
use App\Domain\SecureManagement\SecureRequestedRightCheckerInterface;
use App\Domain\RepositoryManagement\LayerRepositoryFactoryServiceInterface;
use Symfony\Component\HttpFoundation\Request;
use App\Domain\FormManagement\EntityFormBuilderServiceInterface;
use App\Domain\FormManagement\RequestedEntityFormBuilderServiceInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use App\Repository\RepositoryInterface;
use Symfony\Component\Form\FormBuilderInterface;
@@ -38,7 +38,7 @@ final class ActionService implements ActionServiceInterface
private $layerRepositoryFactoryService;
/**
* @var
* @var RequestedEntityFormBuilderServiceInterface
*/
private $entityFormBuilderService;
@@ -50,13 +50,13 @@ final class ActionService implements ActionServiceInterface
/**
* @param RequestedActionInterface $requestedAction
*/
public function __construct(RequestedActionInterface $requestedAction, SecureRequestedRightCheckerInterface $secureRequestedRightChecker, RequestStack $requestStack, LayerRepositoryFactoryServiceInterface $layerRepositoryFactoryService, EntityFormBuilderServiceInterface $entityFormBuilderService, EntityManagerInterface $entityManager)
public function __construct(RequestedActionInterface $requestedAction, SecureRequestedRightCheckerInterface $secureRequestedRightChecker, RequestStack $requestStack, LayerRepositoryFactoryServiceInterface $layerRepositoryFactoryService, RequestedEntityFormBuilderServiceInterface $requestedEntityFormBuilderService, EntityManagerInterface $entityManager)
{
$this->requestedAction = $requestedAction;
$this->secureRequestedRightChecker = $secureRequestedRightChecker;
$this->requestStack = $requestStack;
$this->layerRepositoryFactoryService = $layerRepositoryFactoryService;
$this->entityFormBuilderService = $entityFormBuilderService;
$this->entityFormBuilderService = $requestedEntityFormBuilderService;
$this->entityManager = $entityManager;
}
@@ -85,9 +85,9 @@ final class ActionService implements ActionServiceInterface
*/
public function getForm(): FormBuilderInterface
{
$entity = $this->requestedAction->getRequestedEntity()->getEntity();
$requestedEntity = $this->requestedAction->getRequestedEntity();
return $this->entityFormBuilderService->create($entity);
return $this->entityFormBuilderService->create($requestedEntity);
}
/**

View File

@@ -2,16 +2,55 @@
namespace App\Domain\ActionManagement\Create;
use App\Domain\SourceManagement\SourceClassInformationService;
use App\Form\Source\SourceType;
use App\Entity\Source\AbstractSource;
/**
* @author kevinfrantz
*/
final class CreateSourceAction extends AbstractCreateAction
{
protected function isValidByForm(): bool
/**
* @var string default class name, when no parameter is defined
*/
const DEFAULT_CLASS = AbstractSource::class;
/**
* @see SourceClassInformationService
*
* @var string The source class which should be used
*/
private $sourceClass;
private function setSourceClass(): void
{
$request = $this->actionService->getRequest();
$this->sourceClass = $request->get(SourceType::CLASS_PARAMETER_NAME, self::DEFAULT_CLASS);
}
private function prepare(): void
{
$this->setSourceClass();
}
/**
* {@inheritdoc}
*
* @see \App\Domain\ActionManagement\AbstractAction::isValidByForm()
*/
protected function isValidByForm(): bool
{
$this->actionService->getForm();
}
/**
* {@inheritdoc}
*
* @see \App\Domain\ActionManagement\AbstractAction::proccess()
*/
protected function proccess()
{
$this->prepare();
}
}

View File

@@ -1,21 +0,0 @@
<?php
namespace App\Domain\FormManagement;
use App\Entity\EntityInterface;
use Symfony\Component\Form\FormBuilderInterface;
/**
* Allowes to create an form which fits to an entity.
*
* @author kevinfrantz
*/
interface EntityFormBuilderServiceInterface
{
/**
* @param EntityInterface $entity
*
* @return FormBuilderInterface
*/
public function create(EntityInterface $entity): FormBuilderInterface;
}

View File

@@ -2,8 +2,6 @@
namespace App\Domain\FormManagement;
use App\Entity\EntityInterface;
/**
* @author kevinfrantz
*/
@@ -16,14 +14,13 @@ final class FormClassNameService implements FormClassNameServiceInterface
const SUFFIX = 'Type';
/**
* @param EntityInterface $entity
* {@inheritdoc}
*
* @return string
* @see \App\Domain\FormManagement\FormClassNameServiceInterface::getClass()
*/
public function getName(EntityInterface $entity): string
public function getClass(string $origineClass): string
{
$class = get_class($entity);
$replaced = str_replace(self::ENTITY_BASE_PATH, self::FORM_BASE_PATH, $class);
$replaced = str_replace(self::ENTITY_BASE_PATH, self::FORM_BASE_PATH, $origineClass);
$withSuffix = $replaced.self::SUFFIX;
return $withSuffix;

View File

@@ -14,5 +14,5 @@ interface FormClassNameServiceInterface
*
* @return string The name of the form of the entity
*/
public function getName(EntityInterface $entity): string;
public function getClass(string $origineClass): string;
}

View File

@@ -2,13 +2,13 @@
namespace App\Domain\FormManagement;
use App\Entity\EntityInterface;
use Symfony\Component\Form\FormBuilderInterface;
use App\Domain\RequestManagement\Entity\RequestedEntityInterface;
/**
* @author kevinfrantz
*/
final class EntityFormBuilderService implements EntityFormBuilderServiceInterface
final class RequestedEntityFormBuilderService implements EntityFormBuilderServiceInterface
{
/**
* @var FormBuilderInterface
@@ -34,9 +34,13 @@ final class EntityFormBuilderService implements EntityFormBuilderServiceInterfac
*
* @see \App\Domain\FormManagement\EntityFormBuilderServiceInterface::create()
*/
public function create(EntityInterface $entity): FormBuilderInterface
public function create(RequestedEntityInterface $requestedEntity): FormBuilderInterface
{
$class = $this->formClassNameService->getName($entity);
$origineClass = $requestedEntity->getClass();
$class = $this->formClassNameService->getClass($origineClass);
if ($requestedEntity->hasIdentity()) {
$entity = $requestedEntity->getEntity();
}
$form = $this->formBuilder->create($class, $entity);
return $form;

View File

@@ -0,0 +1,22 @@
<?php
namespace App\Domain\FormManagement;
use Symfony\Component\Form\FormBuilderInterface;
use App\Domain\RequestManagement\Entity\RequestedEntity;
use App\Domain\RequestManagement\Entity\RequestedEntityInterface;
/**
* Allowes to create an form which fits to an entity.
*
* @author kevinfrantz
*/
interface RequestedEntityFormBuilderServiceInterface
{
/**
* @param RequestedEntityInterface $requestedEntity
*
* @return FormBuilderInterface
*/
public function create(RequestedEntityInterface $requestedEntity): FormBuilderInterface;
}

View File

@@ -4,6 +4,9 @@ namespace App\Form;
use Symfony\Component\Form\AbstractType as AbstractSymfonyType;
class AbstractType extends AbstractSymfonyType
/**
* @author kevinfrantz
*/
abstract class AbstractType extends AbstractSymfonyType
{
}

View File

@@ -0,0 +1,21 @@
<?php
namespace App\Form\Source;
use Symfony\Component\Form\FormBuilderInterface;
/**
* @author kevinfrantz
*/
final class PureSourceType extends SourceType
{
/**
* {@inheritdoc}
*
* @see \Symfony\Component\Form\AbstractType::buildForm()
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('slug')->add('class');
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace App\Form\Source;
use App\Form\AbstractType;
/**
* @author kevinfrantz
*/
class SourceType extends AbstractType
{
const CLASS_PARAMETER_NAME = 'class';
}

View File

@@ -0,0 +1,2 @@
# Type
This folder containes general types.

View File

@@ -0,0 +1,63 @@
<?php
namespace App\Form\Type;
use App\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use App\Domain\SourceManagement\SourceClassInformationService;
/**
* @author kevinfrantz
*/
final class SourceType extends AbstractType implements SourceTypeInterface
{
const UNUSED_PRAEFIX = 'App\\Entity\\Source';
/**
* @param string $class
*
* @return string Key which can be used in choice selection
*/
private function getChoiceKey(string $class): string
{
return str_replace(self::UNUSED_PRAEFIX, '', $class);
}
/**
* @return array
*/
private function getChoices(): array
{
$choices = [];
$sourceClassInformationService = new SourceClassInformationService();
$allClasses = $sourceClassInformationService->getAllSourceClasses();
foreach ($allClasses as $class) {
$choices[$this->getChoiceKey($class)] = $class;
}
return $choices;
}
/**
* {@inheritdoc}
*
* @see \Symfony\Component\Form\AbstractType::configureOptions()
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'choices' => $this->getChoices(),
]);
}
/**
* {@inheritdoc}
*
* @see \Symfony\Component\Form\AbstractType::getParent()
*/
public function getParent()
{
return ChoiceType::class;
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace App\Form\Type;
use Symfony\Component\Form\FormTypeInterface;
/**
* @author kevinfrantz
*/
interface SourceTypeInterface extends FormTypeInterface
{
}