PHP code example of ibrostudio / laravel-piped-tasks

1. Go to this page and download the library: Download ibrostudio/laravel-piped-tasks 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/ */

    

ibrostudio / laravel-piped-tasks example snippets




namespace App\Processes\Payloads\Contracts;

use App\Models\Cart;
use App\Models\Invoice;
use App\Models\Order;
use App\Models\Payment;

interface TenantPayload
{
    public function getCart(): Cart;
  
    public function setOrder(Order $order): void;

    public function getOrder(): Order|null;
  
    public function setPayment(Payment $payment): void;

    public function getPayment(): Payment|null;
  
    public function setInvoice(Invoice $invoice): void;

    public function getInvoice(): Invoice|null
}

---------------------------

namespace App\Processes\Payloads;

use App\Models\Cart;
use App\Models\Invoice;
use App\Models\Order;
use App\Models\Payment;
use IBroStudio\PipedTasks\PayloadAbstract;

final class CreateOrderPayload extends PayloadAbstract implements OrderPayload
{
  public function __construct(
    protected Cart $cart,
    protected ?Order $order = null,
    protected ?Payment $payment = null,
    protected ?Invoice $invoice = null,
  )
  {}
  
  public function getCart(): Cart
  {
    return $this->cart;
  }
  
  public function setOrder(Order $order): void
  {
    $this->order = $order;
  }

  public function getOrder(): Order|null
  {
    return $this->order;
  }
  
  public function setPayment(Payment $payment): void
  {
    $this->payment = $payment;
  }

  public function getPayment(): Payment|null
  {
    return $this->payment;
  }
  
  public function setInvoice(Invoice $invoice): void
  {
    $this->invoice = $invoice;
  }

  public function getInvoice(): Invoice|null
  {
    return $this->invoice;
  }
}



namespace App\Processes\Payloads\Concerns;

use App\Models\Cart;
use App\Models\Invoice;
use App\Models\Order;
use App\Models\Payment;

trait OrderPayloadMethods
{
    public function getCart(): Cart
  {
    return $this->cart;
  }
  
  public function setOrder(Order $order): void
  {
    $this->order = $order;
  }

  public function getOrder(): Order|null
  {
    return $this->order;
  }
  (...)
  
  ---------------------------

namespace App\Processes\Payloads;

use App\Processes\Payloads\Concerns\OrderPayloadMethods;
use App\Models\Cart;
use App\Models\Invoice;
use App\Models\Order;
use App\Models\Payment;
use IBroStudio\PipedTasks\PayloadAbstract;

final class CreateOrderPayload extends PayloadAbstract implements OrderPayload
{
    use OrderPayloadMethods;
    
    public function __construct(
    protected Cart $cart,
    protected ?Order $order = null,
    protected ?Payment $payment = null,
    protected ?Invoice $invoice = null,
  )
  {}
}

  ---------------------------

namespace App\Processes\Payloads;

use App\Processes\Payloads\Concerns\OrderPayloadMethods;
use App\Models\Cart;
use App\Models\Invoice;
use App\Models\Order;
use App\Models\Payment;
use IBroStudio\PipedTasks\PayloadAbstract;

final class RebillOrderPayload extends PayloadAbstract implements OrderPayload
{
    use OrderPayloadMethods;
    
    public function __construct(
    protected Order $order,
    protected ?Payment $payment = null,
    protected ?Invoice $invoice = null,
  )
  {}
}



namespace App\Processes\Tasks;

use App\Actions\MakePaymentAction;
use App\Processes\Payloads\Contracts\OrderPayload;
use IBroStudio\User\Actions\CreateUserAction;
use IBroStudio\User\Processes\Payloads\Contracts\UserPayload;
use Closure;

final readonly class MakePaymentTask
{
    public function __construct(
        private MakePaymentAction $action,
    ) {}

    public function __invoke(OrderPayload $payload, Closure $next): mixed
    {
        $payload->setPayment(
            $this->action->execute($payload->getOrder())
        );

        return $next($payload);
    }
}

---------------------------

namespace App\Actions;

use App\Models\Order;
use App\Models\Payment;
use Spatie\QueueableAction\QueueableAction;

final class MakePaymentAction
{
    use QueueableAction;

    public function execute(Order $order): Payment
    {
        $payment = 'Process payment and return model';
        
        return $payment;
    }
}



namespace App\Processes;

use App\Processes\Tasks;
use IBroStudio\PipedTasks\Models\Process;

class CreateOrderProcess extends Process
{
    protected array $tasks = [
        Tasks\CreateOrderTask::class,
        Tasks\MakePaymentTask::class,
        Tasks\GenerateInvoiceTask::class,
        Tasks\SendInvoiceToCustomerTask::class,
        Tasks\NewOrderNotificationTask::class,
    ];
}



use App\Processes\CreateOrderProcess;
use App\Processes\Payloads\CreateOrderPayload;

$process = CreateOrderProcess::process(['cart' => $cart]);
    
$process->getOrder();



namespace App\Models;

use IBroStudio\PipedTasks\Concerns\IsProcessable;
use IBroStudio\PipedTasks\Contracts\Processable;
use Illuminate\Database\Eloquent\Model;

class Cart extends Model implements Processable
{
    use IsProcessable;
}



$cart->process(CreateOrderProcess::class);



namespace App\Processes\Tasks;

use App\Processes\Payloads\Contracts\OrderPayload;
use Closure;

class CreateOrderTask
{
    public function __invoke(OrderPayload $payload, Closure $next): mixed
    {
        $cart = $payload->getProcess()->processable;

        return $next($payload);
    }
}



use App\Models\Order;
use App\Processes\CreateOrderProcess;

Order::callProcess(CreateOrderProcess::class)



namespace App\Processes\Tasks;

use App\Models\Order;
use App\Processes\Payloads\Contracts\OrderPayload;
use Closure;

class CreateOrderTask
{
    public function __invoke(OrderPayload $payload, Closure $next): mixed
    {
        $payload->getProcess()->addProcessable(
            Order::create([...]);
        );

        return $next($payload);
    }
}



namespace App\Processes\Tasks;

use App\Models\Order;
use App\Processes\Payloads\Contracts\MyProcessPayload;
use Closure;
use IBroStudio\PipedTasks\PauseProcess;

class CallExternalTask
{
    public function __invoke(MyProcessPayload $payload, Closure $next): mixed
    {
        // Here call your external service allowing to 



use Spatie\Activitylog\Models\Activity;

$log = Activity::all()->last();

$logs = Activity::inLog('process-name')->get();



namespace App\Processes;

use App\Processes\Tasks;
use IBroStudio\PipedTasks\Models\Process;

class CreateOrderProcess extends Process
{
    public static ?string $logName = 'orders';
}



declare(strict_types=1);

return [

    'tasks' => [

        Process::class => [
            'prepend' => [
                FirstTask::class,
                SecondTask::class,
            ],
            'append' => [
                LastTask::class,
            ],
        ]
    ],

];
bash
php artisan piped-tasks:install
bash
php artisan make:piped-process CreateOrderProcess
bash
php artisan make:piped-process MakePaymentTask
bash
php artisan vendor:publish --tag=piped-tasks