Skip to content

Commit 5405467

Browse files
committed
Improve Domain design
1 parent 2c9855d commit 5405467

File tree

10 files changed

+134
-34
lines changed

10 files changed

+134
-34
lines changed

app/Notification/AppNotification.php

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
4+
namespace App\Notification;
5+
6+
7+
use App\Notification\Client\HTTPClientAdapterInterface;
8+
use App\Notification\Client\ResponseAdapterInterface;
9+
10+
interface AppNotificationInterface
11+
{
12+
public function __construct(HTTPClientAdapterInterface $client, string $message, string $messageType);
13+
14+
public function notify(): ResponseAdapterInterface;
15+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
4+
namespace App\Notification\Client\Guzzle;
5+
6+
7+
use App\Notification\Client\HTTPClientAdapterInterface;
8+
use App\Notification\Client\ResponseAdapterInterface;
9+
use GuzzleHttp\Client;
10+
use Psr\Http\Message\ResponseInterface;
11+
12+
class GuzzleHTTPClient implements HTTPClientAdapterInterface
13+
{
14+
private Client $client;
15+
16+
public function __construct()
17+
{
18+
$this->client = new Client();
19+
}
20+
21+
public function post(string $url, array $params): ResponseAdapterInterface
22+
{
23+
$clientResponse = $this->client->post(
24+
$url,
25+
[
26+
'json' => $params
27+
]
28+
);
29+
30+
return new GuzzleResponse($clientResponse);
31+
}
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
4+
namespace App\Notification\Client\Guzzle;
5+
6+
7+
use App\Notification\Client\ResponseAdapterInterface;
8+
use Psr\Http\Message\ResponseInterface;
9+
10+
class GuzzleResponse implements ResponseAdapterInterface
11+
{
12+
private ResponseInterface $clientResponse;
13+
14+
public function __construct(ResponseInterface $clientResponse)
15+
{
16+
$this->clientResponse = $clientResponse;
17+
}
18+
19+
public function getResponse(): array
20+
{
21+
return self::sanitizeResponse($this->clientResponse);
22+
}
23+
24+
private static function sanitizeResponse(ResponseInterface $guzzleResponse): array
25+
{
26+
return [
27+
'status_code' => $guzzleResponse->getStatusCode(),
28+
'message' => $guzzleResponse->getReasonPhrase()
29+
];
30+
}
31+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
4+
namespace App\Notification\Client;
5+
6+
7+
interface HTTPClientAdapterInterface
8+
{
9+
public function post(string $url, array $params): ResponseAdapterInterface;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
4+
namespace App\Notification\Client;
5+
6+
7+
interface ResponseAdapterInterface
8+
{
9+
public function getResponse(): array;
10+
}

app/Notification/Slack/SlackNotification.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,26 @@
44
namespace App\Notification\Slack;
55

66

7-
use App\Notification\AppNotification;
8-
use GuzzleHttp\Client;
9-
use Psr\Http\Message\ResponseInterface;
7+
use App\Notification\AppNotificationInterface;
8+
use App\Notification\Client\HTTPClientAdapterInterface;
9+
use App\Notification\Client\ResponseAdapterInterface;
1010

11-
final class SlackNotification implements AppNotification
11+
final class SlackNotification implements AppNotificationInterface
1212
{
1313
private SlackStylizedMessageCreator $message;
14+
private HTTPClientAdapterInterface $client;
1415

15-
public function __construct(string $message, string $messageType)
16+
public function __construct(HTTPClientAdapterInterface $client, string $message, string $messageType)
1617
{
1718
$this->message = new SlackStylizedMessageCreator($message, $messageType);
19+
$this->client = $client;
1820
}
1921

20-
public function notify(): ResponseInterface
22+
public function notify(): ResponseAdapterInterface
2123
{
22-
return (new Client())->post(getenv('SLACK_API_WEBHOOK'), [
23-
'json' => $this->message->getMessageStructure()
24-
]);
24+
return $this->client->post(
25+
getenv('SLACK_API_WEBHOOK'),
26+
$this->message->getStructuredMessage()
27+
);
2528
}
2629
}

app/Notification/Slack/SlackStylizedMessageCreator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private static function getErrorMessageStructure(string $message): array
5252
];
5353
}
5454

55-
public function getMessageStructure(): array
55+
public function getStructuredMessage(): array
5656
{
5757
return $this->stylizedMessageStructure;
5858
}

app/Route/Router.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace App\Route;
55

66

7+
use App\Notification\Client\Guzzle\GuzzleHTTPClient;
78
use App\Notification\NotificationTypeEnum;
89
use App\Notification\StatusCodeEnum;
910
use App\Notification\Slack\SlackNotification;
@@ -39,7 +40,11 @@ private static function createReflectionMethod(array $uriContent): ReflectionMet
3940
try {
4041
return new ReflectionMethod($uriContent['namespace'], $uriContent['method']);
4142
} catch (ReflectionException $exception) {
42-
(new SlackNotification($exception->getMessage(), NotificationTypeEnum::ERROR()))->notify();
43+
(new SlackNotification(
44+
new GuzzleHTTPClient(),
45+
$exception->getMessage(),
46+
NotificationTypeEnum::ERROR()
47+
))->notify();
4348
}
4449
}
4550
}

tests/Notification/Slack/SlackNotificationTest.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,35 @@
22

33
namespace App\Test\Notification\Slack;
44

5+
use App\Notification\Client\Guzzle\GuzzleHTTPClient;
6+
use App\Notification\Client\ResponseAdapterInterface;
57
use App\Notification\NotificationTypeEnum;
68
use App\Notification\Slack\SlackNotification;
79
use PHPUnit\Framework\Assert;
810
use PHPUnit\Framework\TestCase;
911

1012
require_once 'app/DotEnvLoader/index.php';
1113

14+
/**
15+
* @group notification
16+
*/
1217
class SlackNotificationTest extends TestCase
1318
{
14-
/**
15-
* @group notification
16-
*/
17-
public static function test_SlackNotification_ShouldNotify(): void
19+
public static function test_SlackNotificationInstance_ShouldAssertResponseAdapterInterfaceResponse(): void
1820
{
19-
$notification = new SlackNotification($notifyText = 'Test Notification', $messageType = NotificationTypeEnum::ERROR());
21+
$notification = new SlackNotification(
22+
new GuzzleHTTPClient(),
23+
$notifyText = 'Test Notification',
24+
$messageType = NotificationTypeEnum::ERROR()
25+
);
2026
$notificationResponse = $notification->notify();
2127

22-
$expectedStatusCodeOK = 200;
23-
$expectedReasonPhrase = 'OK';
28+
$expectedResponse = [
29+
'status_code' => 200,
30+
'message' => 'OK',
31+
];
2432

25-
Assert::assertEquals($expectedStatusCodeOK, $notificationResponse->getStatusCode());
26-
Assert::assertEquals($expectedReasonPhrase, $notificationResponse->getReasonPhrase());
33+
Assert::assertInstanceOf(ResponseAdapterInterface::class, $notificationResponse);
34+
Assert::assertEquals($expectedResponse, $notificationResponse->getResponse());
2735
}
2836
}

0 commit comments

Comments
 (0)