2017 © Pedro Peláez
 

library cqrs-lite-write-model

image

tomaszhanc/cqrs-lite-write-model

  • Thursday, May 10, 2018
  • by tomaszhanc
  • Repository
  • 0 Watchers
  • 0 Stars
  • 64 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 2 % Grown

The README.md

CQRS Lite - Write Model

Build Status Scrutinizer Code Quality Code Coverage, (*1)

Sometimes it happens that you have many different commands which should be handled as one command. On the beginning it looks trivial: just create one command and pass it to CommandBus, then create another one and another one. But what with validation for a user? We would like to validate all commands before handling the first one. What if we do it via PATCH request: sometimes we will need to handle only one command, sometimes many - it depends on a request., (*2)

Handling multiple commands... or just one

Let's assume we have an action for PATCH request and we have two commands: Rename and Describe. Depends on a request we want to create both of them or only one:, (*3)

public function editAction(Request $request, $id)
{
    $renameFactory = new CommandFactory('name');
    $renameFactory->createBy(function (array $data) use ($id) {
        return new Rename($id, $data['name']);
    });

    $describeFactory = new CommandFactory('description');
    $describeFactory->createBy(function (array $data) use ($id) {
        return new Describe($id, $data['description']);
    });

    $builder = new CommandsBuilder();
    $builder->addCommandFactory($renameFactory);
    $builder->addCommandFactory($describeFactory);

    // $request->request->all() returns all data from the request (something like $_POST)
    $commands = $builder->createCommandsFor($request->request->all()); 

    // here you can validate commands and then handle them via command bus

    foreach ($commands as $command) {
        $this->commandBus->handle($command);
    }
}

Method CommandsBuilder::createCommandsFor() will create only those commands which should be created regarding if required fields are available (at least one of them) in the passed data (in the example in $request->request->all()). Above example can be simplified by using CommandsBuilderSupport trait:, (*4)

use CommandsBuilderSupport;

public function editAction(Request $request, $id)
{
    $this->addCommandFor('name')->createBy(
        function (array $data) use ($id) {
            return new Rename($id, $data['name']);
        }
    );

    $this->addCommandFor('description')->createBy(
        function (array $data) use ($id) {
            return new Describe($id, $data['description']);
        }
    );

    $commands = $this->commandsBuilder->createCommandsFor($request->request->all()); 

    foreach ($commands as $command) {
        $this->commandBus->handle($command);
    }
}

The Versions

10/05 2018

dev-master

9999999-dev

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Tomasz Hanc

cqrs cqrs lite write model

28/12 2017

0.2.1

0.2.1.0

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Tomasz Hanc

cqrs cqrs lite write model

28/12 2017

0.2

0.2.0.0

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Tomasz Hanc

cqrs cqrs lite write model

19/11 2017

0.1

0.1.0.0

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Tomasz Hanc

cqrs cqrs lite write model