php - 从父类方法返回子方法的可能性?
问题描述
由于我对 Laravel 很陌生,我目前正在使用队列,并且在工作中遇到以下问题。因为我正在调用的 API 可以节流,所以我需要为我调用的每个方法都有这个逻辑,所以我创建了一个父(基)类。(不确定这是否是正确的方法,如果这里有错误也请纠正我)
所以我有JobClass
扩展的BaseJobClass
,它应该处理 API 客户端的创建。
基本工作
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() 方法。如何从父方法返回句柄方法,而无需检查返回值并实现返回它?如果我愿意,那么让那个父级包含我完成几份工作所需的所有逻辑是没有意义的。
解决方案
推荐阅读
- css - 右对齐表格标题(mat-header-cell)
- django - 导致上一页的视图中的取消按钮
- ios - 在 Objective-c 中没有得到 UILabel 的正确高度
- java - 我可以自定义 lombok 为类中的每个变量编写类似于 getter 和 setter 的代码吗?
- json - 如何在外部文件 conf 中服务 Angular 10 的外部化参数。json
- excel - Excel VBA:将两列合并为一列
- javascript - 鼠标悬停图像卡在循环中
- python - MPI4PY - 我可以预测填充大小吗?
- objective-c - 在 WKWebView 中禁用鼠标单击和鼠标悬停
- javascript - 模态立即关闭