Skip to content

Commit

Permalink
v0.0.33
Browse files Browse the repository at this point in the history
- Bug fixes
  • Loading branch information
ewilan-riviere committed Oct 11, 2023
1 parent ed82318 commit cdb5e65
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 53 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
18 changes: 2 additions & 16 deletions src/Facades/Sentinel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
87 changes: 56 additions & 31 deletions src/Sentinel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions src/SentinelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
6 changes: 4 additions & 2 deletions tests/FailedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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();
Expand All @@ -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 () {
Expand Down
4 changes: 3 additions & 1 deletion tests/SentinelTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

use Kiwilan\Sentinel\Facades\Sentinel;
use Kiwilan\Sentinel\Sentinel as SentinelSentinel;

it('can use sentinel', function () {
$instance = Sentinel::make();
$instance = new SentinelSentinel();
$instance->setup();

expect($instance)->toBeInstanceOf(\Kiwilan\Sentinel\Sentinel::class);
expect($instance->token())->toBeString();
Expand Down

0 comments on commit cdb5e65

Please sign in to comment.