PHP code example of thinkfluent / fanfan-client-php

1. Go to this page and download the library: Download thinkfluent/fanfan-client-php 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/ */

    

thinkfluent / fanfan-client-php example snippets


use FanFan\Client\Message\JobRequest;
use Google\Cloud\PubSub\PubSubClient;

// Prepare a JobRequest
$jobRequest = (new JobRequest())
    ->action('process_order')
    ->forEach('order', [4, 2, 19, 79]);

// Send to FanFan (via Pub/Sub)
$jobRequestTopic = (new PubSubClient())->topic('projects/get-fanfan/topics/fanfan-job-request');
$jobRequestTopic->publish($jobRequest->formatForPubSub());

use FanFan\Client\Message\Builder\TaskBuilder;
use Google\Cloud\PubSub\PubSubClient;

// Grab the Task data from the POST body (most of this is extracting the base64 message from the Pub/Sub payload)
// Your framework of choice may well have tooling to support you with this (Like PSR-7 Requests)
$payload = \json_decode(\file_get_contents('php://input'), false, 512, JSON_THROW_ON_ERROR);
$messageData = \json_decode(\base64_decode($payload->message->data), false, 512, JSON_THROW_ON_ERROR);

// Build `FanFan\Client\Message\Task`, contains instructions & payload for ONE fanned-out task
$task = TaskBuilder::fromPubSub($messageData);

// ...do work here...
// e.g. switch on $task->getAction(), use the data from $task->getPayload();

// Create & publish the Task outcome
$outcome = $task->createOutcome(TaskStatus::SUCCEEDED);
$jobRequestTopic = (new PubSubClient())->topic('projects/get-fanfan/topics/fanfan-task-done');
$jobRequestTopic->publish($outcome->formatForPubSub());

use FanFan\Client\Message\Builder\JobOutcomeBuilder;
// Extract Pub/Sub message into `$messageData` as per above example
$payload = \json_decode(\file_get_contents('php://input'), false, 512, JSON_THROW_ON_ERROR);
$messageData = \json_decode(\base64_decode($payload->message->data), false, 512, JSON_THROW_ON_ERROR);

// Build `FanFan\Client\Message\JobOutcome`
$outcome = JobOutcomeBuilder::fromPubSub($messageData);

// Work with the output
echo $outcome->getStatus(), PHP_EOL;
echo $outcome->getTaskCounts()['SUCCEEDED'], PHP_EOL;
echo $outcome->getTaskCounts()['FAILED'], PHP_EOL;
bash
composer