update EntityParse parameters type
This commit is contained in:
@@ -9,6 +9,7 @@ use App\Model\Company;
|
||||
use App\Model\Platform;
|
||||
use App\Model\Store;
|
||||
use App\Entity\Parse\Traits\EntityParseHelper;
|
||||
use Hyperf\Amqp\Message\ConsumerMessageInterface;
|
||||
use Hyperf\Collection\LazyCollection;
|
||||
use InvalidArgumentException;
|
||||
|
||||
@@ -18,13 +19,14 @@ use InvalidArgumentException;
|
||||
* 使用工厂方法模式 + 延迟初始化
|
||||
* 提供消息解析的通用框架和默认实现
|
||||
*
|
||||
* @method static static create(array $data)
|
||||
* @method static static create(array $data, ConsumerMessageInterface $message)
|
||||
*/
|
||||
abstract class EntityParse implements EntityParseInterface
|
||||
{
|
||||
use EntityParseHelper;
|
||||
|
||||
protected array $data;
|
||||
protected ConsumerMessageInterface $message;
|
||||
protected ?Platform $platform = null;
|
||||
protected ?Company $company = null;
|
||||
protected ?Store $store = null;
|
||||
@@ -42,13 +44,15 @@ abstract class EntityParse implements EntityParseInterface
|
||||
* 工厂方法:创建解析器实例
|
||||
*
|
||||
* @param array $data
|
||||
* @param ConsumerMessageInterface $message
|
||||
* @return static
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public static function create(array $data): static
|
||||
public static function create(array $data, ConsumerMessageInterface $message): static
|
||||
{
|
||||
$instance = new static();
|
||||
$instance->data = $data;
|
||||
$instance->message = $message;
|
||||
|
||||
// 在初始化前先验证数据
|
||||
if (!$instance->messageValidate($data)) {
|
||||
@@ -130,7 +134,7 @@ abstract class EntityParse implements EntityParseInterface
|
||||
/**
|
||||
* 平台作用域匹配 - 提供默认实现
|
||||
*
|
||||
* 从 metadata 中提取平台信息
|
||||
* 从 exchange 中提取平台信息
|
||||
* 子类可以覆盖此方法以实现自定义逻辑
|
||||
*
|
||||
* @param array $metadata
|
||||
@@ -139,13 +143,13 @@ abstract class EntityParse implements EntityParseInterface
|
||||
*/
|
||||
public function platformScopeMatch(array $metadata): Platform
|
||||
{
|
||||
return $this->extractPlatformFromMetadata($metadata);
|
||||
return $this->extractPlatformFromExchange($this->message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 实体匹配 - 提供默认实现
|
||||
*
|
||||
* 从 metadata 中提取实体类型
|
||||
* 从 routing key 中提取实体类型
|
||||
* 子类可以覆盖此方法以实现自定义逻辑
|
||||
*
|
||||
* @param array $metadata
|
||||
@@ -154,22 +158,31 @@ abstract class EntityParse implements EntityParseInterface
|
||||
*/
|
||||
public function entityMatch(array $metadata): Entity
|
||||
{
|
||||
return $this->extractEntityFromMetadata($metadata);
|
||||
return $this->extractEntityFromRoutingKey($this->message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 唯一标识符提取 - 提供默认实现
|
||||
*
|
||||
* 默认从 metadata 中提取 unique_id
|
||||
* 子类可以覆盖此方法以实现自定义逻辑
|
||||
* 子类需要覆盖此方法以实现自定义逻辑
|
||||
*
|
||||
* @param array $metadata
|
||||
* @return string|int
|
||||
* @return array
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function entityUniqueIdentifierExtract(array $metadata): string|int
|
||||
public function entityUniqueIdentifierExtract(array $metadata): array
|
||||
{
|
||||
return $metadata['unique_id'] ?? throw new InvalidArgumentException('unique_id not found in metadata');
|
||||
// 举例说明,当 Order Entity 实体需要根据 平台 ID 和 平台订单 ID 来确定唯一性时
|
||||
// 确认仓库中 Order 模型 对应的数据库字段约束条件为
|
||||
// CONSTRAINT "orders_store_platform_order_unique" UNIQUE ("store_id", "platform_order_id")
|
||||
// 可以从 $metadata 中分别提取 store_id 和 platform_order_id 的值
|
||||
// 返回构造后的结果即可
|
||||
// return ['store_id' => 123, 'platform_order_id' => 123123]
|
||||
|
||||
$className = static::class;
|
||||
throw new InvalidArgumentException(
|
||||
"Method entityUniqueIdentifierExtract() must be implemented in class '{$className}'"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -214,6 +227,16 @@ abstract class EntityParse implements EntityParseInterface
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取消息对象
|
||||
*
|
||||
* @return ConsumerMessageInterface
|
||||
*/
|
||||
public function getMessage(): ConsumerMessageInterface
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取平台对象
|
||||
*
|
||||
|
||||
@@ -130,8 +130,8 @@ class EntityParseFactory
|
||||
// 4. 获取对应的 Parser 类
|
||||
$parserClass = self::resolveParserClass($platformName, $entityType);
|
||||
|
||||
// 5. 创建并返回 Parser 实例,传递解析后的数据
|
||||
return $parserClass::create($data);
|
||||
// 5. 创建并返回 Parser 实例,传递解析后的数据和消息对象
|
||||
return $parserClass::create($data, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user