是否可以从父类方法返回子方法?

时间:2019-03-27 12:13:06

标签: php laravel laravel-queue

由于我是Laravel的新手,我目前正在使用队列,并且在工作中遇到以下问题。 因为我正在调用的API可能会限制,所以我需要为每个调用的方法都具有该逻辑,因此我创建了一个父(基)类。 (不确定这是否是正确的方法,如果这里有误,也请纠正我)

所以我有JobClass扩展了BaseJobClass,它应该处理API客户端的创建。

BaseJob

class BaseJob implements ShouldQueue
{
    protected function performActionOrThrottle($method, $parameters, Customer $customer, Marketplace $marketplace) {
        $client = $this->createClient($customer, $marketplace);
        if (!method_exists($client, $method)) {
            return $this->fail(new \Exception("Method {$method} does not exist in " . get_class($client)));
        }
        try {
            $result = $client->{$method}($parameters);
        } catch (\Exception $exception)
        {
            echo $exception->getMessage().PHP_EOL;
            return $this->release(static::THROTTLE_LIMIT);
        }
        return $result;
    }
}

工作

class Job extends BaseJob
{

    CONST THROTTLE_LIMIT = 60;
    CONST RETRY_DELAY = 10;

    private $customer;
    private $requestId;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Customer $customer, $requestId = null)
    {
        $this->customer = $customer;
        $this->requestId = $requestId;
        echo "Updating Inventory for {$customer->name}\n";
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $marketplace = Marketplace::findOrFail(5);

        if (!$report = $this->performActionOrThrottle('GetReport', $this->requestId, $this->customer, $marketplace)) {
            echo "Report not available, trying again in " . self::RETRY_DELAY;
            return $this->release(self::RETRY_DELAY);
        }

        ... 
        handle Data
        ...

        return;
    }
}

为了结束工作,我需要返回handle()方法。如何从父方法返回handle方法,而不必检查返回值并实现返回值?如果我愿意的话,那就没有必要让那个父母包含我完成几个工作所需的全部逻辑。

0 个答案:

没有答案