-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding more organization in code and improvement use
- Loading branch information
1 parent
175e921
commit 55a1883
Showing
14 changed files
with
287 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DersonSena\Yii2Tactician\ClassName; | ||
|
||
interface ClassNameInflector | ||
{ | ||
public function getClassName(string $commandClassName): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DersonSena\Yii2Tactician\ClassName; | ||
|
||
final class Suffix implements ClassNameInflector | ||
{ | ||
private string $suffix; | ||
|
||
public function __construct(string $suffix) | ||
{ | ||
$this->suffix = $suffix; | ||
} | ||
|
||
public function getClassName(string $commandClassName): string | ||
{ | ||
// Command class name without "Command" + Suffix (most of time "Handler") | ||
return str_replace('Command', '', $commandClassName) . $this->suffix; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DersonSena\Yii2Tactician; | ||
|
||
use League\Tactician\Middleware; | ||
use yii\di\Container; | ||
|
||
final class CommandHandlerMiddleware implements Middleware | ||
{ | ||
private Container $container; | ||
private CommandToHandlerMapping $mapping; | ||
|
||
public function __construct(Container $container, CommandToHandlerMapping $mapping) | ||
{ | ||
$this->container = $container; | ||
$this->mapping = $mapping; | ||
} | ||
|
||
public function execute($command, callable $next) | ||
{ | ||
$methodToCall = $this->mapping->mapCommandToHandler(get_class($command)); | ||
$handler = $this->container->get($methodToCall->getClassName()); | ||
|
||
return $handler->{$methodToCall->getMethodName()}($command); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace DersonSena\Yii2Tactician; | ||
|
||
use DersonSena\Yii2Tactician\Exceptions\FailedToMapCommand; | ||
|
||
interface CommandToHandlerMapping | ||
{ | ||
/** | ||
* @throws FailedToMapCommand | ||
*/ | ||
public function mapCommandToHandler(string $commandFQCN): MethodToCall; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DersonSena\Yii2Tactician\Exceptions; | ||
|
||
use Exception; | ||
|
||
final class FailedToMapCommand extends Exception | ||
{ | ||
public static function className(string $commandClassName): self | ||
{ | ||
return new static('Failed to map the class name for command ' . $commandClassName); | ||
} | ||
|
||
public static function methodName(string $commandClassName): self | ||
{ | ||
return new static('Failed to map the method name for command ' . $commandClassName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DersonSena\Yii2Tactician\Exceptions; | ||
|
||
use BadMethodCallException; | ||
use League\Tactician\Exception\Exception; | ||
|
||
final class MethodDoesNotExist extends BadMethodCallException implements Exception | ||
{ | ||
private string $className; | ||
private string $methodName; | ||
|
||
private function __construct(string $message, string $className, string $methodName) | ||
{ | ||
parent::__construct($message); | ||
$this->className = $className; | ||
$this->methodName = $methodName; | ||
} | ||
|
||
public static function on(string $className, string $methodName): self | ||
{ | ||
return new self( | ||
'The handler method ' . $className . '::' . $methodName . ' does not exist. Please check your Tactician ' . | ||
'mapping configuration or check verify that ' . $methodName . ' is actually declared in . ' . $className, | ||
$className, | ||
$methodName | ||
); | ||
} | ||
|
||
public function getClassName(): string | ||
{ | ||
return $this->className; | ||
} | ||
|
||
public function getMethodName(): string | ||
{ | ||
return $this->methodName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DersonSena\Yii2Tactician\Exceptions; | ||
|
||
use Exception; | ||
use Throwable; | ||
|
||
final class MissingHandleMethodException extends Exception | ||
{ | ||
public function __construct($code = 0, Throwable $previous = null) | ||
{ | ||
$message = "You must have a 'handle' method in your Handler Class"; | ||
parent::__construct($message, $code, $previous); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DersonSena\Yii2Tactician; | ||
|
||
use DersonSena\Yii2Tactician\ClassName\ClassNameInflector; | ||
use DersonSena\Yii2Tactician\MethodName\MethodNameInflector; | ||
|
||
final class MapByNamingConvention implements CommandToHandlerMapping | ||
{ | ||
private ClassNameInflector $classNameInflector; | ||
private MethodNameInflector $methodNameInflector; | ||
|
||
public function __construct( | ||
ClassNameInflector $classNameInflector, | ||
MethodNameInflector $methodNameInflector | ||
) { | ||
$this->classNameInflector = $classNameInflector; | ||
$this->methodNameInflector = $methodNameInflector; | ||
} | ||
|
||
public function mapCommandToHandler(string $commandFQCN): MethodToCall | ||
{ | ||
return new MethodToCall( | ||
$this->classNameInflector->getClassName($commandFQCN), | ||
$this->methodNameInflector->getMethodName($commandFQCN) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DersonSena\Yii2Tactician\MethodName; | ||
|
||
final class Handle implements MethodNameInflector | ||
{ | ||
public function getMethodName(string $commandClassName): string | ||
{ | ||
return 'handle'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DersonSena\Yii2Tactician\MethodName; | ||
|
||
interface MethodNameInflector | ||
{ | ||
public function getMethodName(string $commandClassName): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DersonSena\Yii2Tactician; | ||
|
||
use DersonSena\Yii2Tactician\Exceptions\MethodDoesNotExist; | ||
|
||
final class MethodToCall | ||
{ | ||
private string $className; | ||
private string $methodName; | ||
|
||
public function __construct(string $className, string $methodName) | ||
{ | ||
if (!method_exists($className, $methodName) && ! method_exists($className, '__call')) { | ||
throw MethodDoesNotExist::on($className, $methodName); | ||
} | ||
|
||
$this->className = $className; | ||
$this->methodName = $methodName; | ||
} | ||
|
||
public function getClassName(): string | ||
{ | ||
return $this->className; | ||
} | ||
|
||
public function getMethodName(): string | ||
{ | ||
return $this->methodName; | ||
} | ||
} |
Oops, something went wrong.