2017 © Pedro Peláez
 

library job-collector

job collector to write more clean code

image

ygto/job-collector

job collector to write more clean code

  • Friday, July 14, 2017
  • by yigitoz
  • Repository
  • 2 Watchers
  • 3 Stars
  • 91 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 21 % Grown

The README.md

Job Collector

don't mess around when handling too much process.

Installation

composer require ygto/job-collector

or, (*1)

"require": {
    "ygto/job-collector": "^1.0"
}

JobCollector\Job

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

JobCollector\Collector has 4 methods, (*3)

  • push(\JobCollector\Job $job) push job to collector
  • handle() run pushed jobs if all jobs run successfully return true else return false
  • getSuccess() return onSuccess methods return
  • getError() return onError methods return

Usage

//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();
        }
    }
}

The Versions

14/07 2017

dev-master

9999999-dev

job collector to write more clean code

  Sources   Download

by yigit oztemel

30/06 2017

v1.0.3

1.0.3.0

job collector to write more clean code

  Sources   Download

by yigit oztemel

30/06 2017

v1.0.2

1.0.2.0

job collector to write more clean code

  Sources   Download

by yigit oztemel

07/01 2017

v1.0.1

1.0.1.0

  Sources   Download

by yigit oztemel

07/01 2017

v1.0

1.0.0.0

  Sources   Download

by yigit oztemel