Skip to content

Commit

Permalink
feat: introduce response class
Browse files Browse the repository at this point in the history
  • Loading branch information
kajetan-nobel committed Sep 21, 2023
1 parent 3777713 commit 2f0d028
Show file tree
Hide file tree
Showing 16 changed files with 160 additions and 61 deletions.
74 changes: 74 additions & 0 deletions src/Client/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace Routegroup\Imoje\Payment\Client;

use ArrayAccess;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Client\Response as OriginalResponse;
use Routegroup\Imoje\Payment\DTO\BaseDto;
use Routegroup\Imoje\Payment\Exceptions\InvalidDtoNameException;

class Response implements Arrayable, ArrayAccess
{
public function __construct(
public readonly OriginalResponse $originalResponse,
public readonly BaseDto $data
) {
}

public static function resolve(
OriginalResponse $clientResponse,
string $dto,
): Response {
if (! is_subclass_of($dto, BaseDto::class)) {
throw new InvalidDtoNameException();
}

return new static(
$clientResponse,
new $dto($clientResponse->collect()->toArray())
);
}

public function toArray(): array
{
return $this->data->toArray();
}

public function offsetExists(mixed $offset): bool
{
return isset($this->data[$offset]);
}

public function offsetGet(mixed $offset): mixed
{
return $this->data->get($offset);
}

public function offsetSet(mixed $offset, mixed $value): void
{
//
}

public function offsetUnset(mixed $offset): void
{
unset($this->data[$offset]);
}

public function __get($key)
{
return $this->data->get($key);
}

public function __isset(mixed $key)
{
return $this->offsetExists($key);
}

public function __unset(mixed $key)
{
$this->offsetUnset($key);
}
}
18 changes: 0 additions & 18 deletions src/DTO/BaseDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,8 @@
use Illuminate\Support\Fluent;
use Routegroup\Imoje\Payment\Lib\Utils;

/**
* @template TKey of array-key
* @template TValue
*/
abstract class BaseDto extends Fluent
{
/**
* @var array<string, string>
*/
protected array $casts = [];

public function __construct($attributes = [])
Expand All @@ -24,10 +17,6 @@ public function __construct($attributes = [])
parent::__construct($attributes);
}

/**
* @param array<TKey, TValue> $attributes
* @return array<TKey, TValue> $attributes
*/
private function castAttributes(array $attributes): array
{
foreach ($this->casts as $attributeKey => $cast) {
Expand Down Expand Up @@ -58,20 +47,13 @@ public function castAttribute(string $castType, mixed $value): mixed
};
}

/**
* @return array<TKey, TValue> $attributes
*/
public function toArray(): array
{
return app(Utils::class)->transformValues($this->attributes);
}

/**
* @param array<TKey, TValue> $attributes
*/
public static function make(array $attributes): BaseDto
{
/* @phpstan-ignore-next-line */
return new static($attributes);
}
}
3 changes: 0 additions & 3 deletions src/DTO/Response/Api/PaymentProfileResponseDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
*/
class PaymentProfileResponseDto extends BaseDto
{
/**
* {@inheritdoc}
*/
protected array $casts = [
'year' => 'int',
'month' => 'int',
Expand Down
3 changes: 0 additions & 3 deletions src/DTO/Response/Api/PaymentResponseDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
*/
class PaymentResponseDto extends BaseDto
{
/**
* {@inheritdoc}
*/
protected array $casts = [
'amount' => 'int',
'created' => 'int',
Expand Down
3 changes: 0 additions & 3 deletions src/DTO/Response/Api/TransactionResponseDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
*/
class TransactionResponseDto extends BaseDto
{
/**
* {@inheritdoc}
*/
protected array $casts = [
'amount' => 'int',
'created' => 'int',
Expand Down
3 changes: 0 additions & 3 deletions src/DTO/Response/CancelledResponseDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
*/
class CancelledResponseDto extends BaseDto
{
/**
* {@inheritdoc}
*/
protected array $casts = [
'payment' => PaymentResponseDto::class,
];
Expand Down
3 changes: 0 additions & 3 deletions src/DTO/Response/ChargeProfileResponseDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
*/
class ChargeProfileResponseDto extends BaseDto
{
/**
* {@inheritdoc}
*/
protected array $casts = [
'transaction' => TransactionResponseDto::class,
];
Expand Down
3 changes: 0 additions & 3 deletions src/DTO/Response/OneClickResponseDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
*/
class OneClickResponseDto extends BaseDto
{
/**
* {@inheritdoc}
*/
protected array $casts = [
'transaction' => TransactionResponseDto::class,
'payment' => PaymentResponseDto::class,
Expand Down
18 changes: 18 additions & 0 deletions src/DTO/Response/ProfileResponseDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Routegroup\Imoje\Payment\DTO\Response;

use Routegroup\Imoje\Payment\DTO\BaseDto;
use Routegroup\Imoje\Payment\DTO\Response\Api\PaymentProfileResponseDto;

/**
* @property-read PaymentProfileResponseDto $transaction
*/
class ProfileResponseDto extends BaseDto
{
protected array $casts = [
'paymentProfile' => PaymentProfileResponseDto::class,
];
}
18 changes: 18 additions & 0 deletions src/DTO/Response/RefundResponseDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Routegroup\Imoje\Payment\DTO\Response;

use Routegroup\Imoje\Payment\DTO\BaseDto;
use Routegroup\Imoje\Payment\DTO\Response\Api\TransactionResponseDto;

/**
* @property-read TransactionResponseDto $transaction
*/
class RefundResponseDto extends BaseDto
{
protected array $casts = [
'transaction' => TransactionResponseDto::class,
];
}
3 changes: 0 additions & 3 deletions src/Exceptions/CantChangeTransactionStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@

class CantChangeTransactionStatus extends InvalidArgumentException
{
/**
* {@inheritdoc}
*/
protected $message = 'Cant change transaction status.';
}
12 changes: 12 additions & 0 deletions src/Exceptions/InvalidDtoNameException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Routegroup\Imoje\Payment\Exceptions;

use InvalidArgumentException;

class InvalidDtoNameException extends InvalidArgumentException
{
protected $message = 'Given name is not a subclass of a DTO.';
}
6 changes: 0 additions & 6 deletions src/Exceptions/NotImplementedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@

class NotImplementedException extends Exception
{
/**
* {@inheritDoc}
*/
protected $message = 'Given response has been not recognized';

/**
* {@inheritDoc}
*/
protected $code = '501';
}
3 changes: 0 additions & 3 deletions src/ImojeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ function (Application $app) {
);
}

/**
* {@inheritDoc}
*/
public function provides(): array
{
return [
Expand Down
35 changes: 23 additions & 12 deletions src/Lib/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
namespace Routegroup\Imoje\Payment\Lib;

use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Routegroup\Imoje\Payment\Client\Response;
use Routegroup\Imoje\Payment\DTO\Request\ChargeProfileRequestDto;
use Routegroup\Imoje\Payment\DTO\Request\RefundRequestDto;
use Routegroup\Imoje\Payment\DTO\Response\ChargeProfileResponseDto;
use Routegroup\Imoje\Payment\DTO\Response\ProfileResponseDto;
use Routegroup\Imoje\Payment\DTO\Response\RefundResponseDto;

class Api
{
Expand All @@ -21,38 +24,46 @@ public function __construct(
public function createRefund(
RefundRequestDto $dto,
string $transactionId
): Response {
) {
$url = $this->url->createRefundUrl($transactionId);

return $this
->request()
->post($url, $dto);
return Response::resolve(
$this->request()->post($url, $dto),
RefundResponseDto::class
);
}

public function getProfile(
string $profileId
): Response {
return $this->request()->get();
$url = $this->url->createProfileIdUrl($profileId);

return Response::resolve(
$this->request()->get($url),
ProfileResponseDto::class
);
}

public function chargeProfile(
ChargeProfileRequestDto $dto
): Response {
$url = $this->url->createChargeProfileUrl();

return $this
->request()
->post($url, $dto);
return Response::resolve(
$this->request()->post($url, $dto),
ChargeProfileResponseDto::class
);
}

public function deactivateProfile(
string $profileId
): Response {
$url = $this->url->createProfileIdUrl($profileId);

return $this
->request()
->delete($url);
return Response::resolve(
$this->request()->delete($url),
ProfileResponseDto::class
);
}

protected function request(): PendingRequest
Expand Down
16 changes: 15 additions & 1 deletion tests/Lib/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Routegroup\Imoje\Payment\Client\Response;
use Routegroup\Imoje\Payment\DTO\Request\ChargeProfileRequestDto;
use Routegroup\Imoje\Payment\DTO\Request\RefundRequestDto;
use Routegroup\Imoje\Payment\Lib\Api;
Expand All @@ -12,10 +13,23 @@
Http::fake();
$this->api = app(Api::class);
$this->url = app(Url::class);

// @todo replace with tests...
$mock = Mockery::mock('overload:'.Response::class);
$mock
->allows('resolve')
->andReturnSelf();
});

afterEach(function (): void {
Mockery::close();
});

it('makes refund request', function (): void {
$dto = RefundRequestDto::make(['amount' => 100]);
$dto = RefundRequestDto::make([
'amount' => 100,
]);

$this->api->createRefund($dto, '$transaction_id$');

Http::assertSent(function (Request $request) {
Expand Down

0 comments on commit 2f0d028

Please sign in to comment.