Implemented ParameterFactory

This commit is contained in:
Kevin Frantz 2019-03-28 19:15:02 +01:00
parent fa7d6b0e31
commit a931d7aebc
4 changed files with 124 additions and 0 deletions

View File

@ -0,0 +1,75 @@
<?php
namespace Infinito\Domain\ParameterManagement;
use Infinito\Domain\ParameterManagement\Parameter\ParameterInterface;
use HaydenPierce\ClassFinder\ClassFinder;
use Doctrine\Common\Collections\ArrayCollection;
use PhpCollection\CollectionInterface;
use Doctrine\Migrations\Configuration\Exception\ParameterIncompatibleWithFinder;
use PhpParser\ErrorHandler\Collecting;
use Doctrine\Common\Collections\Collection;
use Infinito\Domain\ParameterManagement\Parameter\AbstractParameter;
/**
*
* @author kevinfrantz
*
*/
final class ParameterFactory implements ParameterFactoryInterface
{
/**
*
* @var string Namespace under which the parameters are stored
*/
const PARAMETER_NAMESPACE = 'Infinito\Domain\ParameterManagement\Parameter';
/**
* @var ArrayCollection|CollectionInterface|ParameterInterface[]
*/
private $parameters;
private function initPossible(string $class){
if($class === AbstractParameter::class){
return false;
}
$reflectionClass = new \ReflectionClass($class);
if($reflectionClass->isInterface()){
return false;
}
return true;
}
private function initParameters():void{
$this->parameters = new ArrayCollection();
$classFinder = new ClassFinder();
$parameterClasses = $classFinder->getClassesInNamespace(self::PARAMETER_NAMESPACE);
foreach ($parameterClasses as $parameterClass){
if($this->initPossible($parameterClass)){
$parameter = new $parameterClass();
$this->parameters->set($parameter::getKey(),$parameter);
}
}
}
public function __construct(){
$this->initParameters();
}
/**
*
* {@inheritDoc}
* @see \Infinito\Domain\ParameterManagement\ParameterFactoryInterface::getParameter()
*/
public function getParameter(string $key): ParameterInterface
{
return $this->parameters->get($key);
}
/**
* {@inheritDoc}
* @see \Infinito\Domain\ParameterManagement\ParameterFactoryInterface::getAllParameters()
*/
public function getAllParameters():Collection{
return $this->parameters;
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace Infinito\Domain\ParameterManagement;
use Infinito\Domain\ParameterManagement\Parameter\ParameterInterface;
use Doctrine\Common\Collections\Collection;
interface ParameterFactoryInterface
{
/**
*
* @param string $key
* @return ParameterInterface
*/
public function getParameter(string $key): ParameterInterface;
/**
* @return Collection|ParameterInterface[]
*/
public function getAllParameters():Collection;
}

View File

@ -14,5 +14,7 @@ class VersionParameterTest extends TestCase
{
$key = VersionParameter::getKey();
$this->assertEquals('version', $key);
$versionParameter = new VersionParameter();
$this->assertEquals($key, $versionParameter::getKey());
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace tests\Unit\Domain\ParameterManagement;
use PHPUnit\Framework\TestCase;
use Infinito\Domain\ParameterManagement\ParameterFactory;
use Infinito\Domain\ParameterManagement\Parameter\VersionParameter;
/**
*
* @author kevinfrantz
*
*/
class ParameterFactoryTest extends TestCase
{
public function testAllParameters():void{
$parameterFactory = new ParameterFactory();
$allParameters = $parameterFactory->getAllParameters();
var_dump($allParameters);
$versionParameter = $allParameters->get('version');
$this->assertInstanceOf(VersionParameter::class, $versionParameter);
$this->assertEquals($versionParameter, $parameterFactory->getParameter('version'));
}
}