diff --git a/LICENCE b/LICENCE new file mode 100644 index 0000000..03d6195 --- /dev/null +++ b/LICENCE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2023 Franck DAKIA + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE +OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/readme.md b/readme.md index 7c27165..2340ac7 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ # Bow CQRS -CQRS (Command Query Responsibility Segregation). It's a pattern that I first heard described by Greg Young. At its heart is the notion that you can use a different model to update information than the model you use to read information. For some situations, this separation can be valuable, but beware that for most systems CQRS adds risky complexity. +CQRS (Command Query Responsibility Segregation). It's a pattern that I first heard described by Greg Young. At its heart is the notion that you can use a different model to update information than the model you use to read information. For some situations, this separation can be valuable but beware that for most systems CQRS adds risky complexity. [For more information](https://www.martinfowler.com/bliki/CQRS.html) @@ -21,11 +21,14 @@ use Bow\CQRS\Command\CommandInterface; class CreateUserCommand implements CommandInterface { - public function __construct(public string $username, public string $email) {} + public function __construct( + public string $username, + public string $email + ) {} } ``` -Second, create the handler here: +Create the handler here: ```php use Bow\CQRS\Command\CommandHandlerInterface; @@ -37,7 +40,9 @@ class CreateUserCommandHandler implements CommandHandlerInterface public function process(CommandInterface $command): mixed { if ($this->userService->exists($command->email)) { - throw new UserServiceException("The user already exists"); + throw new UserServiceException( + "The user already exists" + ); } return $this->userService->create([ @@ -75,11 +80,17 @@ class UserController extends Controller public function __invoke(Request $request) { - $command = new CreateUserCommand($request->get('username'), $request->get('email')); + $payload = $request->only(['username', 'email']); + $command = new CreateUserCommand( + $payload['username'], + $payload['email'] + ); $result = $this->commandBus->execute($command); - return redirect()->back()->withFlash("message", "User created"); + return redirect() + ->back() + ->withFlash("message", "User created"); } } ``` @@ -90,6 +101,12 @@ Put a new route: $app->post("/users/create", UserController::class); ``` +Put a new route: + +```php +$app->post("/users/create", UserController::class); +``` + ## Contributing Thank you for considering contributing to Bow Framework! The contribution guide is in the framework documentation. @@ -101,4 +118,4 @@ Thank you for considering contributing to Bow Framework! The contribution guide [papac@bowphp.com](mailto:papac@bowphp.com) - [@papacdev](https://twitter.com/papacdev) -**Please, if there is a bug in the project. Contact me by email or leave me a message on [slack](https://bowphp.slack.com). or [join us on slask](https://join.slack.com/t/bowphp/shared_invite/enQtNzMxOTQ0MTM2ODM5LTQ3MWQ3Mzc1NDFiNDYxMTAyNzBkNDJlMTgwNDJjM2QyMzA2YTk4NDYyN2NiMzM0YTZmNjU1YjBhNmJjZThiM2Q)** +**Please, if there is a bug in the project. Contact me by email or leave me a message on [slack](https://bowphp.slack.com). or [join us on slack](https://join.slack.com/t/bowphp/shared_invite/enQtNzMxOTQ0MTM2ODM5LTQ3MWQ3Mzc1NDFiNDYxMTAyNzBkNDJlMTgwNDJjM2QyMzA2YTk4NDYyN2NiMzM0YTZmNjU1YjBhNmJjZThiM2Q)** diff --git a/src/Command/CommandBus.php b/src/Command/CommandBus.php index e9fb3d9..788dd1f 100644 --- a/src/Command/CommandBus.php +++ b/src/Command/CommandBus.php @@ -17,8 +17,8 @@ class CommandBus */ public function execute(CommandInterface $command): mixed { - $command_handler = Registration::getHandler($command); + $handler = Registration::getHandler($command); - return $command_handler->process($command); + return $handler->process($command); } } diff --git a/src/Query/QueryBus.php b/src/Query/QueryBus.php index e289ed5..3e7383a 100644 --- a/src/Query/QueryBus.php +++ b/src/Query/QueryBus.php @@ -16,8 +16,8 @@ class QueryBus */ public function execute(QueryInterface $query): mixed { - $query_handler = Registration::getHandler($query); + $handler = Registration::getHandler($query); - return $query_handler->process($query); + return $handler->process($query); } } diff --git a/src/README.md b/src/README.md deleted file mode 100644 index 8bb4aad..0000000 --- a/src/README.md +++ /dev/null @@ -1,81 +0,0 @@ -# Bow CQRS - -CQRS (Command Query Responsibility Segregation). It's a pattern that I first heard described by Greg Young. At its heart is the notion that you can use a different model to update information than the model you use to read information. For some situations, this separation can be valuable, but beware that for most systems CQRS adds risky complexity. - -[For more information](https://www.martinfowler.com/bliki/CQRS.html) - -Create the example command: - -```php -use Bow\CQRS\Command\CommandInterface; - -class CreateUserCommand implements CommandInterface -{ - public function __construct(public string $username, public string $email) {} -} -``` - -Create the handler here: - -```php -use Bow\CQRS\Command\CommandHandlerInterface; - -class CreateUserCommandHandler implements CommandHandlerInterface -{ - public function __construct(public UserService $userService) {} - - public function process(CommandInterface $command): mixed - { - if ($this->userService->exists($command->email)) { - throw new UserServiceException("The user already exists"); - } - - return $this->userService->create([ - "username" => $command->username, - "email" => $command->email - ]); - } -} -``` - -Add command to the register in `App\Configurations\ApplicationConfiguration::class`: - -```php -use Bow\CQRS\Registration as CQRSRegistration; - -public function run() -{ - CQRSRegistration::commands([ - CreateUserCommand::class => CreateUserCommandHandler::class - ]); -} -``` - -Execute the command in controller: - -```php -namespace App\Controllers; - -use App\Controllers\Controller; -use App\Commands\CreateUserCommand; - -class UserController extends Controller -{ - public function __construct(private CommandBus $commandBus) {} - - public function __invoke(Request $request) - { - $command = new CreateUserCommand($request->get('username'), $request->get('email')); - - $result = $this->commandBus->execute($command); - - return redirect()->back()->withFlash("message", "User created"); - } -} -``` - -Put a new route: - -```php -$app->post("/users/create", UserController::class); -``` diff --git a/src/Registration.php b/src/Registration.php index 5d4072c..d74ceb2 100644 --- a/src/Registration.php +++ b/src/Registration.php @@ -55,14 +55,14 @@ public static function commands(array $commands): void * @return QueryHandlerInterface|CommandHandlerInterface */ public static function getHandler( - QueryInterface|CommandInterface $cq + QueryInterface|CommandInterface $action ): QueryHandlerInterface|CommandHandlerInterface { - $cq_class = get_class($cq); + $action_class = get_class($action); - if ($cq instanceof QueryInterface) { - $handler = static::$queries[$cq_class] ?? null; + if ($action instanceof QueryInterface) { + $handler = static::$queries[$action_class] ?? null; } else { - $handler = static::$commands[$cq_class] ?? null; + $handler = static::$commands[$action_class] ?? null; } if (!is_null($handler)) { @@ -72,8 +72,8 @@ public static function getHandler( throw new CQRSException( sprintf( "The %s %s:class handler is not found on the CQ register", - $cq instanceof QueryInterface ? 'query' : 'command', - $cq_class + $action instanceof QueryInterface ? 'query' : 'command', + $action_class ) ); }