14/07
2017
Wallogit.com
2017 © Pedro Peláez
job collector to write more clean code
don't mess around when handling too much process.
composer require ygto/job-collector
or, (*1)
"require": {
"ygto/job-collector": "^1.0"
}
JobCollector\Job interface has 4 methods - handle() first handle method run. - rollback() if handle method throw exception then rollback method run. - onSuccess() if handle method run successfully then onSuccess run and keep the method's return ; - onError() if handle method throw exception then onError run and keep the method's return ;, (*2)
JobCollector\Collector has 4 methods, (*3)
//GetPayment.php, (*4)
<?php 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)) {
//payment error setted;
$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;
}
}
//ExampleController.php, (*5)
<?php
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()) {
//$collector->getSuccess();
} else {
//$collector->getError();
}
}
}