PHP code example of lmc / cqrs-handler

1. Go to this page and download the library: Download lmc/cqrs-handler 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/ */

    

lmc / cqrs-handler example snippets


$queryFetcher = new QueryFetcher(
    // Cache
    false,  // disabled cache
    null,   // no cache pool -> no caching

    // Profiling
    null    // no profiler bag -> no profiling
);

$profilerBag = new ProfilerBag();

$queryFetcher = new QueryFetcher(
    // Cache
    true,           // is cache enabled
    $cache,         // instance of Psr\Cache\CacheItemPoolInterface

    // Profiling
    $profilerBag,   // collection of profiled information

    // Custom handlers
    // NOTE: there is multiple ways of defining handler
    [
        [new TopMostHandler(), PrioritizedItem::PRIORITY_HIGHEST],                      // Array definition of priority
        new OtherHandler(),                                                             // Used with default priority of 50
        new PrioritizedItem(new FallbackHandler(), PrioritizedItem::PRIORITY_LOWEST)    // PrioritizedItem value object definition
    ],

    // Custom response decoders
    // NOTE: there is multiple ways of defining response decoders
    [
        [new TopMostDecoder(), PrioritizedItem::PRIORITY_HIGHEST],                      // Array definition of priority
        new OtherDecoder(),                                                             // Used with default priority of 50
        new PrioritizedItem(new FallbackDecoder(), PrioritizedItem::PRIORITY_LOWEST)    // PrioritizedItem value object definition
    ]
);

$this->queryFetcher->addHandler(new MyQueryHandler(), PrioritizedItem::PRIORITY_MEDIUM);
$this->queryFetcher->addDecoder(new MyQueryResponseDecoder(), PrioritizedItem::PRIORITY_HIGH);

// with continuation
$this->queryFetcher->fetch(
    $query,
    fn ($response) => $this->repository->save($response),
    fn (\Throwable $error) => $this->logger->critical($error->getMassage())
);

// with return
try {
    $response = $this->queryFetcher->fetchAndReturn($query);
    $this->repository->save($response);
} catch (\Throwable $error) {
    $this->logger->critical($error->getMessage());
}

$query = new CallbackQueryHandler(
    fn () => $this->repository->fetchData(),
    new CacheKey('my-data-key'),
    CacheTime::oneHour()
);

$query = new ProfiledCallbackQueryHandler(
    fn () => $this->repository->fetchData(),
    new CacheKey('my-data-key'),
    CacheTime::oneHour(),
    'my-profiler-key',
    ['additional' => 'data']  // optional
);

$commandSender = new CommandSender(
    // Profiling
    null    // no profiler bag -> no profiling
);

$profilerBag = new ProfilerBag();

$commandSender = new CommandSender(
    // Profiling
    $profilerBag,   // collection of profiled information

    // Custom handlers
    // NOTE: there is multiple ways of defining handler
    [
        [new TopMostHandler(), PrioritizedItem::PRIORITY_HIGHEST],                      // Array definition of priority
        new OtherHandler(),                                                             // Used with default priority of 50
        new PrioritizedItem(new FallbackHandler(), PrioritizedItem::PRIORITY_LOWEST)    // PrioritizedItem value object definition
    ],

    // Custom response decoders
    // NOTE: there is multiple ways of defining response decoders
    [
        [new TopMostDecoder(), PrioritizedItem::PRIORITY_HIGHEST],                      // Array definition of priority
        new OtherDecoder(),                                                             // Used with default priority of 50
        new PrioritizedItem(new FallbackDecoder(), PrioritizedItem::PRIORITY_LOWEST)    // PrioritizedItem value object definition
    ]
);

$this->commandSender->addHandler(new MyCommandHandler(), PrioritizedItem::PRIORITY_MEDIUM);
$this->commandSender->addDecoder(new MyCommandResponseDecoder(), PrioritizedItem::PRIORITY_HIGH);

// with continuation
$this->commandSender->send(
    $command,
    fn ($response) => $this->repository->save($response),
    fn (\Throwable $error) => $this->logger->critical($error->getMassage())
);

// with return
try {
    $response = $this->commandSender->sendAndReturn($query);
    $this->repository->save($response);
} catch (\Throwable $error) {
    $this->logger->critical($error->getMessage());
}

$command = new ProfiledDataCommand(
    fn () => $this->repository->fetchData(),
    new CacheKey('my-data-key'),
    CacheTime::oneHour(),
    'my-profiler-key',
    ['additional' => 'data']  // optional
);

$profilerBag->setVerbosity(ProfilerBag::VERBOSITY_VERBOSE);