Skip to content

Commit

Permalink
Merge pull request #2 from bowphp/refactoring
Browse files Browse the repository at this point in the history
Add readme file
  • Loading branch information
papac authored Mar 16, 2023
2 parents 41a5e76 + c17065f commit 17b66b4
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 99 deletions.
21 changes: 21 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2023 Franck DAKIA <[email protected]>

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.
31 changes: 24 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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;
Expand All @@ -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([
Expand Down Expand Up @@ -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");
}
}
```
Expand All @@ -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.
Expand All @@ -101,4 +118,4 @@ Thank you for considering contributing to Bow Framework! The contribution guide

[[email protected]](mailto:[email protected]) - [@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)**
4 changes: 2 additions & 2 deletions src/Command/CommandBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions src/Query/QueryBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
81 changes: 0 additions & 81 deletions src/README.md

This file was deleted.

14 changes: 7 additions & 7 deletions src/Registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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
)
);
}
Expand Down

0 comments on commit 17b66b4

Please sign in to comment.