定制 ygto/job-collector 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ygto/job-collector

Composer 安装命令:

composer require ygto/job-collector

包简介

job collector to write more clean code

README 文档

README

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

统计信息

  • 总下载量: 586
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2017-01-07

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固