Skip to content

Commit

Permalink
Merge pull request #7 from MyParcelCOM/feature/authorization-disrupti…
Browse files Browse the repository at this point in the history
…on-event

♻️ ✨  Refactored publish library + Introduced AuthorizationDisruption message
  • Loading branch information
M4tini committed Feb 2, 2024
2 parents a31c09d + 6c364e3 commit d03227b
Show file tree
Hide file tree
Showing 16 changed files with 330 additions and 138 deletions.
4 changes: 2 additions & 2 deletions src/Providers/SnsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Aws\Sns\SnsClient;
use GuzzleHttp\Client;
use Illuminate\Support\ServiceProvider;
use MyParcelCom\Payments\Providers\Sns\LocalClient;
use MyParcelCom\Payments\Providers\Sns\Publisher;
use MyParcelCom\Payments\Providers\Publish\LocalClient;
use MyParcelCom\Payments\Providers\Publish\Publisher;

class SnsServiceProvider extends ServiceProvider
{
Expand Down
31 changes: 31 additions & 0 deletions src/Publish/AuthorizationDisruption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace MyParcelCom\Payments\Providers\Publish;

use Override;

class AuthorizationDisruption extends Message
{
private readonly string $paymentProviderCode;

public function __construct(
private readonly string $shopId,
?string $paymentProviderCode = null,
?string $topicArn = null,
) {
$this->paymentProviderCode = $paymentProviderCode ?? env('PAYMENT_PROVIDER_CODE');

parent::__construct($topicArn);
}

#[Override]
public function payload(): array
{
return [
'shop_id' => $this->shopId,
'payment_provider_code' => $this->paymentProviderCode,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace MyParcelCom\Payments\Providers\Sns;
namespace MyParcelCom\Payments\Providers\Publish;

use RuntimeException;

Expand Down
2 changes: 1 addition & 1 deletion src/Sns/FailureCode.php → src/Publish/FailureCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace MyParcelCom\Payments\Providers\Sns;
namespace MyParcelCom\Payments\Providers\Publish;

enum FailureCode: string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Sns/LocalClient.php → src/Publish/LocalClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace MyParcelCom\Payments\Providers\Sns;
namespace MyParcelCom\Payments\Providers\Publish;

use GuzzleHttp\Client;
use GuzzleHttp\Promise\PromiseInterface;
Expand Down
29 changes: 29 additions & 0 deletions src/Publish/Message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace MyParcelCom\Payments\Providers\Publish;

use Illuminate\Support\Str;

abstract class Message
{
private readonly string $topicArn;

public function __construct(?string $topicArn = null)
{
$this->topicArn = $topicArn ?? config('publish.sns.topic_arn');
}

public function getTopicArn(): string
{
return $this->topicArn;
}

public function getType(): string
{
return strtolower(Str::snake(class_basename($this)));
}

abstract public function payload(): array;
}
29 changes: 29 additions & 0 deletions src/Publish/PaymentFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace MyParcelCom\Payments\Providers\Publish;

use Override;

class PaymentFailed extends Message
{
public function __construct(
private readonly string $myparcelcomPaymentId,
private readonly FailureCode $failureCode,
private readonly ?string $failureMessage = null,
?string $topicArn = null,
) {
parent::__construct($topicArn);
}

#[Override]
public function payload(): array
{
return [
'myparcelcom_payment_id' => $this->myparcelcomPaymentId,
'failure_code' => $this->failureCode->value,
'failure_message' => $this->failureMessage,
];
}
}
28 changes: 28 additions & 0 deletions src/Publish/PaymentSuccessful.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace MyParcelCom\Payments\Providers\Publish;

use DateTimeInterface;
use Override;

class PaymentSuccessful extends Message
{
public function __construct(
private readonly string $myparcelcomPaymentId,
private readonly DateTimeInterface $paidAt,
?string $topicArn = null,
) {
parent::__construct($topicArn);
}

#[Override]
public function payload(): array
{
return [
'myparcelcom_payment_id' => $this->myparcelcomPaymentId,
'paid_at' => $this->paidAt->format(DateTimeInterface::ATOM),
];
}
}
27 changes: 27 additions & 0 deletions src/Publish/PublishJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace MyParcelCom\Payments\Providers\Publish;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class PublishJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function __construct(private readonly Message $message)
{
}

public function handle(Publisher $publisher): void
{
$publisher
->publish($this->message)
->wait();
}
}
36 changes: 36 additions & 0 deletions src/Publish/Publisher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace MyParcelCom\Payments\Providers\Publish;

use Aws\Sns\SnsClient;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Utils;
use Illuminate\Support\Env;

class Publisher
{
public function __construct(
private readonly SnsClient $snsClient,
private readonly LocalClient $localClient,
) {
}

public function publish(Message $message): PromiseInterface
{
$payload = [
'type' => $message->getType(),
'data' => $message->payload(),
];

if (Env::get('APP_ENV') === 'local') {
return $this->localClient->publish($payload);
}

return $this->snsClient->publishAsync([
'Message' => Utils::jsonEncode($payload),
'TopicArn' => $message->getTopicArn(),
]);
}
}
39 changes: 0 additions & 39 deletions src/Sns/PublishJob.php

This file was deleted.

48 changes: 0 additions & 48 deletions src/Sns/Publisher.php

This file was deleted.

24 changes: 24 additions & 0 deletions src/Support/GetPrivatePropertyValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace MyParcelCom\Payments\Providers\Support;

use ReflectionClass;
use ReflectionException;

trait GetPrivatePropertyValue
{
/**
* @throws ReflectionException
*/
private function getPrivatePropertyValue(object $object, string $property): mixed
{
$classReflection = new ReflectionClass($object);
$propertyReflection = $classReflection->getProperty($property);
/** @noinspection PhpExpressionResultUnusedInspection */
$propertyReflection->setAccessible(true);

return $propertyReflection->getValue($object);
}
}
26 changes: 9 additions & 17 deletions tests/Sns/PublishJobTest.php → tests/Publish/PublishJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
use Mockery;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Mockery\MockInterface;
use MyParcelCom\Payments\Providers\Sns\Publisher;
use MyParcelCom\Payments\Providers\Sns\PublishJob;
use MyParcelCom\Payments\Providers\Publish\Message;
use MyParcelCom\Payments\Providers\Publish\Publisher;
use MyParcelCom\Payments\Providers\Publish\PublishJob;
use PHPUnit\Framework\TestCase;

class PublishJobTest extends TestCase
Expand All @@ -20,32 +21,23 @@ class PublishJobTest extends TestCase
public function test_it_handles_publish_job(): void
{
$faker = Factory::create();
$topicArn = "arn:aws:sns:eu-west-1:{$faker->randomNumber()}:{$faker->word}";
$myparcelcomPaymentId = $faker->uuid;
$paidAt = $faker->dateTime;

$snsPromise = Mockery::mock(Promise::class, function (MockInterface & Promise $mock) {
$mock->expects('wait');
});

$message = Mockery::mock(Message::class);

$publisher = Mockery::mock(Publisher::class, function (MockInterface & Publisher $mock) use (
$snsPromise,
$topicArn,
$myparcelcomPaymentId,
$paidAt
) {
$message) {
$mock->expects('publish')
->with(
$topicArn,
$myparcelcomPaymentId,
$paidAt,
null,
null,
)
->with($message)
->andReturns($snsPromise);
});

$job = new PublishJob($topicArn, $myparcelcomPaymentId, $paidAt);

$job = new PublishJob($message);
$job->handle($publisher);
}
}
Loading

0 comments on commit d03227b

Please sign in to comment.