Git Product home page Git Product logo

job-collector's Introduction

Job Collector

don't mess around when handling too much process.

Installation

composer require ygto/job-collector

or

"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 ;

JobCollector\Collector

JobCollector\Collector has 4 methods

  • 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

<?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

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

job-collector's People

Contributors

ygto avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

beyntech

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.