skolodyazhnyy/message-bus-client

This package is abandoned and no longer maintained. No replacement package was suggested.

Simple Magento Message Bus Client

0.2.0 2016-08-04 15:05 UTC

This package is not auto-updated.

Last update: 2020-03-20 17:36:36 UTC


README

Very basic Magento Service Contract Client. It allows to publish and consume messages from the Magento Message Bus over AMQP or HTTP protocol. It's great for quick prototyping as it uses PHP arrays instead of objects, support multiple versions and multiple protocols.

Usage

There are few examples of how to glue things together below, but you can find more, complete examples in the examples folder.

Defining a service

// Create a client
$bus = new ServiceBus($driver);

// Define a new service "calc"
$service = $bus->define('calc');

// Bind methods to the service
$service->bind(
    CallbackBinding::make()
        ->on('add', '1.0', function(Request $request) {
            return new Response($request->getArgument('a') + $request->getArgument('b'));
        })
);

// Call method of the service
// You can also call method asynchronously using $service->publish(...), but you won't receive direct response
$promise = $service->call(new Request('add', '1.0', ['a' => 10, 'b' => 65]));

// Resolve response
$response = $promise->resolve();

// Receive result
$response->getValue(); // gives 75

Calling remote service

$bus = new ServiceBus($driver);

// Fetch service endpoint
$endpoint = $bus->discover('calc');

// Call method of the service
// You can also call method asynchronously using $endpoint->publish(...), but you won't receive direct response
$promise = $endpoint->call(new Request('add', '1.0', ['a' => 10, 'b' => 65]));

// Resolve response and receive result
$promise->resolve()->getValue(); // gives 75

Using promise fallback

If you want to build really reliable software you need to think about having fallback in case a synchronous call fails. Promises allows you to set a simple callback which will be called to get "fallback" result in case RPC call fails.

$promise = $endpoint->call(...);

$result = $promise->fallback(function(\Exception $exception) {
       // You may build different behaviour based on $exception
       
       return $this->storeage->get('call_result');
    })
    ->resolve(1);
    
$this->storeage->save('call_result', $result);

Other examples