mirror of
https://github.com/kevinveenbirkenbach/infinito.git
synced 2024-12-04 23:17:19 +01:00
Implemented OptionalGetParameterService
This commit is contained in:
parent
7f3f43156a
commit
b6819b6db0
@ -93,17 +93,17 @@ final class MVCRoutineService implements MVCRoutineServiceInterface
|
||||
if (!$this->actionType) {
|
||||
if ($this->requestedActionService->hasRequestedEntity() && $this->requestedActionService->getRequestedEntity()->hasIdentity()) {
|
||||
//READ VIEW
|
||||
$this->requestedActionService->setActionType(ActionType::READ);
|
||||
// $this->requestedActionService->setActionType(ActionType::READ);
|
||||
if ($this->secureRequestedRightCheckerService->check($this->requestedActionService)) {
|
||||
$read = $this->actionHandlerService->handle();
|
||||
$this->actionTemplateDataStore->setData(ActionType::READ, $read);
|
||||
}
|
||||
$this->requestedActionService->setActionType(ActionType::UPDATE);
|
||||
// $this->requestedActionService->setActionType(ActionType::UPDATE);
|
||||
//UPDATE VIEW
|
||||
if ($this->secureRequestedRightCheckerService->check($this->requestedActionService)) {
|
||||
$updateForm = $this->requestedActionFormBuilderService->createByService()->getForm()->createView();
|
||||
$this->actionTemplateDataStore->setData(ActionType::UPDATE, $updateForm);
|
||||
}
|
||||
// if ($this->secureRequestedRightCheckerService->check($this->requestedActionService)) {
|
||||
// $updateForm = $this->requestedActionFormBuilderService->createByService()->getForm()->createView();
|
||||
// $this->actionTemplateDataStore->setData(ActionType::UPDATE, $updateForm);
|
||||
// }
|
||||
//DELETE VIEW
|
||||
//EXECUTE VIEW
|
||||
} else {
|
||||
|
@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace Infinito\Domain\ParameterManagement;
|
||||
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Infinito\Exception\UnvalidParameterException;
|
||||
use Infinito\Exception\NotDefinedException;
|
||||
|
||||
/**
|
||||
* @author kevinfrantz
|
||||
*/
|
||||
final class OptionalGetParameterService implements OptionalGetParameterServiceInterface
|
||||
{
|
||||
/**
|
||||
* @var Request
|
||||
*/
|
||||
private $currentRequest;
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
*
|
||||
* @throws UnvalidParameterException If the parameter is not valid
|
||||
*/
|
||||
private function validateParameter(string $key): void
|
||||
{
|
||||
if (in_array($key, self::OPTIONAL_PARAMETERS)) {
|
||||
return;
|
||||
}
|
||||
throw new UnvalidParameterException("Parameter <<$key>> isn't valid.");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RequestStack $requestStack
|
||||
*/
|
||||
private function setCurrentRequest(RequestStack $requestStack): void
|
||||
{
|
||||
$this->currentRequest = $requestStack->getCurrentRequest();
|
||||
}
|
||||
|
||||
private function validateCurrentRequestKeys(): void
|
||||
{
|
||||
foreach ($this->currentRequest->query->keys() as $key) {
|
||||
$this->validateParameter($key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RequestStack $requestStack
|
||||
*/
|
||||
public function __construct(RequestStack $requestStack)
|
||||
{
|
||||
$this->setCurrentRequest($requestStack);
|
||||
$this->validateCurrentRequestKeys();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \Infinito\Domain\ParameterManagement\OptionalGetParameterServiceInterface::hasParameter()
|
||||
*/
|
||||
public function hasParameter(string $key): bool
|
||||
{
|
||||
$this->validateParameter($key);
|
||||
|
||||
return $this->currentRequest->query->has($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \Infinito\Domain\ParameterManagement\OptionalGetParameterServiceInterface::getParameter()
|
||||
*/
|
||||
public function getParameter(string $key)
|
||||
{
|
||||
$this->validateParameter($key);
|
||||
if ($this->hasParameter($key)) {
|
||||
return $this->currentRequest->get($key);
|
||||
}
|
||||
throw new NotDefinedException("The parameter <<$key>> is not defined!");
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace Infinito\Domain\ParameterManagement;
|
||||
|
||||
/**
|
||||
* This interface offers a service to manage all optional get parameters.
|
||||
*
|
||||
* @author kevinfrantz
|
||||
*/
|
||||
interface OptionalGetParameterServiceInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
const VERSION_PARAMETER = 'version';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
const EXECUTE_PARAMETER = 'execute';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
const VIEW_PARAMETER = 'view';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
const CLASS_PARAMETER = 'class';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
const FRAME_PARAMETER = 'frame';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
const SCHEMA_PARAMETER = 'schema';
|
||||
|
||||
/**
|
||||
* @var array|string[]
|
||||
*/
|
||||
const OPTIONAL_PARAMETERS = [
|
||||
self::VERSION_PARAMETER,
|
||||
self::VIEW_PARAMETER,
|
||||
self::CLASS_PARAMETER,
|
||||
self::FRAME_PARAMETER,
|
||||
self::SCHEMA_PARAMETER,
|
||||
self::EXECUTE_PARAMETER,
|
||||
];
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
*
|
||||
* @return bool True if the version parameter in the request is set
|
||||
*/
|
||||
public function hasParameter(string $key): bool;
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getParameter(string $key);
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Infinito\Exception;
|
||||
|
||||
/**
|
||||
* @author kevinfrantz
|
||||
*/
|
||||
class UnvalidParameterException extends \Exception
|
||||
{
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace tests\Unit\Domain\ParameterManagement;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Infinito\Domain\ParameterManagement\OptionalGetParameterServiceInterface;
|
||||
use Infinito\Domain\ParameterManagement\OptionalGetParameterService;
|
||||
use Infinito\Exception\UnvalidParameterException;
|
||||
use Infinito\Exception\NotDefinedException;
|
||||
|
||||
/**
|
||||
* @author kevinfrantz
|
||||
*/
|
||||
class OptionalGetParameterServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var Request
|
||||
*/
|
||||
private $currentRequest;
|
||||
|
||||
/**
|
||||
* @var RequestStack
|
||||
*/
|
||||
private $requestStack;
|
||||
|
||||
/**
|
||||
* @var OptionalGetParameterServiceInterface
|
||||
*/
|
||||
private $optionalGetParameterService;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->currentRequest = new Request();
|
||||
$this->requestStack = $this->createMock(RequestStack::class);
|
||||
$this->requestStack->method('getCurrentRequest')->willReturn($this->currentRequest);
|
||||
$this->optionalGetParameterService = new OptionalGetParameterService($this->requestStack);
|
||||
}
|
||||
|
||||
public function testConstructor(): void
|
||||
{
|
||||
$this->expectException(UnvalidParameterException::class);
|
||||
$this->currentRequest->query->set('asdwgwe', 'adasa');
|
||||
new OptionalGetParameterService($this->requestStack);
|
||||
}
|
||||
|
||||
public function testHasAndGetParameter(): void
|
||||
{
|
||||
foreach (OptionalGetParameterServiceInterface::OPTIONAL_PARAMETERS as $key) {
|
||||
$this->assertFalse($this->optionalGetParameterService->hasParameter($key));
|
||||
$this->currentRequest->query->set($key, 'adasa');
|
||||
$this->assertTrue($this->optionalGetParameterService->hasParameter($key));
|
||||
$this->assertEquals('adasa', $this->optionalGetParameterService->getParameter($key));
|
||||
}
|
||||
$this->expectException(UnvalidParameterException::class);
|
||||
$this->optionalGetParameterService->getParameter('12312312asdas');
|
||||
}
|
||||
|
||||
public function testSetParameterException(): void
|
||||
{
|
||||
$this->expectException(NotDefinedException::class);
|
||||
$this->optionalGetParameterService->getParameter(OptionalGetParameterServiceInterface::VERSION_PARAMETER);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user