Skip to content

Commit

Permalink
Merge pull request #1 from victormln/1.1.0
Browse files Browse the repository at this point in the history
Refactor with some code improvements and minimal version of php7.2 re…
  • Loading branch information
victormln committed Dec 8, 2020
2 parents b990f16 + c21eec7 commit 31bb5ef
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 88 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ php:
- 7.2
- 7.3
- 7.4
- 8.0
before_script:
- composer self-update
- composer install
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
],
"require": {
"php": ">=7.1.0",
"php": ">=7.2.0|^8.0",
"league/tactician": "1.0.*"
},
"require-dev": {
Expand Down
82 changes: 38 additions & 44 deletions src/Bus.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace Victormln\LaravelTactician;

use RuntimeException;
use League\Tactician\CommandBus;
use League\Tactician\Plugins\LockingMiddleware;
use League\Tactician\Handler\CommandHandlerMiddleware;
use Victormln\LaravelTactician\Exceptions\CommandHandlerNotExists;
use League\Tactician\Exception\MissingHandlerException;
use Victormln\LaravelTactician\Locator\LocatorInterface;
use Victormln\LaravelTactician\Services\GetCommandHandlerNameService;
use League\Tactician\Handler\MethodNameInflector\MethodNameInflector;
use League\Tactician\Handler\CommandNameExtractor\CommandNameExtractor;
use Victormln\LaravelTactician\Services\GetCommandHandlerNameService;
use Victormln\LaravelTactician\Services\GetCommandHandlerService;

/**
* The default Command bus Using Tactician, this is an implementation to dispatch commands to their handlers trough a middleware stack, every class is resolved from the laravel's service container.
Expand Down Expand Up @@ -47,63 +47,63 @@ public function __construct(
*
* @param object $command Command to be dispatched
* @param array $middleware Array of middleware class name to add to the stack, they are resolved from the laravel container
* @throws CommandHandlerNotExists
* @throws MissingHandlerException|RuntimeException
* @return mixed
*/
public function dispatch($command, array $middleware = [])
public function dispatch(object $command, array $middleware = [])
{
if(!$this->handlerLocator->handlers()
|| !$this->commandHasABindedCommandHandler($command)) {
$this->bindCommandWitHisCommandHandler($command);
$fullCommandName = $this->getFullCommandNameOrFail($command);
if(!$this->handlerLocator->handlers() || !$this->commandHasABoundedCommandHandler($command)) {
$this->bindCommandWitHisCommandHandler($fullCommandName, $this->getCommandHandlerName($fullCommandName));
}

return $this->handleTheCommand($command, $middleware);
}

private function getCommandAndCommandHandlerNames($commandClass): array
/**
* @param object $commandClass
* @return string
* @throws RuntimeException
*/
private function getFullCommandNameOrFail(object $commandClass): string
{
$fullCommandName = get_class($commandClass);
if (!$fullCommandName) {
throw new RuntimeException('Invalid Command ' . $commandClass. ' given');
}

return $fullCommandName;
}

/**
* @param string $fullCommandName
* @return string
* @throws MissingHandlerException
*/
protected function getCommandHandlerName(string $fullCommandName): string
{
return (new GetCommandHandlerNameService())->execute($commandClass);
return (new GetCommandHandlerNameService())->execute($fullCommandName);
}

private function commandHasABindedCommandHandler($command): bool
private function commandHasABoundedCommandHandler(string $fullCommandName): bool
{
[$commandFullName, $commandHandlerFullName] = $this->getCommandAndCommandHandlerNames($command);
$currentCommandAndHisHandlers = $this->handlerLocator->handlers();
if(is_array($currentCommandAndHisHandlers)
&& !empty($currentCommandAndHisHandlers[$commandFullName])) {
return true;
}

return false;
return is_array($currentCommandAndHisHandlers)
&& !empty($currentCommandAndHisHandlers[$fullCommandName]);
}

private function bindCommandWitHisCommandHandler($commandClass)
private function bindCommandWitHisCommandHandler(string $fullCommandName, string $fullCommandHandlerName): void
{
[$commandFullName, $commandHandlerFullName] = $this->getCommandAndCommandHandlerNames($commandClass);

$this->addHandler($commandFullName, $commandHandlerFullName);
$this->addHandler($fullCommandName, $fullCommandHandlerName);
}

/**
* Add the Command Handler
*
* @param string $command Class name of the command
* @param string $handler Class name of the handler to be resolved from the Laravel Container
* @return mixed
*/
public function addHandler($command, $handler)
public function addHandler(string $command, string $handler): void
{
$this->handlerLocator->addHandler($handler, $command);
}

/**
* Handle the command
*
* @param $command
* @param $middleware
* @return mixed
*/
protected function handleTheCommand($command, array $middleware)
protected function handleTheCommand(object $command, array $middleware)
{
$this->bus = new CommandBus(
array_merge(
Expand All @@ -116,13 +116,7 @@ protected function handleTheCommand($command, array $middleware)
return $this->bus->handle($command);
}

/**
* Resolve the middleware stack from the laravel container
*
* @param $middleware
* @return array
*/
protected function resolveMiddleware(array $middleware)
protected function resolveMiddleware(array $middleware): array
{
$m = [];
foreach ($middleware as $class) {
Expand Down
4 changes: 2 additions & 2 deletions src/CommandBusInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface CommandBusInterface
* @param array $middleware Array of middleware class name to add to the stack, they are resolved from the laravel container they are resolved fro the laravel container
* @return mixed
*/
public function dispatch($command, array $middleware = []);
public function dispatch(object $command, array $middleware = []);

/**
* Add the Command Handler
Expand All @@ -25,5 +25,5 @@ public function dispatch($command, array $middleware = []);
* @param string $handler Class name of the handler to be resolved from the Laravel Container
* @return mixed
*/
public function addHandler($command, $handler);
public function addHandler(string $command, string $handler);
}
21 changes: 0 additions & 21 deletions src/Exceptions/CommandHandlerNotExists.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Locator/LocatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface LocatorInterface extends HandlerLocator
* @param string $handler Handler to receive class name
* @param string $commandClassName Command class e.g. "My\TaskAddedCommand"
*/
public function addHandler($handler, $commandClassName);
public function addHandler(string $handler, string $commandClassName);

/**
* Allows you to add multiple handlers at once.
Expand Down
39 changes: 22 additions & 17 deletions src/Services/GetCommandHandlerNameService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,38 @@

declare(strict_types=1);


namespace Victormln\LaravelTactician\Services;


use Victormln\LaravelTactician\Exceptions\CommandHandlerNotExists;
use League\Tactician\Exception\MissingHandlerException;

final class GetCommandHandlerNameService
{

public function execute($commandClass): array
private const HANDLER_SUFFIX = 'Handler';

/**
* @param string $fullCommandName
* @return string
* @throws MissingHandlerException
*/
public function execute(string $fullCommandName): string
{
$commandFullName = $this->getNameOfClass($commandClass);
$commandHandlerFullName = $commandFullName . 'Handler';
if (!class_exists($commandHandlerFullName)) {
throw CommandHandlerNotExists::with($commandHandlerFullName);
}
$commandHandlerName = $fullCommandName . self::HANDLER_SUFFIX;
$this->validateIfCommandHandlerExistsOrFail($commandHandlerName);

return [
$commandFullName,
$commandHandlerFullName
];
return $commandHandlerName;
}

private function getNameOfClass($commandClass): string
/**
* @param string $fullCommandHandlerName
* @return bool
* @throws MissingHandlerException
*/
private function validateIfCommandHandlerExistsOrFail(string $fullCommandHandlerName): bool
{
$reflectionCommand = new \ReflectionObject($commandClass);
if (!class_exists($fullCommandHandlerName)) {
throw MissingHandlerException::forCommand($fullCommandHandlerName);
}

return $reflectionCommand->getNamespaceName() . '\\' . $reflectionCommand->getShortName();
return true;
}
}
4 changes: 2 additions & 2 deletions tests/TestBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Victormln\LaravelTactician\Tests;

use Victormln\LaravelTactician\Exceptions\CommandHandlerNotExists;
use League\Tactician\Exception\MissingHandlerException;
use Victormln\LaravelTactician\Tests\Stubs\TestCommandArray;
use Victormln\LaravelTactician\Tests\Stubs\TestCommandInput;
use Victormln\LaravelTactician\Tests\Stubs\TestCommand;
Expand Down Expand Up @@ -57,7 +57,7 @@ public function test_it_applies_a_middleware(): void

public function test_it_throws_exception_if_input_can_not_be_mapped_to_the_command(): void
{
$this->expectException(CommandHandlerNotExists::class);
$this->expectException(MissingHandlerException::class);
$bus = app('Victormln\LaravelTactician\CommandBusInterface');
$commandHandler = $bus->dispatch(new TestCommandInput('HELLO'));
}
Expand Down

0 comments on commit 31bb5ef

Please sign in to comment.