From cdb5e658a9e1cab3adc77fdc22a5638faa788133 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ewilan=20Rivi=C3=A8re?= Date: Wed, 11 Oct 2023 17:03:46 +0200 Subject: [PATCH] v0.0.33 - Bug fixes --- composer.json | 2 +- src/Facades/Sentinel.php | 18 +------ src/Sentinel.php | 87 +++++++++++++++++++++------------ src/SentinelServiceProvider.php | 4 +- tests/FailedTest.php | 6 ++- tests/SentinelTest.php | 4 +- 6 files changed, 68 insertions(+), 53 deletions(-) diff --git a/composer.json b/composer.json index 6546b72..49e2b0d 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "kiwilan/sentinel-laravel", "description": "PHP package for Laravel to send errors to Sentinel.", - "version": "0.0.32", + "version": "0.0.33", "keywords": [ "kiwilan", "laravel", diff --git a/src/Facades/Sentinel.php b/src/Facades/Sentinel.php index 29f3949..8ef6642 100644 --- a/src/Facades/Sentinel.php +++ b/src/Facades/Sentinel.php @@ -5,23 +5,9 @@ use Illuminate\Support\Facades\Facade; /** - * @method static self make() Get Sentinel instance. - * @method static array|false register(\Throwable $e) Register exception in Sentinel, return `false` if Sentinel is disabled. - * @method static ?string token() Get Sentinel application token. - * @method static ?string host() Get Sentinel host. - * @method static bool enabled() Know if Sentinel is enabled. - * @method static bool throwErrors() Know if Sentinel is throwing errors. - * @method static int status() Get response status. - * @method static array payload() Get response payload. - * @method static array response() Get response. - * @method static array body() Get response body. - * @method static string message() Get response message. - * @method static ?string json() Get response json. - * @method static ?LogHandler error() Get LogHandler instance. - * @method static ?string user() Get authenticated user. - * @method static array toArray() Get Sentinel instance as array. + * @method static array|false register(\Throwable $e, bool $throwErrors = false) Register exception in Sentinel, return `false` if Sentinel is disabled. If you want to throw Sentinel errors for debug, set `$throwErrors` to `true`. * - * @see \Kiwilan\Sentinel\Sentinel::class + * @see \Kiwilan\Sentinel\Sentinel */ class Sentinel extends Facade { diff --git a/src/Sentinel.php b/src/Sentinel.php index 6d70ef2..ddba4dd 100755 --- a/src/Sentinel.php +++ b/src/Sentinel.php @@ -4,13 +4,14 @@ use Exception; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Log; use Kiwilan\Sentinel\Log\LogHandler; use Throwable; class Sentinel { - protected function __construct( + public function __construct( protected ?string $token = null, protected ?string $host = null, protected bool $enabled = false, @@ -22,37 +23,56 @@ protected function __construct( protected string $json = '', protected ?string $message = 'Unknown error', protected ?LogHandler $error = null, + protected bool $isAuth = false, protected ?string $user = null, ) { } - public static function make(): self + /** + * Setup Sentinel. + */ + public function setup(): self { $baseURL = SentinelConfig::host(); - $token = SentinelConfig::token(); + $this->token = SentinelConfig::token(); + $this->host = "{$baseURL}/api/logs"; + $this->enabled = SentinelConfig::enabled(); - return new self( - token: $token, - host: "{$baseURL}/api/logs", - enabled: SentinelConfig::enabled(), - throwErrors: SentinelConfig::debug(), - ); + return $this; } - public function register(Throwable $e): array|false + /** + * Register exception in Sentinel, return `false` if Sentinel is disabled. + * + * @param \Throwable $e From `app/Exceptions/Handler.php` + * @param bool $throwErrors If you want to throw Sentinel errors for debug, set `$throwErrors` to `true`. + */ + public function register(Throwable $e, bool $throwErrors = false): array|false { - if (! $this->enabled) { - return false; - } + $this->setup(); - $this->error = LogHandler::make($e); - $this->user = $this->setUser(); + try { + $this->throwErrors = $throwErrors; - if (! $this->token) { - $this->pushError('Sentinel token is not set', 500); - } + if (! $this->enabled) { + return false; + } + + $this->error = LogHandler::make($e); + $this->user = $this->setUser(); + + if (! $this->token) { + $this->pushError('Sentinel token is not set', 500); + } - $this->payload = $this->setPayload(); + $this->payload = $this->setPayload(); + } catch (\Throwable $th) { + if ($this->throwErrors) { + throw $th; + } + + return false; + } return $this->send(); } @@ -157,10 +177,10 @@ private function send(): array 'json' => $body, ]; - $this->status = $this->response['status']; - $this->body = $this->response['body'] ?? []; - $this->json = $this->response['json']; - $this->message = $this->body['message'] ?? null; + $this->status = $this->response['status'] ?: 0; + $this->body = $this->response['body'] ?: []; + $this->json = $this->response['json'] ?: ''; + $this->message = $this->body['message'] ?: null; if ($body === false) { $this->pushError("Sentinel error {$this->status}: {$this->json}", $this->status); @@ -194,16 +214,21 @@ private function setUser(): ?string { $user = null; - if (auth()->check()) { - $id = auth()->user()->getAuthIdentifierName(); + try { + $this->isAuth = Auth::check(); + + if (Auth::check()) { + $id = Auth::user()?->getAuthIdentifierName(); - /** @var Model */ - $auth = auth()->user(); - $user = $auth->toArray()[$id] ?? null; + /** @var Model */ + $auth = auth()->user(); + $user = $auth->toArray()[$id] ?? null; - if ($user) { - $user = "{$id}: {$user}"; + if ($user) { + $user = "{$id}: {$user}"; + } } + } catch (\Throwable $th) { } return $user; @@ -217,7 +242,7 @@ private function setPayload(): array 'env' => $this->error->env(), 'laravel_version' => app()->version(), 'php_version' => phpversion(), - 'is_auth' => auth()->check(), + 'is_auth' => $this->isAuth, 'user' => $this->user, 'is_production' => $this->error->isProduction(), 'url' => $this->error->url(), diff --git a/src/SentinelServiceProvider.php b/src/SentinelServiceProvider.php index 2c62bd0..66abe1e 100644 --- a/src/SentinelServiceProvider.php +++ b/src/SentinelServiceProvider.php @@ -23,8 +23,8 @@ public function configurePackage(Package $package): void ->hasCommand(SentinelTestCommand::class); } - public function bootingPackage() + public function registeringPackage() { - $this->app->bind('sentinel', fn () => \Kiwilan\Sentinel\Sentinel::make()); + $this->app->bind('sentinel', \Kiwilan\Sentinel\Sentinel::class); } } diff --git a/tests/FailedTest.php b/tests/FailedTest.php index 91d4318..595db33 100644 --- a/tests/FailedTest.php +++ b/tests/FailedTest.php @@ -3,6 +3,7 @@ use Kiwilan\Sentinel\Commands\SentinelInstallCommand; use Kiwilan\Sentinel\Commands\SentinelTestCommand; use Kiwilan\Sentinel\Facades\Sentinel; +use Kiwilan\Sentinel\Sentinel as SentinelSentinel; use function Pest\Laravel\artisan; beforeEach(function () { @@ -17,7 +18,8 @@ }); it('can use sentinel', function () { - $instance = Sentinel::make(); + $instance = new SentinelSentinel(); + $instance->setup(); expect($instance->enabled())->toBeFalse(); expect($instance->host())->toBeString(); @@ -42,7 +44,7 @@ config(['sentinel.debug' => true]); $exception = new \Exception('This is a test exception', 500); - expect(fn () => Sentinel::register($exception))->toThrow(\Exception::class); + expect(fn () => Sentinel::register($exception, true))->toThrow(\Exception::class); }); it('can fail on sentinel host', function () { diff --git a/tests/SentinelTest.php b/tests/SentinelTest.php index f98b18f..025b481 100644 --- a/tests/SentinelTest.php +++ b/tests/SentinelTest.php @@ -1,9 +1,11 @@ setup(); expect($instance)->toBeInstanceOf(\Kiwilan\Sentinel\Sentinel::class); expect($instance->token())->toBeString();