refactor(core): enhance SDK structure and documentation
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
@@ -2,21 +2,29 @@
|
||||
|
||||
namespace Apoloz\AlfaBank\Webhooks;
|
||||
|
||||
use Apoloz\AlfaBank\Webhooks\Data\BaseWebhookData;
|
||||
use Apoloz\AlfaBank\Data\Webhooks\Payloads\BaseWebhookData;
|
||||
use Apoloz\AlfaBank\Data\Webhooks\Payloads\BaasWebhookData;
|
||||
use Apoloz\AlfaBank\Data\Webhooks\Payloads\SbpWebhookData;
|
||||
|
||||
class WebhookFactory
|
||||
{
|
||||
public static function create(array $payload): BaseWebhookData
|
||||
{
|
||||
// Handle array of events (BaaS often sends arrays)
|
||||
if (isset($payload[0]) && is_array($payload[0])) {
|
||||
// For simplicity in this factory, we take the first one or wrapper needed
|
||||
// But let's assume standard single event for the DTO mostly.
|
||||
// If it's a list, the user should iterate.
|
||||
return BaseWebhookData::from($payload[0]);
|
||||
// Handle array of events (BaaS often sends arrays, e.g. [ {object...} ])
|
||||
$targetPayload = $payload;
|
||||
if (array_is_list($payload) && isset($payload[0])) {
|
||||
$targetPayload = $payload[0];
|
||||
}
|
||||
|
||||
return BaseWebhookData::from($payload);
|
||||
if (self::isSbp($targetPayload)) {
|
||||
return SbpWebhookData::from($targetPayload);
|
||||
}
|
||||
|
||||
if (self::isBaas($targetPayload)) {
|
||||
return BaasWebhookData::from($targetPayload);
|
||||
}
|
||||
|
||||
return BaseWebhookData::from($targetPayload);
|
||||
}
|
||||
|
||||
public static function isSbp(array $payload): bool
|
||||
@@ -26,8 +34,9 @@ class WebhookFactory
|
||||
|
||||
public static function isBaas(array $payload): bool
|
||||
{
|
||||
// BaaS usually has 'object' or is a list of events
|
||||
if (isset($payload[0]) && isset($payload[0]['object'])) return true;
|
||||
if (array_is_list($payload) && isset($payload[0]['object'])) {
|
||||
return true;
|
||||
}
|
||||
return isset($payload['object']);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user