Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mikey179 committed Aug 14, 2014
1 parent 11940e8 commit f63b142
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 189 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
* @package stubbles\input
*/
namespace stubbles\input\broker;
use stubbles\input\Request;
use stubbles\lang\reflect\annotation\Annotation;
/**
* Map which contains all single parameter brokers.
*
* @Singleton
* @internal
*/
class ParamBrokerMap
class ParamBrokers
{
/**
* list of build in param brokers
Expand All @@ -33,18 +36,18 @@ class ParamBrokerMap
*/
public function __construct()
{
$this->paramBroker = self::getBuildInParamBroker();
$this->paramBroker = self::buildIn();
}

/**
* sets map of param brokers
* adds map of param brokers
*
* @param \stubbles\input\broker\param\ParamBroker[] $paramBrokers
* @return \stubbles\input\broker\param\ParamBrokerMap
* @Inject(optional=true)
* @Map(stubbles\input\broker\param\ParamBroker.class)
*/
public function setParamBrokers(array $paramBrokers)
public function addParamBrokers(array $paramBrokers)
{
foreach ($paramBrokers as $key => $paramBroker) {
$this->paramBroker[strtolower($key)] = $paramBroker;
Expand All @@ -53,14 +56,27 @@ public function setParamBrokers(array $paramBrokers)
return $this;
}

/**
* retrieves value defined by annotation from request
*
* @param \stubbles\input\Request $request instance to handle value with
* @param \stubbles\lang\reflect\annotation\Annotation $annotation annotation which contains request param metadata
* @return mixed
*/
public function procure(Request $request, Annotation $annotation)
{
return $this->paramBroker($annotation->getAnnotationName())
->procure($request, $annotation);
}

/**
* retrieves param broker for given key
*
* @param string $key
* @return \stubbles\input\broker\param\ParamBroker
* @throws \RuntimeException
*/
public function getBroker($key)
public function paramBroker($key)
{
if (isset($this->paramBroker[strtolower($key)])) {
return $this->paramBroker[strtolower($key)];
Expand All @@ -74,7 +90,7 @@ public function getBroker($key)
*
* @return \stubbles\input\broker\param\ParamBroker[]
*/
public static function getBuildInParamBroker()
public static function buildIn()
{
if (null === self::$buildInParamBroker) {
self::$buildInParamBroker = ['array' => new param\ArrayParamBroker(),
Expand Down
34 changes: 10 additions & 24 deletions src/main/php/broker/RequestBroker.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ class RequestBroker
/**
* factory to create filters with
*
* @type \stubbles\input\broker\ParamBrokerMap
* @type \stubbles\input\broker\ParamBrokers
*/
private $paramBrokerMap;
private $paramBrokers;

/**
* constructor
*
* @param \stubbles\input\broker\RequestBrokerMethods $brokerMethods
* @param \stubbles\input\broker\ParamBrokerMap $paramBrokerMap
* @param \stubbles\input\broker\ParamBrokers $paramBrokers
* @Inject
*/
public function __construct(RequestBrokerMethods $brokerMethods, ParamBrokerMap $paramBrokerMap)
public function __construct(RequestBrokerMethods $brokerMethods, ParamBrokers $paramBrokers)
{
$this->brokerMethods = $brokerMethods;
$this->paramBrokerMap = $paramBrokerMap;
$this->brokerMethods = $brokerMethods;
$this->paramBrokers = $paramBrokers;
}

/**
Expand All @@ -51,37 +51,23 @@ public function __construct(RequestBrokerMethods $brokerMethods, ParamBrokerMap
*/
public function procure(Request $request, $object, $group = null)
{
foreach ($this->brokerMethods->get($object, $group) as $refMethod) {
$requestAnnotation = $refMethod->getAnnotation('Request');
$value = $this->paramBrokerMap->getBroker($requestAnnotation->getAnnotationName())
->procure($request, $requestAnnotation);
foreach ($this->brokerMethods->of($object, $group) as $refMethod) {
$value = $this->paramBrokers->procure($request, $refMethod->annotation('Request'));
if (null !== $value) {
$refMethod->invoke($object, $value);
}
}
}

/**
* returns all methods of given instance which are applicable for brokerage
*
* @param object $object
* @param string $group restrict list to given group
* @return \stubbles\lang\reflect\ReflectionMethod[]
*/
public function getMethods($object, $group = null)
{
return $this->brokerMethods->get($object, $group);
}

/**
* returns a list of all request annotations on given object
*
* @param object $object
* @param string $group restrict list to given group
* @return \stubbles\lang\reflect\annotation\Annotation[]
*/
public function getAnnotations($object, $group = null)
public function annotationsFor($object, $group = null)
{
return $this->brokerMethods->getAnnotations($object, $group);
return $this->brokerMethods->annotationsFor($object, $group);
}
}
20 changes: 4 additions & 16 deletions src/main/php/broker/RequestBrokerFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public function __construct(Request $request, RequestBroker $requestBroker, Para
*
* @param object $object
* @param string $group
* @param \Closure $write function to call when errors should be processed
* @param callable $write function to call when errors should be processed
*/
public function procure($object, $group = null, \Closure $write = null)
public function procure($object, $group = null, callable $write = null)
{
$this->requestBroker->procure($this->request, $object, $group);
if (null === $write || !$this->request->paramErrors()->exist()) {
Expand All @@ -73,27 +73,15 @@ public function procure($object, $group = null, \Closure $write = null)
}
}

/**
* returns all methods of given instance which are applicable for brokerage
*
* @param object $object
* @param string $group restrict list to given group
* @return \stubbles\lang\reflect\ReflectionMethod[]
*/
public function getMethods($object, $group = null)
{
return $this->requestBroker->getMethods($object, $group);
}

/**
* returns a list of all request annotations on given object
*
* @param object $object
* @param string $group restrict list to given group
* @return \stubbles\lang\reflect\annotation\Annotation[]
*/
public function getAnnotations($object, $group = null)
public function annotationsFor($object, $group = null)
{
return $this->requestBroker->getAnnotations($object, $group);
return $this->requestBroker->annotationsFor($object, $group);
}
}
22 changes: 12 additions & 10 deletions src/main/php/broker/RequestBrokerMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,32 @@
* @package stubbles\input
*/
namespace stubbles\input\broker;
use stubbles\lang;
use stubbles\lang\reflect\ReflectionMethod;
use stubbles\lang\reflect\matcher\MethodMatcher;
/**
* Provides access to methods applicable for brokerage.
*
* @Singleton
* @internal
*/
class RequestBrokerMethods implements MethodMatcher
{
/**
* returns all methods of given instance which are applicable for brokerage
*
* @param object|string $object
* @param string $group restrict list to given group
* @param string $group restrict list to given group
* @return \stubbles\lang\reflect\ReflectionMethod[]
* @throws \InvalidArgumentException
*/
public function get($object, $group = null)
public function of($object, $group = null)
{
if (!is_object($object) && !is_string($object)) {
throw new \InvalidArgumentException('Parameter $object must be a concrete object instance or class name.');
throw new \InvalidArgumentException('Parameter $object must be an object instance or a class name');
}

$refClass = \stubbles\lang\reflect($object);
$refClass = lang\reflect($object);
$methods = $refClass->getMethodsByMatcher($this);
if (empty($group)) {
return $methods;
Expand All @@ -40,7 +42,7 @@ public function get($object, $group = null)
return array_filter($methods,
function(ReflectionMethod $method) use ($group)
{
return $method->getAnnotation('Request')->getGroup() === $group;
return $method->annotation('Request')->group() === $group;
}
);
}
Expand All @@ -52,12 +54,12 @@ function(ReflectionMethod $method) use ($group)
* @param string $group restrict list to given group
* @return \stubbles\lang\reflect\annotation\Annotation[]
*/
public function getAnnotations($object, $group = null)
public function annotationsFor($object, $group = null)
{
$annotations = [];
foreach ($this->get($object, $group) as $method) {
foreach ($this->of($object, $group) as $method) {
/* @var $method ReflectionMethod */
$annotations[] = $method->getAnnotation('Request');
$annotations[] = $method->annotation('Request');
}

return $annotations;
Expand All @@ -71,11 +73,11 @@ public function getAnnotations($object, $group = null)
*/
public function matchesMethod(\ReflectionMethod $method)
{
if ($method->isPublic() === false || $method->isStatic() === true) {
if (!$method->isPublic() || $method->isStatic()) {
return false;
}

if ($method->isConstructor() === true || $method->isDestructor() === true) {
if ($method->isConstructor() || $method->isDestructor()) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
*/
namespace stubbles\input\broker;
use stubbles\lang;
use stubbles\lang\reflect\annotation\Annotation;
/**
* Tests for stubbles\input\broker\ParamBrokerMap.
*
* @group broker
* @group broker_core
*/
class ParamBrokerMapTest extends \PHPUnit_Framework_TestCase
class ParamBrokersTest extends \PHPUnit_Framework_TestCase
{
/**
* instance to test
Expand All @@ -29,23 +30,25 @@ class ParamBrokerMapTest extends \PHPUnit_Framework_TestCase
*/
public function setUp()
{
$this->paramBrokerMap = new ParamBrokerMap();
$this->paramBrokerMap = new ParamBrokers();
}

/**
* @test
*/
public function annotationsPresentOnClass()
{
$this->assertTrue(lang\reflect($this->paramBrokerMap)->hasAnnotation('Singleton'));
$this->assertTrue(
lang\reflect($this->paramBrokerMap)->hasAnnotation('Singleton')
);
}

/**
* @test
*/
public function annotationsPresentOnSetParamBrokersMethod()
public function annotationsPresentOnAddParamBrokersMethod()
{
$setParamBrokers = lang\reflect($this->paramBrokerMap, 'setParamBrokers');
$setParamBrokers = lang\reflect($this->paramBrokerMap, 'addParamBrokers');
$this->assertTrue($setParamBrokers->hasAnnotation('Inject'));
$this->assertTrue($setParamBrokers->getAnnotation('Inject')->isOptional());
$this->assertTrue($setParamBrokers->hasAnnotation('Map'));
Expand All @@ -59,7 +62,7 @@ public function annotationsPresentOnSetParamBrokersMethod()
public function getDefaultBrokerList()
{
$defaultBroker = [];
foreach (ParamBrokerMap::getBuildInParamBroker() as $name => $paramBroker) {
foreach (ParamBrokers::buildIn() as $name => $paramBroker) {
$defaultBroker[] = [$name, get_class($paramBroker)];
}

Expand Down Expand Up @@ -101,7 +104,7 @@ private function getBcDefaultBrokerList()
public function returnsBroker($key, $brokerClass)
{
$this->assertInstanceOf($brokerClass,
$this->paramBrokerMap->getBroker($key)
$this->paramBrokerMap->paramBroker($key)
);
}

Expand All @@ -114,29 +117,32 @@ public function returnsBroker($key, $brokerClass)
public function returnsBrokerWithLowerCaseKey($key, $brokerClass)
{
$this->assertInstanceOf($brokerClass,
$this->paramBrokerMap->getBroker(strtolower($key))
$this->paramBrokerMap->paramBroker(strtolower($key))
);
}

/**
* @test
* @expectedException RuntimeException
*/
public function getUnknownBrokerThrowsRuntimeException()
public function procureUnknownTypeThrowsRuntimeException()
{
$this->paramBrokerMap->getBroker('doesNotExist');
$this->paramBrokerMap->procure(
$this->getMock('stubbles\input\Request'),
new Annotation('doesNotExist', 'foo')
);
}

/**
* @test
* @dataProvider getDefaultBrokerList
*/
public function settingBrokersDoesNotOverrideDefaultBrokers($key, $brokerClass)
public function addingBrokersDoesNotOverrideDefaultBrokers($key, $brokerClass)
{
$mockParamBroker = $this->getMock('stubbles\input\broker\param\ParamBroker');
$this->assertInstanceOf($brokerClass,
$this->paramBrokerMap->setParamBrokers(['Mock' => $mockParamBroker])
->getBroker($key)
$this->paramBrokerMap->addParamBrokers(['Mock' => $mockParamBroker])
->paramBroker($key)
);
}

Expand All @@ -147,8 +153,8 @@ public function returnsAddedBroker()
{
$mockParamBroker = $this->getMock('stubbles\input\broker\param\ParamBroker');
$this->assertSame($mockParamBroker,
$this->paramBrokerMap->setParamBrokers(['Mock' => $mockParamBroker])
->getBroker('Mock')
$this->paramBrokerMap->addParamBrokers(['Mock' => $mockParamBroker])
->paramBroker('Mock')
);
}

Expand All @@ -159,8 +165,8 @@ public function canOverwriteDefaultBroker()
{
$mockParamBroker = $this->getMock('stubbles\input\broker\param\ParamBroker');
$this->assertSame($mockParamBroker,
$this->paramBrokerMap->setParamBrokers(['string' => $mockParamBroker])
->getBroker('string')
$this->paramBrokerMap->addParamBrokers(['string' => $mockParamBroker])
->paramBroker('string')
);
}
}
Loading

0 comments on commit f63b142

Please sign in to comment.