mirror of
https://github.com/kevinveenbirkenbach/infinito.git
synced 2025-04-16 10:16:22 +02:00
Optimized ParameterManagement
This commit is contained in:
parent
d2baca0822
commit
62f953c706
@ -22,14 +22,12 @@ class OptionalGetParameterService implements OptionalGetParameterServiceInterfac
|
|||||||
/**
|
/**
|
||||||
* @param string $key
|
* @param string $key
|
||||||
*
|
*
|
||||||
|
* @deprecated
|
||||||
|
*
|
||||||
* @throws UnvalidParameterException If the parameter is not valid
|
* @throws UnvalidParameterException If the parameter is not valid
|
||||||
*/
|
*/
|
||||||
protected function validateParameter(string $key): void
|
protected function validateParameter(string $key): void
|
||||||
{
|
{
|
||||||
if (in_array($key, self::OPTIONAL_PARAMETERS)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
throw new UnvalidParameterException("Parameter <<$key>> isn't valid.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -9,13 +9,6 @@ namespace Infinito\Domain\ParameterManagement;
|
|||||||
*/
|
*/
|
||||||
interface OptionalGetParameterServiceInterface
|
interface OptionalGetParameterServiceInterface
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @deprecated
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
const VERSION_PARAMETER = 'version';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated
|
* @deprecated
|
||||||
*
|
*
|
||||||
@ -57,7 +50,6 @@ interface OptionalGetParameterServiceInterface
|
|||||||
* @var array|string[]
|
* @var array|string[]
|
||||||
*/
|
*/
|
||||||
const OPTIONAL_PARAMETERS = [
|
const OPTIONAL_PARAMETERS = [
|
||||||
self::VERSION_PARAMETER,
|
|
||||||
self::VIEW_PARAMETER,
|
self::VIEW_PARAMETER,
|
||||||
self::CLASS_PARAMETER,
|
self::CLASS_PARAMETER,
|
||||||
self::FRAME_PARAMETER,
|
self::FRAME_PARAMETER,
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
namespace Infinito\Domain\ParameterManagement\Parameter;
|
namespace Infinito\Domain\ParameterManagement\Parameter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Parameter classes shouldn't throw exceptions!
|
||||||
|
*
|
||||||
* @author kevinfrantz
|
* @author kevinfrantz
|
||||||
*/
|
*/
|
||||||
abstract class AbstractParameter implements ParameterInterface
|
abstract class AbstractParameter implements ParameterInterface
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
namespace Infinito\Domain\ParameterManagement\Parameter;
|
namespace Infinito\Domain\ParameterManagement\Parameter;
|
||||||
|
|
||||||
use Symfony\Component\Validator\Constraints as Assert;
|
use Symfony\Component\Validator\Constraints as Assert;
|
||||||
use Infinito\Exception\SetNotPossibleException;
|
use Infinito\Exception\UnvalidGetParameterException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author kevinfrantz
|
* @author kevinfrantz
|
||||||
@ -16,7 +16,7 @@ final class FrameParameter extends AbstractParameter
|
|||||||
const STANDART_VALUE = true;
|
const STANDART_VALUE = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var int|null
|
* @var bool
|
||||||
* @Assert\Type("bool")
|
* @Assert\Type("bool")
|
||||||
*/
|
*/
|
||||||
protected $value;
|
protected $value;
|
||||||
@ -28,20 +28,26 @@ final class FrameParameter extends AbstractParameter
|
|||||||
*/
|
*/
|
||||||
public function setValue($value): void
|
public function setValue($value): void
|
||||||
{
|
{
|
||||||
if (is_null($value)) {
|
$type = gettype($value);
|
||||||
//Use standart value
|
switch ($type) {
|
||||||
$this->value = self::STANDART_VALUE;
|
case 'NULL':
|
||||||
|
// Use standart value
|
||||||
|
$this->value = self::STANDART_VALUE;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
case 'boolean':
|
||||||
|
$this->value = $value;
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if (is_numeric($value)) {
|
if (is_numeric($value)) {
|
||||||
$number = (int) $value;
|
$value = (int) $value;
|
||||||
if ($number >= 0 && $number <= 1) {
|
if ($value >= 0 && $value <= 1) {
|
||||||
$this->value = (bool) $value;
|
$this->value = (bool) $value;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new SetNotPossibleException("It\'s not possible to set <<$value>> of type <<".gettype($value).'>> for class <<'.get_class().'>>. Just 0 and 1 are allowed!');
|
throw new UnvalidGetParameterException("It\'s not possible to set <<$value>> of type <<".$type.'>> for class <<'.get_class().'>>. Just 0 and 1 are allowed!');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
namespace Infinito\Domain\ParameterManagement;
|
namespace Infinito\Domain\ParameterManagement;
|
||||||
|
|
||||||
use Infinito\Exception\UnvalidParameterException;
|
|
||||||
use Symfony\Component\HttpFoundation\RequestStack;
|
use Symfony\Component\HttpFoundation\RequestStack;
|
||||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||||
|
use Infinito\Exception\UnvalidGetParameterException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author kevinfrantz
|
* @author kevinfrantz
|
||||||
@ -40,12 +40,11 @@ final class ValidGetParametersService extends OptionalGetParameterService implem
|
|||||||
*/
|
*/
|
||||||
protected function validateParameter(string $key): void
|
protected function validateParameter(string $key): void
|
||||||
{
|
{
|
||||||
parent::validateParameter($key);
|
|
||||||
$parameter = $this->parameterFactory->getParameter($key);
|
$parameter = $this->parameterFactory->getParameter($key);
|
||||||
$parameter->setValue($this->currentRequest->get($key));
|
$parameter->setValue($this->currentRequest->get($key));
|
||||||
$errors = $this->validator->validate($parameter);
|
$errors = $this->validator->validate($parameter);
|
||||||
foreach ($errors as $error) {
|
foreach ($errors as $error) {
|
||||||
throw new UnvalidParameterException("Parameter <<$key>> didn't pass the validation; Message: <<".$error->getMessage().'>> ,Value: <<'.$parameter->getValue().'>> .');
|
throw new UnvalidGetParameterException("Parameter <<$key>> didn't pass the validation; Message: <<".$error->getMessage().'>> ,Value: <<'.$parameter->getValue().'>> .');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ use Infinito\Domain\ActionManagement\ActionServiceInterface;
|
|||||||
use Infinito\Domain\ActionManagement\ActionFactoryServiceInterface;
|
use Infinito\Domain\ActionManagement\ActionFactoryServiceInterface;
|
||||||
use Infinito\Domain\TemplateManagement\TemplateNameServiceInterface;
|
use Infinito\Domain\TemplateManagement\TemplateNameServiceInterface;
|
||||||
use Infinito\Domain\ParameterManagement\ValidGetParameterServiceInterface;
|
use Infinito\Domain\ParameterManagement\ValidGetParameterServiceInterface;
|
||||||
|
use Infinito\Domain\ParameterManagement\Parameter\FrameParameter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author kevinfrantz
|
* @author kevinfrantz
|
||||||
@ -56,8 +57,8 @@ final class ViewBuilder implements ViewBuilderInterface
|
|||||||
*/
|
*/
|
||||||
private function checkLoadWithFrame(): bool
|
private function checkLoadWithFrame(): bool
|
||||||
{
|
{
|
||||||
if ($this->validGetParameterService->hasParameter(ValidGetParameterServiceInterface::FRAME_PARAMETER)) {
|
if ($this->validGetParameterService->hasParameter(FrameParameter::getKey())) {
|
||||||
return $this->validGetParameterService->getParameter(ValidGetParameterServiceInterface::FRAME_PARAMETER);
|
return $this->validGetParameterService->getParameter(FrameParameter::getKey());
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Infinito\Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thrown when an unvalid get paramter is used.
|
||||||
|
*
|
||||||
|
* @author kevinfrantz
|
||||||
|
*/
|
||||||
|
class UnvalidGetParameterException extends UnvalidParameterException
|
||||||
|
{
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace tests\Unit\Domain\ParameterManagement;
|
namespace tests\Integration\Domain\ParameterManagement;
|
||||||
|
|
||||||
use Infinito\Domain\ParameterManagement\ParameterFactory;
|
use Infinito\Domain\ParameterManagement\ParameterFactory;
|
||||||
use Infinito\Domain\ParameterManagement\ValidGetParametersService;
|
use Infinito\Domain\ParameterManagement\ValidGetParametersService;
|
||||||
@ -74,7 +74,7 @@ class ValidGetParameterServiceTest extends KernelTestCase
|
|||||||
|
|
||||||
public function testConstructor(): void
|
public function testConstructor(): void
|
||||||
{
|
{
|
||||||
$this->expectException(UnvalidParameterException::class);
|
$this->expectException(NotDefinedException::class);
|
||||||
$this->currentRequest->query->set('asdwgwe', 'adasa');
|
$this->currentRequest->query->set('asdwgwe', 'adasa');
|
||||||
new ValidGetParametersService($this->requestStack, $this->parameterFactory, $this->validator);
|
new ValidGetParametersService($this->requestStack, $this->parameterFactory, $this->validator);
|
||||||
}
|
}
|
||||||
|
@ -1,65 +0,0 @@
|
|||||||
<?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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,73 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace tests\Unit\Domain\ParameterManagement;
|
||||||
|
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpFoundation\RequestStack;
|
||||||
|
use Infinito\Domain\ParameterManagement\OptionalGetParameterServiceInterface;
|
||||||
|
use Infinito\Exception\NotDefinedException;
|
||||||
|
use Infinito\Domain\ParameterManagement\Parameter\VersionParameter;
|
||||||
|
use Infinito\Domain\ParameterManagement\ParameterFactory;
|
||||||
|
use Infinito\Domain\ParameterManagement\ValidGetParametersService;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||||
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class is a bit messed up because it is an recycled class of an other unit.
|
||||||
|
*
|
||||||
|
* @author kevinfrantz
|
||||||
|
*/
|
||||||
|
class ValidGetParameterServiceTest extends KernelTestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var Request
|
||||||
|
*/
|
||||||
|
private $currentRequest;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var RequestStack
|
||||||
|
*/
|
||||||
|
private $requestStack;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var OptionalGetParameterServiceInterface
|
||||||
|
*/
|
||||||
|
private $validGetParameterService;
|
||||||
|
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
$this->currentRequest = new Request();
|
||||||
|
$this->requestStack = $this->createMock(RequestStack::class);
|
||||||
|
$this->requestStack->method('getCurrentRequest')->willReturn($this->currentRequest);
|
||||||
|
$parameterFactory = new ParameterFactory();
|
||||||
|
self::bootKernel();
|
||||||
|
$validator = self::$container->get(ValidatorInterface::class);
|
||||||
|
$this->validGetParameterService = new ValidGetParametersService($this->requestStack, $parameterFactory, $validator);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testHasAndGetParameter(): void
|
||||||
|
{
|
||||||
|
$parameterFactory = new ParameterFactory();
|
||||||
|
foreach ($parameterFactory->getAllParameters()->getKeys() as $key) {
|
||||||
|
$this->assertFalse($this->validGetParameterService->hasParameter($key));
|
||||||
|
switch ($key) {
|
||||||
|
case VersionParameter::getKey():
|
||||||
|
$value = 1;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$value = true;
|
||||||
|
}
|
||||||
|
$this->currentRequest->query->set($key, $value);
|
||||||
|
$this->assertTrue($this->validGetParameterService->hasParameter($key));
|
||||||
|
$this->assertEquals($value, $this->validGetParameterService->getParameter($key));
|
||||||
|
}
|
||||||
|
$this->expectException(NotDefinedException::class);
|
||||||
|
$this->validGetParameterService->getParameter('12312312asdas');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSetParameterException(): void
|
||||||
|
{
|
||||||
|
$this->expectException(NotDefinedException::class);
|
||||||
|
$this->validGetParameterService->getParameter(VersionParameter::getKey());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user