Skip to content

Commit

Permalink
Phpstan update #15
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoJaureRomero committed Aug 18, 2024
1 parent 53170b2 commit c73837c
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 47 deletions.
14 changes: 12 additions & 2 deletions src/Builder/ArrayFromAuditableContractBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class ArrayFromAuditableContractBuilder
{
protected string $className;
protected string $shortName;
protected string $default;
protected string $active;
protected bool $default;
protected bool $active;
protected string $version;
protected string $source;

Expand All @@ -27,6 +27,16 @@ public function __construct(
$this->source = $auditableModel->getSourceModelClass();
}

/**
* @return array{
* class_name: string,
* short_name: string,
* default: bool,
* active: bool,
* version: string,
* source_class: string
* }
*/
public function toArray(): array
{
return [
Expand Down
1 change: 1 addition & 0 deletions src/Console/Commands/AuditableClassCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function handle(
$auditableModels = $auditableModelsFromProject->getAuditableModels();
table(
headers: Headers::AUDITABLE_CLASSES_HEADER,
/** @phpstan-ignore-next-line */
rows: $auditableModels
);
foreach ($auditableModels as $auditableModel) {
Expand Down
12 changes: 9 additions & 3 deletions src/Console/Commands/AuditsFromClassCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,22 @@ public function __construct(
public function handle(
AuditableModelsFromProject $auditableModelsFromProject
): void {
$modelName = $this->argument('modelName');
if ($modelName === null) {
if ($$this->argument('modelName') === null) {
info('You must provide a model Name as an argument');
info($this->signature);
return;
}

/** @var string $modelName */
$modelName = $this->argument('modelName');

$auditableModels = $auditableModelsFromProject->getAuditableModels();

$modelClassName = '';
$modelFound = false;
foreach ($auditableModels as $auditableModel) {
if ($auditableModel['short_name'] === $modelName) {
/** @var string $modelClassName */
$modelClassName = $auditableModel['class_name'];
$modelFound = true;
}
Expand All @@ -61,8 +66,9 @@ public function handle(
info('Check eloise:audit:class command to see all auditable Models');
return;
}

/** @var int|null $modelId */
$modelId = $this->option('modelId');
/** @var int|null $userId */
$userId = $this->option('userId');

$this->getAuditsFromParameter($modelClassName, $modelId, $userId);
Expand Down
14 changes: 13 additions & 1 deletion src/Contracts/AuditableModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ public function activeAudit(): bool;
public function versionAudit(): string;
//public function authRequiredAudit(): bool;

//Methods comming from Model class
/**
* Methods comming from Model class
*
* @return array<string, mixed>
*/
public function getDirty();

/**
* Get the original value of an attribute.
*
* @param string $attribute
* @return mixed
*/
public function getOriginal(string $attribute);
}
2 changes: 1 addition & 1 deletion src/Listeners/LoggingAuditListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function __construct(
) {
}

public function onAudit(AuditEvent $event): void
public function handle(AuditEvent $event): void
{
$auditableModel = $event->getAuditableModel();
$action = $event->getAuditAction();
Expand Down
11 changes: 10 additions & 1 deletion src/Managers/AuditModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ public function createAudit(AuditableModel $auditableModel, string $action): Aud

$sourceClass = $auditableModel->getSourceModelClass();
$audit->source_class = $sourceClass;

/** @phpstan-ignore-next-line */
$audit->source_id = $auditableModel->id;

$audit->action = $action;
$audit->version = $auditableModel->versionAudit();

Expand All @@ -32,7 +35,13 @@ public function createAudit(AuditableModel $auditableModel, string $action): Aud
return $audit;
}

public function getChangesInAuditableModel($auditableModel): array
/**
* Get the changes in the auditable model.
*
* @param AuditableModel $auditableModel
* @return array<int, array<string, array<string, mixed>>>
*/
public function getChangesInAuditableModel(AuditableModel $auditableModel): array
{
$auditableModelChanges = $auditableModel->getDirty();

Expand Down
19 changes: 16 additions & 3 deletions src/Models/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
*/
class Audit extends Model
{
use HasFactory;

protected $table = 'eloise_audit';

protected $fillable = [
Expand All @@ -53,16 +51,25 @@ class Audit extends Model
'serialized_data' => 'array',
];

/**
* @return BelongsTo<User, Audit>
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}

/**
* @return BelongsTo<AuditAction, Audit>
*/
public function setAuditAction(): BelongsTo
{
return $this->belongsTo(AuditAction::class, 'eloise_audit_action_id');
}

/**
* @return array<int, mixed>
*/
public function toArrayForTable(): array
{
return [
Expand All @@ -80,13 +87,19 @@ public function toArrayForTable(): array
];
}

/**
* @return MorphTo<Model, Audit>
*/
public function source(): MorphTo
{
return $this->morphTo(__FUNCTION__, 'source_class', 'source_id');
}

/**
* @return MorphTo<Model, Audit>
*/
public function target(): MorphTo
{
return $this->morphTo(__FUNCTION__, 'source_class', 'source_id');
return $this->morphTo(__FUNCTION__, 'target_class', 'target_id');
}
}
5 changes: 3 additions & 2 deletions src/Models/AuditAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
*/
class AuditAction extends Model
{
use HasFactory;

protected $table = 'eloise_audit_action';

protected $fillable = [
Expand All @@ -40,6 +38,9 @@ class AuditAction extends Model
'serialized_data' => 'array',
];

/**
* @return BelongsTo<AuditableClass, AuditAction>
*/
public function setAuditableClass(): BelongsTo
{
return $this->belongsTo(AuditableClass::class, 'eloise_audit_class_id');
Expand Down
5 changes: 4 additions & 1 deletion src/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

class EventServiceProvider extends IlluminateEventServiceProvider
{
/**
* @var array<string, array<int, string>>
*/
protected $listen = [
AuditEvent::class => [
[LoggingAuditListener::class, 'onAudit'],
LoggingAuditListener::class,
],
];

Expand Down
2 changes: 1 addition & 1 deletion src/Queries/AuditQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function getAuditFromUserAndModelId(
): void {
$query = Audit::query();

$baseCondition = function ($q) use ($modelId, $userId, $modelName) {
$baseCondition = function ($q) use ($modelId, $modelName) {
if ($modelId !== null) {
$q->where('source_id', $modelId)->where('source_class', $modelName)
->orWhere(function ($subQuery) use ($modelId, $modelName) {
Expand Down
18 changes: 14 additions & 4 deletions src/Services/AuditableModelsFromProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Eloise\DataAudit\Services;

use Eloise\DataAudit\Builder\ArrayFromAuditableContractBuilder;
use Eloise\DataAudit\Contracts\AuditableModel as AuditableModelContract;
use Eloise\DataAudit\Constants\PathNames;
use Exception;
use Illuminate\Support\Facades\File;
Expand All @@ -11,10 +12,18 @@
class AuditableModelsFromProject
{
/**
* This method gets all models implementing AuditableModel Contract
*
* @throws Exception
*/
* This method gets all models implementing the AuditableModel contract.
*
* @return array<int,array{
* class_name: string,
* short_name: string,
* default: bool,
* active: bool,
* version: string,
* source_class: string
* }>
* @throws Exception
*/
public function getAuditableModels(): array
{
$auditableModels = [];
Expand All @@ -37,6 +46,7 @@ public function getAuditableModels(): array
continue;
}

/** @var AuditableModelContract $modelClass */
$modelClass = new $className();

$builder = new ArrayFromAuditableContractBuilder($modelClass);
Expand Down
54 changes: 53 additions & 1 deletion src/Services/LoadAuditableClassFromArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@

class LoadAuditableClassFromArray
{
/**
* Load or create an auditable class from an array.
*
* @param array{
* class_name: string,
* short_name: string,
* default: bool,
* active: bool,
* version: string,
* auditable_id?: int
* } $auditModel
* @return array{
* class_name: string,
* short_name: string,
* default: bool,
* active: bool,
* version: string,
* auditable_id: int
* }
*/
public function loadAuditableClass(array $auditModel): array
{
$auditableClass = AuditableClass::updateOrCreate(
Expand All @@ -25,8 +45,25 @@ public function loadAuditableClass(array $auditModel): array
return $auditModel;
}

/**
* Load actions for the auditable model.
*
* @param array{
* class_name: string,
* short_name: string,
* default: bool,
* active: bool,
* version: string,
* auditable_id: int
* } $auditModel
* @param string|null $action
* @return void
*/
public function loadActions(array $auditModel, string $action = null): void
{
// Ensure the 'source_class' key is added before passing it to updateOrCreateAction
$auditModel['source_class'] = $auditModel['class_name'];

$actionsArray = $auditModel['default'] ? Actions::DEFAULT_ACTIONS : [];
if ($action !== null && !in_array($action, $actionsArray)) {
$actionsArray[] = $action;
Expand All @@ -37,7 +74,22 @@ public function loadActions(array $auditModel, string $action = null): void
}
}

public function updateOrCreateAction(array $auditModel, string $action)
/**
* Update or create an audit action.
*
* @param array{
* class_name: string,
* short_name: string,
* default: bool,
* active: bool,
* version: string,
* auditable_id: int,
* source_class: string
* } $auditModel
* @param string $action
* @return void
*/
public function updateOrCreateAction(array $auditModel, string $action): void
{
AuditAction::updateOrCreate(
[
Expand Down
26 changes: 0 additions & 26 deletions src/Suppliers/AuditableClassSupplier.php

This file was deleted.

1 change: 0 additions & 1 deletion src/Suppliers/AuditableSupplier.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Eloise\DataAudit\Suppliers;

use Eloise\DataAudit\Constants\Actions;
use Eloise\DataAudit\Contracts\AuditableModel;
use Eloise\DataAudit\Models\AuditableClass;
use Eloise\DataAudit\Models\AuditAction;
Expand Down

0 comments on commit c73837c

Please sign in to comment.