源平台查询适配器
源平台查询适配器用于与源平台的数据查询接口进行交互,包含 dispatch
、handleResponse
和 connect
三个核心方法。
调度方法(dispatch
)
该方法初始化查询,生成请求参数,并将请求添加到队列中。
<?php
namespace Adapter\PlatformName;
use Domain\Datahub\Instance\Adapter\Adapter;
use Adapter\PlatformName\SDK\PlatformNameSDK;
class PlatformNameAdapter extends Adapter {
const DIRECTION = 'source';
public function dispatch() {
$this->setVariable('PAGINATION_START_PAGE', 1);
$request = $this->generateRequestParams();
$jobId = $this->getAsynSourceJobStorage()->insertOne($this->metaData['api'], $request);
$this->asynSourceJob(0, $jobId);
return ['status' => true, 'content' => $jobId];
}
}
处理响应方法(handleResponse
)
此方法处理源平台返回的数据,包括状态检查、数据解析和存储,以及分页逻辑的处理。
public function handleResponse(array $response, $jobId = null) {
if ($response['code'] != 200) {
return $this->handleError($response, $jobId);
}
$listKey = !empty($this->metaData['otherResponse']) ? $this->metaData['otherResponse'][0]['value'] : 'rows';
foreach ($response['data'][$listKey] as $obj) {
$id = $this->getDataKeyValue($obj, $this->metaData['id']);
$number = $this->getDataKeyValue($obj, $this->metaData['number']);
$this->getDataStorage()->insertOne($id, $number, $obj, $this->metaData['idCheck'], $jobId);
}
if ($response['data']['page'] * $response['data']['size'] < $response['data']['total']) {
$this->invokeRequest['page'] += 1;
$nid = $this->getAsynSourceJobStorage()->insertOne($this->metaData['api'], $this->invokeRequest);
$this->asynSourceJob(5, $nid);
}
return true;
}
连接方法(connect
)
该方法实例化并连接到SDK,确保与源平台的通信正常。
public function connect() {
if ($this->SDK != null) return;
$connector = ConnectorRepository::findOne($this->strategy[$this->direction]->connector_id);
$env = 'env_' . $connector->env . '_params';
$this->SDK = new PlatformNameSDK($connector->$env, $connector->env);
return $this->SDK->connection();
}
通过这种方式,代码变得更加清晰简洁,并且易于理解。同时,确保所有必要的功能和逻辑都能被保留和执行。