forked from auth0/laravel-auth0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvents.php
102 lines (86 loc) · 2.94 KB
/
Events.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
declare(strict_types=1);
namespace Auth0\Laravel;
use Auth0\Laravel\Events\Configuration\{
BuildingConfigurationEvent,
BuiltConfigurationEvent,
};
use Auth0\Laravel\Events\Middleware\{
StatefulMiddlewareRequest,
StatelessMiddlewareRequest,
};
use Auth0\Laravel\Events\{
AuthenticationFailed,
AuthenticationSucceeded,
EventContract,
LoginAttempting,
TokenExpired,
TokenRefreshFailed,
TokenRefreshSucceeded,
TokenVerificationAttempting,
TokenVerificationFailed,
TokenVerificationSucceeded,
};
/**
* @api
*
* @codeCoverageIgnore
*/
final class Events implements EventsContract
{
/**
* @var string
*/
private const TELESCOPE = '\Laravel\Telescope\Telescope';
public static bool $enabled = true;
public static function dispatch(EventContract $event): void
{
if (self::$enabled) {
if (self::withoutTelescopeRecording(self::getName($event))) {
self::TELESCOPE::withoutRecording(static fn (): mixed => event(self::getName($event), $event));
return;
}
event(self::getName($event), $event);
}
}
public static function framework(object $event): void
{
if (self::$enabled) {
if (self::withoutTelescopeRecording($event::class)) {
self::TELESCOPE::withoutRecording(static fn (): mixed => event($event));
return;
}
event($event);
}
}
private static function getName(EventContract $event): string
{
return match ($event::class) {
AuthenticationFailed::class => self::AUTHENTICATION_FAILED,
AuthenticationSucceeded::class => self::AUTHENTICATION_SUCCEEDED,
BuildingConfigurationEvent::class => self::CONFIGURATION_BUILDING,
BuiltConfigurationEvent::class => self::CONFIGURATION_BUILT,
LoginAttempting::class => self::LOGIN_ATTEMPTING,
StatefulMiddlewareRequest::class => self::MIDDLEWARE_STATEFUL_REQUEST,
StatelessMiddlewareRequest::class => self::MIDDLEWARE_STATELESS_REQUEST,
TokenExpired::class => self::TOKEN_EXPIRED,
TokenRefreshFailed::class => self::TOKEN_REFRESH_FAILED,
TokenRefreshSucceeded::class => self::TOKEN_REFRESH_SUCCEEDED,
TokenVerificationAttempting::class => self::TOKEN_VERIFICATION_ATTEMPTING,
TokenVerificationFailed::class => self::TOKEN_VERIFICATION_FAILED,
TokenVerificationSucceeded::class => self::TOKEN_VERIFICATION_SUCCEEDED,
default => $event::class,
};
}
private static function withoutTelescopeRecording(string $event): bool
{
if (! class_exists(self::TELESCOPE)) {
return false;
}
return match ($event) {
self::CONFIGURATION_BUILDING => true,
self::CONFIGURATION_BUILT => true,
default => false,
};
}
}