PHP code example of pektiyaz / laravel-event-engine

1. Go to this page and download the library: Download pektiyaz/laravel-event-engine 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/ */

    

pektiyaz / laravel-event-engine example snippets


return new EventCanValue(true)
// OR
return new EventCanValue(true, $data)
// OR
return new EventCanValue(false, "Reason..")

return new EventDoValue(true)
// OR
return new EventDoValue(true, $data) 
// OR
return new EventDoValue(false, "Reason...")


//Listener in module Account
return new EventAskValue(true, ["name" => "John"])

//Listener in module Finance
return new EventAskValue(true, ["balance" => 500])

//Result
[
    "name" => "John",
    "balance" => 500
]


namespace App\Events;

class CanClientPay
{
    public function __construct(
        public int   $clientId,
        public float $amount
    ) {}
}

namespace App\Listeners;

use App\Events\CanClientPay;use Pektiyaz\LaravelEventEngine\CanValues\EventCanValue;

class CheckClientBalanceListener
{
    public function handle(CanClientPay $event): EventCanValue
    {
        // Example logic: client can pay if they have more than $event->amount
        $balance = 500; // You’d normally fetch this from DB
    
        if ($balance >= $event->amount) {
            return new EventCanValue(true);
        }
    
        return new EventCanValue(false, 'Insufficient balance');
    }
}

protected $listen = [
    \App\Events\CanClientPay::class => [
        \App\Listeners\CheckClientBalanceListener::class,
    ],
];

use App\Support\EventEngine;
use App\Events\CanClientPay;

$result = EventEngine::can(new CanClientPay($clientId, $amount));

if (!$result->can) {
    // Listing all answers
    foreach ($result->answers as $answer) {
        //$answer->can
        //$answer->data
        //$answer->toArray()
        Log::debug('Event Answer', $answer->toArray());
    }
    //$result->toArray()
    return response()->json(['error' => 'Payment denied', 'details' => $result->data]);
}

use App\Events\ClientProcessPay;

$process = EventEngine::do(new ClientProcessPay($clientId, $amount));

if (!$process->success) {
    // Listing all answers
    foreach ($result->answers as $answer) {
        //$answer->success
        //$answer->data
        //$answer->toArray()
        Log::debug('Event Answer', $answer->toArray());
    }
    //$result->toArray()
    return response()->json(['error' => 'Payment failed', 'details' => $process->data]);
}

return response()->json(['message' => 'Payment success', 'data' => $process->data]);

$response = EventEngine::doAtomic(new ClientPaid($clientId, $amount));

if (!$response->success) {
    return response()->json([
        'error' => 'Transaction rolled back',
        'details' => $response->data,
    ]);
}

use App\Events\ClientPaid;

EventEngine::after(new ClientPaid($clientId, $amount));

use App\Events\GetUserInfo;

$process = EventEngine::ask(new GetUserInfo($clientId));
if($process->success){
    return response()->json([
        'user' => $response->data,
    ]);
}



namespace App\Listeners;

use App\Events\CanClientPay;use Pektiyaz\LaravelEventEngine\CanValues\EventCanValue;

class CanClientPayListener
{
    public function handle(CanClientPay $event): EventCanValue
    {
        // Example logic: client can pay if they have more than $event->amount
        $balance = 500; // You’d normally fetch this from DB
    
        if ($balance >= $event->amount) {
            return new EventCanValue(true);
        }
    
        return new EventCanValue(false, 'Insufficient balance', "CanClientPayListener");
    }
}

$result = EventEngine::can(new CanClientPay($clientId, $amount));
foreach($result->answers as $answer){
    $answer->listener // CanClientPayListener
}
shell
php artisan event:clear
php artisan event:cache