Skip to content

Commit

Permalink
Extract errors constructors into single private method
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed Jun 14, 2023
1 parent 41b7455 commit 7e64d8b
Showing 1 changed file with 23 additions and 42 deletions.
65 changes: 23 additions & 42 deletions src/Client/ValueObject/ErrorResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,60 +27,22 @@ private function __construct(

public static function forAssert(array $data): self
{
return new self(
$data['StatusCode'],
ResponseHeader::fromArray($data['ResponseHeader']),
$data['ErrorName'],
$data['ErrorMessage'],
[],
$data['Behavior'],
'Assert',
$data['TransactionId'],
$data['OrderId'],
$data['PayerMessage'] ?? null,
);
return self::createForOperation($data, 'Assert');
}

public static function forCapture(array $data): self
{
return new self(
$data['StatusCode'],
ResponseHeader::fromArray($data['ResponseHeader']),
$data['ErrorName'],
$data['ErrorMessage'],
[],
$data['Behavior'],
'Capture',
);
return self::createForOperation($data, 'Capture');
}

public static function forAuthorize(array $data): self
{
return new self(
$data['StatusCode'],
ResponseHeader::fromArray($data['ResponseHeader']),
$data['ErrorName'],
$data['ErrorMessage'],
$data['ErrorDetail'] ?? [],
$data['Behavior'],
'Authorize',
$data['TransactionId'] ?? null,
$data['OrderId'] ?? null,
$data['PayerMessage'] ?? null,
);
return self::createForOperation($data, 'Authorize');
}

public static function forRefund(array $data): self
{
return new self(
$data['StatusCode'],
ResponseHeader::fromArray($data['ResponseHeader']),
$data['ErrorName'],
$data['ErrorMessage'],
[],
$data['Behavior'],
'Refund',
);
return self::createForOperation($data, 'Refund');
}

public function getStatusCode(): int
Expand Down Expand Up @@ -170,4 +132,23 @@ public function isSuccessful(): bool
{
return false;
}

private static function createForOperation(array $data, string $operation): self
{
return new self(
$data['StatusCode'],
ResponseHeader::fromArray($data['ResponseHeader']),
$data['ErrorName'],
$data['ErrorMessage'],
$data['ErrorDetail'] ?? [],
$data['Behavior'],
$operation,
$data['TransactionId'] ?? null,
$data['OrderId'] ?? null,
$data['PayerMessage'] ?? null,
$data['ProcessorName'] ?? null,
$data['ProcessorResult'] ?? null,
$data['ProcessorMessage'] ?? null,
);
}
}

0 comments on commit 7e64d8b

Please sign in to comment.