PHP code example of wyrihaximus / tactician-job-command-mapper

1. Go to this page and download the library: Download wyrihaximus/tactician-job-command-mapper 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/ */

    

wyrihaximus / tactician-job-command-mapper example snippets




namespace Test\App\Commands;

use WyriHaximus\Tactician\JobCommand\Annotations\Job;

/**
 * @Job("awesomesauce")
 * OR
 * @Job({"awesomesauce", "sauceawesome"})
 */
class AwesomesauceCommand
{
}

use League\Tactician\Setup\QuickStart;

$map = (new Mapper())->map('src' . DS . 'CommandBus');

$job = 'awesomesauce';
// True when it has a command or false when it doesn't
$map->hasCommand($job);

$job = 'awesomesauce';
// Command class when it has a command, or throws an exception when it doesn't
$command = $map->getCommand($job);

// Call the command bus with the command we found to handle the given job
$commandBus->handle(new $command(...$data));

final class CommandBusWrapper
{
    /**
      * @var CommandBus
      */
    private $commandBus;
    
    /**
      * @var Mapper
      */
    private $map;
    
    /**
      * @param CommandBus
      */
    public function __construct(CommandBus $commandBus)
    {
        $this->commandBus = $commandBus;
        $this->map = (new Mapper())->map('src' . DS . 'CommandBus', 'App\CommandBus');
    }
    
    /**
      * @param string $job
      * @param array $data
      * @return bool
      */
    function handle(string $job, array $data = []): bool
    {
        if (!$this->map->hasCommand($job)) {
            return false;
        }

        $command = $map->getCommand($job);
        $this->commandBus->handle(new $command(...$data));
        return true;
    }
}