Skip to content

Commit

Permalink
added validator handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ivannis committed Mar 17, 2017
1 parent 4e4f471 commit 8067219
Show file tree
Hide file tree
Showing 9 changed files with 380 additions and 21 deletions.
19 changes: 10 additions & 9 deletions Exception/NotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@ class NotFoundException extends RuntimeException
*
* @param string $type
* @param mixed $message
* @param int $code
* @param Exception|null $cause
*
* @return NotFoundException
*/
protected static function notFound($type, $message, Exception $cause = null)
protected static function notFound($type, $message, $code = 0, Exception $cause = null)
{
return new static(sprintf(
'Not found a %s for a given object of type %s',
$type,
is_object($message) ? get_class($message) : gettype($message)
), 0, $cause);
), $code, $cause);
}

/**
Expand All @@ -46,7 +47,7 @@ protected static function notFound($type, $message, Exception $cause = null)
*/
public static function nameOfMessage($object, Exception $cause = null)
{
return self::notFound('name of message', $object, $cause);
return self::notFound('name of message', $object, 1, $cause);
}

/**
Expand All @@ -57,7 +58,7 @@ public static function nameOfMessage($object, Exception $cause = null)
*/
public static function nameOfCommand($object, Exception $cause = null)
{
return self::notFound('name of command', $object, $cause);
return self::notFound('name of command', $object, 2, $cause);
}

/**
Expand All @@ -68,7 +69,7 @@ public static function nameOfCommand($object, Exception $cause = null)
*/
public static function handlerMethodNameForObject($object, Exception $cause = null)
{
return self::notFound('handler method name', $object, $cause);
return self::notFound('handler method name', $object, 3, $cause);
}

/**
Expand All @@ -79,7 +80,7 @@ public static function handlerMethodNameForObject($object, Exception $cause = nu
*/
public static function nameOfQuery($object, Exception $cause = null)
{
return self::notFound('name of query', $object, $cause);
return self::notFound('name of query', $object, 4, $cause);
}

/**
Expand All @@ -93,7 +94,7 @@ public static function handlerFor($messageName, Exception $cause = null)
return new static(sprintf(
'Not found a handler for a given message named %s',
$messageName
), 0, $cause);
), 5, $cause);
}

/**
Expand All @@ -109,7 +110,7 @@ public static function middlewareOfType($type, Exception $cause = null)
return new static(sprintf(
'Not found a middleware of type %s',
$type
), 0, $cause);
), 6, $cause);
}

/**
Expand All @@ -121,6 +122,6 @@ public static function middlewareOfType($type, Exception $cause = null)
*/
public static function methodForObject($object, $method, Exception $cause = null)
{
return self::notFound('method with name `'.$method.'`', $object, $cause);
return self::notFound('method with name `'.$method.'`', $object, 7, $cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* This file is part of the Cubiche package.
*
* Copyright (c) Cubiche
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Cubiche\Core\Bus\Middlewares\Handler\Resolver\HandlerMethodName;

/**
* MethodWithShortObjectNameAndSuffixResolver class.
*
* Examples:
* - Cubiche\Application\Command\ChangeTitleCommand => $fooTaskCommandHandler->changeTitleHandler()
* - Cubiche\Application\Query\PostListQuery => $postListQueryHandler->postListHandler()
*
* @author Ivannis Suárez Jerez <ivannis.suarez@gmail.com>
*/
class MethodWithShortObjectNameAndSuffixResolver extends MethodWithObjectNameResolver
{
/**
* @var string
*/
private $suffixToRemove;

/**
* @var string
*/
private $suffixToAdd;

/**
* @var int
*/
private $suffixLength;

/**
* @param string $suffixToRemove The string to remove from end of each class name
* @param string $suffixToAdd The string to add to the end of each class name
*/
public function __construct($suffixToRemove = 'Message', $suffixToAdd = 'Handler')
{
$this->suffixToRemove = $suffixToRemove;
$this->suffixToAdd = $suffixToAdd;
$this->suffixLength = strlen($suffixToRemove);
}

/**
* {@inheritdoc}
*/
public function resolve($className)
{
$methodName = parent::resolve($className);
if (substr($methodName, $this->suffixLength * -1) !== $this->suffixToRemove) {
throw new \Exception(sprintf('Invalid suffix %s', $this->suffixToRemove));
}

return substr($methodName, 0, strlen($methodName) - $this->suffixLength).$this->suffixToAdd;
}
}
19 changes: 17 additions & 2 deletions Middlewares/Validator/ValidatorMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,40 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Cubiche\Core\Bus\Middlewares\Validator;

use Cubiche\Core\Bus\Middlewares\MiddlewareInterface;
use Cubiche\Core\Bus\Exception\NotFoundException;
use Cubiche\Core\Bus\Middlewares\Handler\MessageHandlerMiddleware;
use Cubiche\Core\Validator\Validator;

/**
* ValidatorMiddleware class.
*
* @author Ivannis Suárez Jerez <ivannis.suarez@gmail.com>
*/
class ValidatorMiddleware implements MiddlewareInterface
class ValidatorMiddleware extends MessageHandlerMiddleware
{
/**
* {@inheritdoc}
*/
public function handle($message, callable $next)
{
$this->ensureTypeOfMessage($message);
Validator::assert($message);

try {
$handler = $this->handlerClassResolver->resolve($message);

$handler($message);
} catch (NotFoundException $e) {
// NotFoundException::handlerMethodNameForObject
// NotFoundException::methodForObject
if ($e->getCode() == 3 || $e->getCode() == 7) {
throw $e;
}
}

$next($message);
}
}
66 changes: 66 additions & 0 deletions Tests/Fixtures/Message/RemoveUserMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/**
* This file is part of the Cubiche package.
*
* Copyright (c) Cubiche
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Cubiche\Core\Bus\Tests\Fixtures\Message;

use Cubiche\Core\Bus\MessageInterface;
use Cubiche\Core\Validator\Assert;
use Cubiche\Core\Validator\Mapping\ClassMetadata;

/**
* RemoveUserMessage class.
*
* @author Ivannis Suárez Jerez <ivannis.suarez@gmail.com>
*/
class RemoveUserMessage implements MessageInterface
{
/**
* @var string
*/
protected $email;

/**
* RemoveUserMessage constructor.
*
* @param $email
*/
public function __construct($email)
{
$this->setEmail($email);
}

/**
* @return string
*/
public function email()
{
return $this->email;
}

/**
* @param string $email
*/
public function setEmail($email)
{
$this->email = $email;
}

/**
* {@inheritdoc}
*/
public static function loadValidatorMetadata(ClassMetadata $classMetadata)
{
$classMetadata->addPropertyConstraint(
'email',
Assert::email()
);
}
}
44 changes: 44 additions & 0 deletions Tests/Fixtures/Message/UserMessageValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* This file is part of the Cubiche package.
*
* Copyright (c) Cubiche
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Cubiche\Core\Bus\Tests\Fixtures\Message;

use Cubiche\Core\Validator\Assert;
use Cubiche\Core\Validator\Exception\ValidationException;
use Cubiche\Core\Validator\Validator;

/**
* UserMessageValidator class.
*
* @author Ivannis Suárez Jerez <ivannis.suarez@gmail.com>
*/
class UserMessageValidator
{
/**
* @param LoginUserMessage $event
*
* @return bool
*/
public function loginUserValidator(LoginUserMessage $event)
{
Validator::assert($event->email(), Assert::email()->contains('gmail.com'));
}

/**
* @param LogoutUserMessage $event
*
* @return bool
*/
public function logoutUserValidator(LogoutUserMessage $event)
{
throw new ValidationException('The email is invalid');
}
}
14 changes: 13 additions & 1 deletion Tests/Units/Exception/NotFoundExceptionTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testNameOfMessage()
->object($exception)
->isInstanceOf(NotFoundException::class)
->integer($exception->getCode())
->isEqualTo(0)
->isEqualTo(1)
->object($exception->getPrevious())
->isIdenticalTo($cause)
;
Expand All @@ -59,6 +59,8 @@ public function testMethodNameForObject()
->then()
->variable($exception->getPrevious())
->isNull()
->integer($exception->getCode())
->isEqualTo(3)
;
}

Expand All @@ -72,6 +74,8 @@ public function testHandlerFor()
->then()
->variable($exception->getPrevious())
->isNull()
->integer($exception->getCode())
->isEqualTo(5)
;
}

Expand All @@ -85,6 +89,8 @@ public function testMiddlewareOfType()
->then()
->variable($exception->getPrevious())
->isNull()
->integer($exception->getCode())
->isEqualTo(6)
;
}

Expand All @@ -98,6 +104,8 @@ public function testNameOfCommand()
->then()
->variable($exception->getPrevious())
->isNull()
->integer($exception->getCode())
->isEqualTo(2)
;
}

Expand All @@ -111,6 +119,8 @@ public function testNameOfQuery()
->then()
->variable($exception->getPrevious())
->isNull()
->integer($exception->getCode())
->isEqualTo(4)
;
}

Expand All @@ -124,6 +134,8 @@ public function testMethodForObject()
->then()
->variable($exception->getPrevious())
->isNull()
->integer($exception->getCode())
->isEqualTo(7)
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Cubiche\Core\Bus\Exception\NotFoundException;
use Cubiche\Core\Bus\Middlewares\Handler\Resolver\HandlerMethodName\ChainResolver;
use Cubiche\Core\Bus\Middlewares\Handler\Resolver\HandlerMethodName\DefaultResolver;
use Cubiche\Core\Bus\Middlewares\Handler\Resolver\HandlerMethodName\MethodWithShortObjectNameAndSuffixResolver;
use Cubiche\Core\Bus\Middlewares\Handler\Resolver\HandlerMethodName\MethodWithShortObjectNameResolver;
use Cubiche\Core\Bus\Tests\Fixtures\Message\LoginUserMessage;
use Cubiche\Core\Bus\Tests\Fixtures\Message\LogoutUserMessage;
Expand All @@ -34,9 +35,10 @@ public function testResolve()
{
$this
->given($resolver1 = new InvalidHandlerMethodNameResolver())
->given($resolver2 = new MethodWithShortObjectNameResolver('Message'))
->and($resolver3 = new DefaultResolver())
->and($resolver = new ChainResolver([$resolver1, $resolver2, $resolver3]))
->and($resolver2 = new MethodWithShortObjectNameAndSuffixResolver('Listener'))
->and($resolver3 = new MethodWithShortObjectNameResolver('Message'))
->and($resolver4 = new DefaultResolver())
->and($resolver = new ChainResolver([$resolver1, $resolver2, $resolver3, $resolver4]))
->when($result = $resolver->resolve(LoginUserMessage::class))
->then()
->string($result)
Expand Down

0 comments on commit 8067219

Please sign in to comment.