PHP code example of tomaszhanc / cqrs-lite-write-model

1. Go to this page and download the library: Download tomaszhanc/cqrs-lite-write-model library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tomaszhanc / cqrs-lite-write-model example snippets


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);
    }
}

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);
    }
}