PHP code example of juststeveking / laravel-business-process

1. Go to this page and download the library: Download juststeveking/laravel-business-process 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/ */

    

juststeveking / laravel-business-process example snippets


use JustSteveKing\BusinessProcess\Process;

final class PurchaseProduct extends Process
{
    protected array $tasks = [
        CheckStockLevel::class,
        ProcessOrder::class,
        DecreaseStockLevel::class,
        NotifyWarehouse::class,
        EmailCustomer::class,
    ];
}

use JustSteveKing\BusinessProcess\Contracts\TaskContract;

final class CheckStockLevel implements TaskContract
{
    public function __invoke(ProcessPayload $payload, Closure $next): mixed
    {
        // perform your logic here with the passed in payload.
        
        return $next($payload);
    }
}

use JustSteveKing\BusinessProcess\Contracts\ProcessPayload;

final class PurchaseProductPayload implements ProcessPayload
{
    public function __construct(
        // add whatever public properties you need here
        public int $product,
        public int $user,
        public int $order,
    ) {}
}

final class PurchaseController
{
     public function __construct(
        private readonly PurchaseProduct $process,
     ) {}
     
     public function __invoke(PurchaseRequest $request, int $product): JsonResponse
     {
        try {
            $this->process->run(
                payload: $request->payload(),
            );
        } catch (Throwable $exception) {
            // Handle exception
        }
        
        // return response.
     }
}