Skip to content

Commit 5cef392

Browse files
committed
Add Slack integration
1 parent 000b4f1 commit 5cef392

File tree

15 files changed

+689
-68
lines changed

15 files changed

+689
-68
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SLACK_API_WEBHOOK=Your_webhook_here

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1+
# FOLDERS
12
.idea
23
vendor
4+
5+
# FILES
6+
.env
7+
.phpunit.result.cache

app/DotEnvLoader/index.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
$dotenv = \Dotenv\Dotenv::createImmutable(__DIR__ . '/../../');
4+
$dotenv->load();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
4+
namespace App\Notification;
5+
6+
7+
use Psr\Http\Message\ResponseInterface;
8+
9+
interface AppNotification
10+
{
11+
public function __construct(string $message, string $messageType);
12+
13+
public function notify(): ResponseInterface;
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
4+
namespace App\Notification;
5+
6+
/**
7+
* Class MessageTypeEnum
8+
*
9+
* @package App\Notification
10+
* @method static string ERROR()
11+
*/
12+
final class MessageTypeEnum
13+
{
14+
private const ERROR = 'error';
15+
16+
public static function __callStatic($name, $arguments): string
17+
{
18+
return constant('self::' . strtoupper($name));
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
4+
namespace App\Notification;
5+
6+
/**
7+
* Class MessageTypeEnum
8+
*
9+
* @package App\Notification
10+
* @method static int NOT_FOUND()
11+
*/
12+
final class StatusCodeEnum
13+
{
14+
private const NOT_FOUND = 404;
15+
16+
public static function __callStatic($name, $arguments): int
17+
{
18+
return constant('self::' . $name);
19+
}
20+
}

app/Route/Router.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,26 @@
44
namespace App\Route;
55

66

7+
use App\Notification\MessageTypeEnum;
8+
use App\Notification\StatusCodeEnum;
9+
use App\Slack\SlackNotification;
710
use InvalidArgumentException;
811
use ReflectionException;
912
use ReflectionMethod;
1013

11-
class Router implements RouterInterface
14+
final class Router implements RouterInterface
1215
{
13-
private const NOT_FOUND_CODE = 204;
1416
private array $routes = [];
1517

1618
public function dispatch(string $uri, array $params = []): void
1719
{
1820
if (key_exists($uri, $this->routes)) {
1921
$uriContent = $this->routes[$uri];
2022
$reflectedControllerMethod = self::createReflectionMethod($uriContent);
21-
2223
$reflectedControllerMethod->invokeArgs(new $uriContent['namespace'], $params);
2324
}
2425

25-
throw new InvalidArgumentException("'{$uri}' is an unregistered route", self::NOT_FOUND_CODE);
26+
throw new InvalidArgumentException("'{$uri}' is an unregistered route", StatusCodeEnum::NOT_FOUND());
2627
}
2728

2829
public function registry(string $uri, string $controller, string $method): void
@@ -38,7 +39,7 @@ private static function createReflectionMethod(array $uriContent): ReflectionMet
3839
try {
3940
return new ReflectionMethod($uriContent['namespace'], $uriContent['method']);
4041
} catch (ReflectionException $exception) {
41-
print $exception->getMessage();
42+
(new SlackNotification($exception->getMessage(), MessageTypeEnum::ERROR()))->notify();
4243
}
4344
}
4445
}

app/Slack/SlackNotification.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
4+
namespace App\Slack;
5+
6+
7+
use App\Notification\AppNotification;
8+
use GuzzleHttp\Client;
9+
use Psr\Http\Message\ResponseInterface;
10+
11+
final class SlackNotification implements AppNotification
12+
{
13+
private SlackStylizedMessageCreator $message;
14+
15+
public function __construct(string $message, string $messageType)
16+
{
17+
$this->message = new SlackStylizedMessageCreator($message, $messageType);
18+
}
19+
20+
public function notify(): ResponseInterface
21+
{
22+
return (new Client())->post(getenv('SLACK_API_WEBHOOK'), [
23+
'json' => $this->message->getMessageStructure()
24+
]);
25+
}
26+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
4+
namespace App\Slack;
5+
6+
7+
use App\Notification\MessageTypeEnum;
8+
use App\Notification\StatusCodeEnum;
9+
use InvalidArgumentException;
10+
11+
final class SlackStylizedMessageCreator
12+
{
13+
private array $stylizedMessageStructure;
14+
15+
public function __construct(string $message, string $messageType)
16+
{
17+
$this->stylizedMessageStructure = self::getSlylizedMessageByType($message, $messageType);
18+
}
19+
20+
private static function getSlylizedMessageByType(string $message, string $messageType): array
21+
{
22+
switch ($messageType) {
23+
case MessageTypeEnum::ERROR():
24+
return self::getErrorMessageStructure($message);
25+
default:
26+
throw new InvalidArgumentException("Invalid message type: '{$messageType}'", StatusCodeEnum::NOT_FOUND());
27+
}
28+
}
29+
30+
private static function getErrorMessageStructure(string $message): array
31+
{
32+
$currentDate = date('d/m/Y H:i:s');
33+
34+
return [
35+
'text' => true,
36+
'blocks' => [
37+
[
38+
'type' => 'section',
39+
'text' => [
40+
'type' => 'mrkdwn',
41+
'text' => "[{$currentDate}] " . strtoupper(MessageTypeEnum::ERROR()) . ' :this-is-fine-fire:'
42+
]
43+
],
44+
[
45+
'type' => 'section',
46+
'text' => [
47+
'type' => 'mrkdwn',
48+
'text' => $message
49+
]
50+
]
51+
]
52+
];
53+
}
54+
55+
public function getMessageStructure(): array
56+
{
57+
return $this->stylizedMessageStructure;
58+
}
59+
}

bootstrap/index.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
<?php
22

33
require_once __DIR__ . '/../app/Route/index.php';
4+
5+
require_once __DIR__ . '/../app/DotEnvLoader/index.php';

0 commit comments

Comments
 (0)