PHP code example of t3ran13 / golos-php-event-listener

1. Go to this page and download the library: Download t3ran13/golos-php-event-listener 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/ */

    

t3ran13 / golos-php-event-listener example snippets



namespace MyApp;

use GolosPhpEventListener\app\process\BlockchainExplorerProcess;
use GolosPhpEventListener\app\process\EventsHandlersProcess;
use GrapheneNodeClient\Connectors\ConnectorInterface;
use ProcessManager\db\RedisManager;
use ProcessManager\ProcessManager;

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
define('PATH', __DIR__);
;
} else {
    $pm->setPriority(25)
        ->setExecutionStep(1)
        ->setMaxNTriesOfRun(0)
        ->setSecondsBetweenRuns(55)
        ->setMaxLifetimeWithoutResults(20)
        ->saveState();
}

// creating event handler
$eh1 = (new PostIsCreatedEventHandler($db))
    ->setProcessName('GEV:votesOffafnur:1')
    ->generateIdFromProcessName()
    ->addCondition('op:1:voter','golosboard') //event trigger 1
    ->addCondition('op:0','vote'); //event trigger 2
if ($eh1->hasState()) {
    $eh1->loadState();
} else {
    $eh1->setPriority(35)
        ->setExecutionStep(1)
        ->setMaxNTriesOfRun(0)
        ->setSecondsBetweenRuns(10)
        ->setMaxLifetimeWithoutResults(15)
        ->saveState();
}

// creating event handler
$eh2 = (new PostIsCreatedEventHandler($db))
    ->setProcessName('GEV:allComments:2')
    ->generateIdFromProcessName()
    ->addCondition('op:0','comment'); //event trigger 1
if ($eh2->hasState()) {
    $eh2->loadState();
} else {
    $eh2->setPriority(35)
        ->setExecutionStep(1)
        ->setMaxNTriesOfRun(0)
        ->setSecondsBetweenRuns(10)
        ->setMaxLifetimeWithoutResults(15)
        ->saveState();
}

// creating event handler
$eh3 = (new PostIsCreatedEventHandler($db))
    ->setProcessName('GEV:allVotes:3')
    ->generateIdFromProcessName()
    ->addCondition('op:0','vote'); //event trigger 1
if ($eh3->hasState()) {
    $eh3->loadState();
} else {
    $eh3->setPriority(35)
        ->setExecutionStep(1)
        ->setMaxNTriesOfRun(0)
        ->setSecondsBetweenRuns(10)
        ->setMaxLifetimeWithoutResults(15)
        ->saveState();
}


// Creating blockchain lestener
$BEP = (new BlockchainExplorerProcess($db,ConnectorInterface::PLATFORM_GOLOS))
    ->setProcessName('GEV:BlockchainExplorer')
    ->setLastBlock(16146488)
    ->addEvent($eh1) //do not forget add eventhandlers to explorer process
    ->addEvent($eh2) //adding event handler for detecting events
    ->addEvent($eh3);
if ($BEP->hasState()) {
    $BEP->loadState();
} else {
    $BEP->setPriority(30)
        ->setExecutionStep(1)
        ->setMaxNTriesOfRun(0)
        ->setSecondsBetweenRuns(30)
        ->setMaxLifetimeWithoutResults(20)
        ->saveState();
}

$pm->addProcess($BEP)
    ->addProcess($eh1)//add event listener for handling events
    ->addProcess($eh2)
    ->addProcess($eh3);
$pm->start();



namespace MyApp;

use GolosPhpEventListener\app\process\handlers\EventHandlerAbstract;


class VoteHandler extends EventHandlerAbstract
{
    public function start()
    {
        $events = $this->getEvents(); //TODO FIXME
        echo PHP_EOL . date('Y-m-d H:i:s') . $this->getProcessName() . ' is running and have total events='
            . count($events);

        foreach ($events as $key => $event) {
            // some code
            $this->setLastUpdateDatetime(date('Y-m-d H:i:s'))
                ->removeEventByKey($key)
                ->saveState();
        }
    }

    /**
     * ask process to start
     *
     * @return bool
     */
    public function isStartNeeded(): bool
    {
        return parent::isStartNeeded()
            && count($this->getEvents()) > 0;
    }
}

composer