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
{
}

View File

@ -6,7 +6,7 @@ use App\Domain\ActionManagement\Create\CreateSourceAction;
use App\Domain\ActionManagement\ActionService;
use App\Domain\ActionManagement\Create\CreateActionInterface;
use App\Domain\ActionManagement\ActionServiceInterface;
use App\Domain\FormManagement\EntityFormBuilderServiceInterface;
use App\Domain\FormManagement\RequestedEntityFormBuilderServiceInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use App\Domain\RepositoryManagement\LayerRepositoryFactoryServiceInterface;
use App\Domain\SecureManagement\SecureRequestedRightCheckerInterface;
@ -16,8 +16,13 @@ use App\Domain\RequestManagement\Right\RequestedRightService;
use App\Domain\RequestManagement\Action\RequestedActionServiceInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use App\DBAL\Types\ActionType;
use App\Domain\RequestManagement\User\RequestedUserService;
use App\Domain\UserManagement\UserSourceDirectorService;
use Symfony\Component\Security\Core\Security;
/**
* @todo Implement test and logic!!!!!
*
* @author kevinfrantz
*/
class CreateSourceActionIntegrationTest extends KernelTestCase
@ -37,28 +42,31 @@ class CreateSourceActionIntegrationTest extends KernelTestCase
*/
private $requestedActionService;
// public function setUp(): void
// {
// self::bootKernel();
// $entityManager = static::$kernel->getContainer()->get('doctrine')->getManager();
// $requestedRightService = new RequestedRightService();
// $this->requestedActionService = new RequestedActionService($requestedRightService);
// $this->requestedActionService->setActionType(ActionType::CREATE);
// $entityFormBuilderService = $this->createMock(EntityFormBuilderServiceInterface::class);
// $requestStack = $this->createMock(RequestStack::class);
// $layerRepositoryFactoryService = $this->createMock(LayerRepositoryFactoryServiceInterface::class);
// $secureRequestedRightChecker = $this->createMock(SecureRequestedRightCheckerInterface::class);
// $this->actionService = new ActionService($this->requestedActionService, $secureRequestedRightChecker, $requestStack, $layerRepositoryFactoryService, $entityFormBuilderService, $entityManager);
// $this->createSourceAction = new CreateSourceAction($this->actionService);
// }
public function setUp(): void
{
self::bootKernel();
$entityManager = static::$kernel->getContainer()
->get('doctrine')
->getManager();
$security = $this->createMock(Security::class);
$userSourceDirectorService = new UserSourceDirectorService($entityManager, $security);
$requestedRightService = new RequestedRightService();
$requestedUserService = new RequestedUserService($userSourceDirectorService, $requestedRightService);
$this->requestedActionService = new RequestedActionService($userSourceDirectorService, $requestedUserService);
$this->requestedActionService->setActionType(ActionType::CREATE);
$entityFormBuilderService = $this->createMock(RequestedEntityFormBuilderServiceInterface::class);
$requestStack = $this->createMock(RequestStack::class);
$layerRepositoryFactoryService = $this->createMock(LayerRepositoryFactoryServiceInterface::class);
$secureRequestedRightChecker = $this->createMock(SecureRequestedRightCheckerInterface::class);
$this->actionService = new ActionService($this->requestedActionService, $secureRequestedRightChecker, $requestStack, $layerRepositoryFactoryService, $entityFormBuilderService, $entityManager);
$this->createSourceAction = new CreateSourceAction($this->actionService);
}
// public function testCreateWithGuestUser(): void
// {
// $this->requestedActionService->setReciever($reciever);
// $this->assertInstanceOf(PureSourceInterface::class, $this->createSourceAction->execute());
// }
public function testCreateWithGuestUser(): void
{
$this->assertInstanceOf(PureSourceInterface::class, $this->createSourceAction->execute());
}
// public function testCreatedWithKnownUser(): void
// {
// }
// {}
}

View File

@ -7,7 +7,7 @@ use App\Domain\ActionManagement\ActionService;
use App\Domain\RequestManagement\Action\RequestedActionInterface;
use App\Domain\SecureManagement\SecureRequestedRightCheckerInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Domain\FormManagement\EntityFormBuilderServiceInterface;
use App\Domain\FormManagement\RequestedEntityFormBuilderServiceInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use App\Domain\RepositoryManagement\LayerRepositoryFactoryServiceInterface;
use App\Domain\ActionManagement\ActionServiceInterface;
@ -34,9 +34,9 @@ class ActionServiceTest extends TestCase
private $secureRequestedRightChecker;
/**
* @var EntityFormBuilderServiceInterface|MockObject
* @var RequestedEntityFormBuilderServiceInterface|MockObject
*/
private $entityFormBuilderService;
private $requestedEntityFormBuilderService;
/**
* @var RequestStack|MockObject
@ -78,11 +78,11 @@ class ActionServiceTest extends TestCase
$this->requestedAction = $this->createMock(RequestedActionInterface::class);
$this->requestedAction->method('getRequestedEntity')->willReturn($this->requestedEntity);
$this->secureRequestedRightChecker = $this->createMock(SecureRequestedRightCheckerInterface::class);
$this->entityFormBuilderService = $this->createMock(EntityFormBuilderServiceInterface::class);
$this->requestedEntityFormBuilderService = $this->createMock(RequestedEntityFormBuilderServiceInterface::class);
$this->requestStack = $this->createMock(RequestStack::class);
$this->layerRepositoryFactoryService = $this->createMock(LayerRepositoryFactoryServiceInterface::class);
$this->entityManager = $this->createMock(EntityManagerInterface::class);
$this->actionService = new ActionService($this->requestedAction, $this->secureRequestedRightChecker, $this->requestStack, $this->layerRepositoryFactoryService, $this->entityFormBuilderService, $this->entityManager);
$this->actionService = new ActionService($this->requestedAction, $this->secureRequestedRightChecker, $this->requestStack, $this->layerRepositoryFactoryService, $this->requestedEntityFormBuilderService, $this->entityManager);
}
public function testIsRequestedActionSecure(): void
@ -120,7 +120,7 @@ class ActionServiceTest extends TestCase
public function testGetForm(): void
{
$form = $this->createMock(FormBuilderInterface::class);
$this->entityFormBuilderService->method('create')->willReturn($form);
$this->requestedEntityFormBuilderService->method('create')->willReturn($form);
$result = $this->actionService->getForm();
$this->assertEquals($form, $result);
}

View File

@ -3,10 +3,11 @@
namespace tests\Unit\Domain\FormManagement;
use PHPUnit\Framework\TestCase;
use App\Domain\FormManagement\EntityFormBuilderService;
use Symfony\Component\Form\FormBuilderInterface;
use App\Domain\FormManagement\FormClassNameServiceInterface;
use App\Entity\EntityInterface;
use App\Domain\RequestManagement\Entity\RequestedEntityInterface;
use App\Entity\Source\PureSource;
use App\Domain\FormManagement\RequestedEntityFormBuilderService;
/**
* @author kevinfrantz
@ -22,10 +23,13 @@ class EntityFormBuilderServiceTest extends TestCase
$formBuilder = $this->createMock(FormBuilderInterface::class);
$formBuilder->method('create')->willReturn($expectedResult);
$formClassNameService = $this->createMock(FormClassNameServiceInterface::class);
$formClassNameService->method('getName')->willReturn('dummyNamespace');
$entityFormBuilderService = new EntityFormBuilderService($formBuilder, $formClassNameService);
$entity = $this->createMock(EntityInterface::class);
$result = $entityFormBuilderService->create($entity);
$formClassNameService->method('getClass')->willReturn('dummyNamespace');
$entityFormBuilderService = new RequestedEntityFormBuilderService($formBuilder, $formClassNameService);
$entity = new PureSource();
$entityRequested = $this->createMock(RequestedEntityInterface::class);
$entityRequested->method('hasIdentity')->willReturn(true);
$entityRequested->method('getEntity')->willReturn($entity);
$result = $entityFormBuilderService->create($entityRequested);
$this->assertEquals($expectedResult, $result);
}
}

View File

@ -13,9 +13,9 @@ class FormClassNameServiceTest extends TestCase
{
public function testGetName()
{
$entity = new PureSource();
$entityClass = PureSource::class;
$formNameService = new FormClassNameService();
$entityForm = $formNameService->getName($entity);
$entityForm = $formNameService->getClass($entityClass);
$this->assertEquals('App\\Form\\Source\\PureSourceType', $entityForm);
}
}