PHP code example of ygto / job-collector
1. Go to this page and download the library: Download ygto/job-collector 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' );
ygto / job-collector example snippets
namespace Jobs ;
use JobCollector \Job ;
class GetPayment implements Job
{
protected $user;
protected $order;
protected $payment;
protected $success = 'payment handled successfully.' ;
protected $error = 'there is a error in payment' ;
public function __construct ($user, $order, $payment)
{
$this ->user = $user;
$this ->order = $order;
$this ->payment = $payment;
}
public function handle ()
{
if (!$this ->payment->getPayment($this ->user, $this ->order)) {
$this ->error = $this ->payment->getError();
throw new \Exception ($this ->error);
}
}
public function rollback ()
{
$this ->payment->refundPayment($this ->user, $this ->order);
}
public function onSuccess ()
{
return $this ->success;
}
public function onError ()
{
return $this ->error;
}
}
use JobCollector \Collector ;
use Jobs \CheckUserBalance ;
use Jobs \GetPayment ;
use Jobs \PrintPayslip ;
class ExampleController
{
public function checkout (User $user, Order $order, Payment $payment, PdfLibrary $pdf)
{
$collector = new JobCollector\Collector();
$collector->push(new CheckUserBalance($user, $order))
->push(new GetPayment($user, $order, $payment))
->push(new PrintPayslip($user, $order, $pdf));
if ($collector->handle()) {
} else {
}
}
}