Optimized SecureCRUDFactory

This commit is contained in:
Kevin Frantz 2019-01-08 15:51:28 +01:00
parent 51667ee381
commit 814fa978e0
13 changed files with 97 additions and 150 deletions

View File

@ -7,6 +7,6 @@ use App\Domain\SecureCRUDManagement\CRUD\AbstractSecureCRUD;
/** /**
* @author kevinfrantz * @author kevinfrantz
*/ */
abstract class AbstractSecureCreator extends AbstractSecureCRUD implements SecureCreatorInterface abstract class AbstractSecureCreate extends AbstractSecureCRUD implements SecureCreateInterface
{ {
} }

View File

@ -10,7 +10,7 @@ use App\Domain\SecureCRUDManagement\CRUD\SecureCRUDInterface;
* *
* @author kevinfrantz * @author kevinfrantz
*/ */
interface SecureCreatorInterface extends SecureCRUDInterface interface SecureCreateInterface extends SecureCRUDInterface
{ {
/** /**
* @return EntityInterface The created entity * @return EntityInterface The created entity

View File

@ -7,13 +7,12 @@ use App\Entity\EntityInterface;
/** /**
* @author kevinfrantz * @author kevinfrantz
*/ */
final class SecureHeredityCreator extends AbstractSecureCreator final class SecureHeredityCreate extends AbstractSecureCreate
{ {
/** /**
* @todo Implement * @todo Implement
* {@inheritdoc}
* *
* @see \App\Domain\SecureCRUDManagement\CRUD\Create\SecureCreatorInterface::create() * @return EntityInterface
*/ */
public function create(): EntityInterface public function create(): EntityInterface
{ {

View File

@ -7,7 +7,7 @@ use App\Entity\EntityInterface;
/** /**
* @author kevinfrantz * @author kevinfrantz
*/ */
final class SecureMemberCreator extends AbstractSecureCreator final class SecureMemberCreate extends AbstractSecureCreate
{ {
public function create(): EntityInterface public function create(): EntityInterface
{ {

View File

@ -7,13 +7,12 @@ use App\Entity\EntityInterface;
/** /**
* @author kevinfrantz * @author kevinfrantz
*/ */
final class SecureRightCreator extends AbstractSecureCreator final class SecureRightCreate extends AbstractSecureCreate
{ {
/** /**
* @todo Implement! * @todo Implement!
* {@inheritdoc}
* *
* @see \App\Domain\SecureCRUDManagement\CRUD\Create\SecureCreatorInterface::create() * @return EntityInterface
*/ */
public function create(): EntityInterface public function create(): EntityInterface
{ {

View File

@ -10,7 +10,7 @@ use App\Entity\Source\Primitive\Text\TextSource;
* *
* @todo Implement! * @todo Implement!
*/ */
final class SecureSourceCreator extends AbstractSecureCreator final class SecureSourceCreate extends AbstractSecureCreate
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}

View File

@ -2,27 +2,34 @@
namespace App\Domain\SecureCRUDManagement\Factory; namespace App\Domain\SecureCRUDManagement\Factory;
use App\Domain\SecureCRUDManagement\CRUD\SecureCRUDInterface;
use App\Entity\Meta\RightInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security; use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\RequestStack;
use Doctrine\ORM\EntityManagerInterface;
/** /**
* @author kevinfrantz * @author kevinfrantz
* *
* @todo Implement! * @todo Improve code performance
* @todo substitute through child classes!
*/ */
abstract class AbstractSecureCRUDFactoryService implements SecureCRUDFactoryServiceInterface final class SecureCRUDFactoryService implements SecureCRUDFactoryServiceInterface
{ {
/** /**
* @var Request * @var Request
*/ */
protected $request; private $request;
/** /**
* @var Security * @var Security
*/ */
protected $security; private $security;
/**
* @var EntityManagerInterface
*/
private $entityManager;
/** /**
* @param string $crud * @param string $crud
@ -40,7 +47,7 @@ abstract class AbstractSecureCRUDFactoryService implements SecureCRUDFactoryServ
* *
* @return string * @return string
*/ */
protected function getClassName(string $layer, string $crud): string private function getClassName(string $layer, string $crud): string
{ {
return 'Secure'.ucfirst(strtolower($layer)).$this->getCrud($crud); return 'Secure'.ucfirst(strtolower($layer)).$this->getCrud($crud);
} }
@ -50,14 +57,27 @@ abstract class AbstractSecureCRUDFactoryService implements SecureCRUDFactoryServ
* *
* @return string * @return string
*/ */
protected function getCRUDNamespace(string $layer, string $crud): string private function getCRUDNamespace(string $layer, string $crud): string
{ {
return 'App\\Domain\\SecureCRUDManagement\\CRUD\\'.$this->getCrud($crud).'\\'.$this->getClassName($layer, $crud); return 'App\\Domain\\SecureCRUDManagement\\CRUD\\'.$this->getCrud($crud).'\\'.$this->getClassName($layer, $crud);
} }
public function __construct(RequestStack $requestStack, Security $security) public function __construct(RequestStack $requestStack, Security $security, EntityManagerInterface $entityManager)
{ {
$this->request = $requestStack->getCurrentRequest(); $this->request = $requestStack->getCurrentRequest();
$this->security = $security; $this->security = $security;
$this->entityManager = $entityManager;
}
/**
* {@inheritdoc}
*
* @see \App\Domain\SecureCRUDManagement\Factory\SecureCRUDFactoryServiceInterface::create()
*/
public function create(RightInterface $requestedRight): SecureCRUDInterface
{
$namespace = $this->getCRUDNamespace($requestedRight->getLayer(), $requestedRight->getType());
return new $namespace($this->request, $this->security, $this->entityManager);
} }
} }

View File

@ -3,7 +3,7 @@
namespace App\Domain\SecureCRUDManagement\Factory; namespace App\Domain\SecureCRUDManagement\Factory;
use App\Entity\Meta\RightInterface; use App\Entity\Meta\RightInterface;
use App\Domain\SecureCRUDManagement\CRUD\Create\SecureCreatorInterface; use App\Domain\SecureCRUDManagement\CRUD\SecureCRUDInterface;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -11,7 +11,7 @@ use App\Domain\SecureCRUDManagement\CRUD\Create\SecureCreatorInterface;
interface SecureCRUDFactoryServiceInterface interface SecureCRUDFactoryServiceInterface
{ {
/** /**
* @return SecureCreatorInterface * @return SecureCRUDInterface
*/ */
public function create(RightInterface $requestedRight): SecureCreatorInterface; public function create(RightInterface $requestedRight): SecureCRUDInterface;
} }

View File

@ -1,40 +0,0 @@
<?php
namespace App\Domain\SecureCRUDManagement\Factory;
use App\Entity\Meta\RightInterface;
use App\Domain\SecureCRUDManagement\CRUD\Create\SecureCreatorInterface;
use App\DBAL\Types\Meta\Right\CRUDType;
/**
* @author kevinfrantz
*
* @todo Implement!
*/
final class SecureCreatorFactoryService extends AbstractSecureCRUDFactoryService
{
const CRUD_TYPE = CRUDType::CREATE;
/**
* @param string $layer
* @param string $crud
*
* @return string
*/
protected function getClassName(string $layer, string $crud): string
{
return 'Secure'.ucfirst(strtolower($layer)).'Creator';
}
/**
* {@inheritdoc}
*
* @see \App\Domain\SecureCRUDManagement\Factory\SecureCRUDFactoryServiceInterface::create()
*/
public function create(RightInterface $requestedRight): SecureCreatorInterface
{
$namespace = $this->getCRUDNamespace($requestedRight->getLayer(), self::CRUD_TYPE);
return new $namespace($this->request, $this->security);
}
}

View File

@ -1,10 +0,0 @@
<?php
namespace App\Domain\SecureCRUDManagement\Factory;
/**
* @author kevinfrantz
*/
interface SecureCreatorFactoryServiceInterface extends SecureCRUDFactoryServiceInterface
{
}

View File

@ -1,34 +0,0 @@
<?php
namespace tests\Unit\Domain\SecureCRUDManagement\Factory;
use PHPUnit\Framework\TestCase;
use App\Domain\SecureCRUDManagement\Factory\AbstractSecureCRUDFactoryService;
use App\Entity\Meta\RightInterface;
use App\Domain\SecureCRUDManagement\CRUD\Create\SecureCreatorInterface;
/**
* @author kevinfrantz
*/
class AbstractSecureCRUDFactoryServiceTest extends TestCase
{
public function testGetCRUDNamespace(): void
{
$abstractSecureCRUDFactoryService = new class() extends AbstractSecureCRUDFactoryService {
public function __construct()
{
}
public function publicGetCRUDNamespace(string $layer, string $crud): string
{
return $this->getCRUDNamespace($layer, $crud);
}
public function create(RightInterface $requestedRight): SecureCreatorInterface
{
}
};
$result = $abstractSecureCRUDFactoryService->publicGetCRUDNamespace('Layer', 'Crud');
$this->assertEquals('App\\Domain\\SecureCRUDManagement\\CRUD\\Crud\\SecureLayerCrud', $result);
}
}

View File

@ -0,0 +1,58 @@
<?php
namespace tests\Unit\Domain\SecureCRUDManagement\Factory;
use App\Domain\SecureCRUDManagement\Factory\SecureCRUDFactoryServiceInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Security\Core\Security;
use App\Domain\SecureCRUDManagement\Factory\SecureCRUDFactoryService;
use App\DBAL\Types\Meta\Right\LayerType;
use App\Entity\Meta\Right;
use App\Domain\SecureCRUDManagement\CRUD\SecureCRUDInterface;
use App\DBAL\Types\Meta\Right\CRUDType;
/**
* @author kevinfrantz
*/
class SecureCRUDFactoryServiceTest extends KernelTestCase
{
const EXCLUDED_TYPES = [
CRUDType::CREATE => [
LayerType::LAW,
],
CRUDType::DELETE => [
LayerType::LAW,
],
CRUDType::READ => [],
CRUDType::UPDATE => [],
];
/**
* @var SecureCRUDFactoryServiceInterface
*/
private $secureCRUDFactoryService;
public function setUp(): void
{
self::bootKernel();
$requestStack = self::$container->get('request_stack');
$security = new Security(self::$kernel->getContainer());
$entityManager = self::$container->get('doctrine.orm.default_entity_manager');
$this->secureCRUDFactoryService = new SecureCRUDFactoryService($requestStack, $security, $entityManager);
}
public function testCreate(): void
{
foreach (CRUDType::getChoices() as $crud) {
foreach (LayerType::getChoices() as $layer) {
if (!in_array($layer, self::EXCLUDED_TYPES[$crud])) {
$requestedRight = new Right();
$requestedRight->setLayer($layer);
$requestedRight->setType($crud);
$secureCreator = $this->secureCRUDFactoryService->create($requestedRight);
$this->assertInstanceOf(SecureCRUDInterface::class, $secureCreator);
}
}
}
}
}

View File

@ -1,45 +0,0 @@
<?php
namespace tests\Unit\Domain\SecureCRUDManagement\Factory;
use App\Domain\SecureCRUDManagement\Factory\SecureCreatorFactoryServiceInterface;
use App\Domain\SecureCRUDManagement\Factory\SecureCreatorFactoryService;
use Symfony\Component\Security\Core\Security;
use App\DBAL\Types\Meta\Right\LayerType;
use App\Entity\Meta\Right;
use App\Domain\SecureCRUDManagement\CRUD\Create\SecureCreatorInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* @author kevinfrantz
*/
class SecureCreatorFactoryServiceTest extends KernelTestCase
{
/**
* @var SecureCreatorFactoryServiceInterface
*/
private $secureCreatorFactoryService;
public function setUp(): void
{
self::bootKernel();
$requestStack = self::$container->get('request_stack');
$security = new Security(self::$kernel->getContainer());
$this->secureCreatorFactoryService = new SecureCreatorFactoryService($requestStack, $security);
}
public function testCreate(): void
{
$excludedTypes = [
LayerType::LAW,
];
foreach (LayerType::getChoices() as $layer) {
if (!in_array($layer, $excludedTypes)) {
$requestedRight = new Right();
$requestedRight->setLayer($layer);
$secureCreator = $this->secureCreatorFactoryService->create($requestedRight);
$this->assertInstanceOf(SecureCreatorInterface::class, $secureCreator);
}
}
}
}