prooph/service-bus-symfony-bundle

Use queries without promises as an alternative

xgc1986 opened this issue · 4 comments

Using promises for synchronous code makes my code somewhat dirty, and i've seen in the demo that the alternative exist, but I don't see in the documentation how to use queries without promises

It is not supported. The QueryBus class in prooph/service-bus repository simply requires react/promises.

I managed to hack it like this but I just finished it like 2 minutes ago so it's not tested at all.

I'd also welcome some support in prooph.

sstok commented

I am using this one https://github.com/park-manager/park-manager/blob/master/src/Component/Model/QueryResponseNegotiator.php

use Prooph\ServiceBus\QueryBus;
use React\Promise\RejectedPromise;

final class QueryResponseNegotiator
{
    /**
     * @param QueryBus $queryBus
     * @param mixed    $query
     *
     * @return mixed
     */
    public static function handle(QueryBus $queryBus, $query)
    {
        $finalResult = null;
        $promise = $queryBus->dispatch($query);

        if ($promise instanceof RejectedPromise) {
            $promise->done();
        }

        $promise->then(
            function ($result) use (&$finalResult) {
                $finalResult = $result;
            }
        );

        return $finalResult;
    }
}

Note the original code is MPLv2.0. As the author - The code fragment shown above is hereby released under C0 (Public Domain).

I close this because it is out of scope for the service-bus-bundle. Feel free to open an issue at prooph/service-bus.