diff --git a/README.md b/README.md index 99bc710..344f0e9 100644 --- a/README.md +++ b/README.md @@ -59,12 +59,15 @@ Due to the large amount of records anticipated to be created, you must create an return [ 'morphs' => [ - 0 => App\Models\User::class + 1 => App\Models\User::class ], ]; ``` +> [!CAUTION] +> `0` is not a valid key for the morph map, as it will get resolved to `false` in Laravel's morphing logic. + This points our `App\Models\User::class` to an enum (integer). This means our database is created with small integers vs large fully qualified namespaces. Recommended action is creating an enum class to describe all models in your system. If an integer mapping is not detected. The system will error out with an `\InvalidArgumentException`. diff --git a/src/Commands/AuditModelResolver.php b/src/Commands/AuditModelResolver.php index 3a91c14..ffcfd7f 100644 --- a/src/Commands/AuditModelResolver.php +++ b/src/Commands/AuditModelResolver.php @@ -3,6 +3,7 @@ namespace Sourcetoad\Logger\Commands; use Illuminate\Console\Command; +use Sourcetoad\Logger\Logger; use Sourcetoad\Logger\Helpers\AuditResolver; use Sourcetoad\Logger\Models\AuditChange; use Sourcetoad\Logger\Models\AuditModel; @@ -21,7 +22,7 @@ public function handle(): void $item->processed = true; $item->owner_id = $owner?->getKey(); - $item->owner_type = $owner?->getMorphClass(); + $item->owner_type = !is_null($owner) ? Logger::getNumericMorphMap($owner) : null; $item->saveOrFail(); } }); @@ -33,7 +34,7 @@ public function handle(): void $item->processed = true; $item->owner_id = $owner?->getKey(); - $item->owner_type = $owner?->getMorphClass(); + $item->owner_type = !is_null($owner) ? Logger::getNumericMorphMap($owner) : null; $item->saveOrFail(); } }); diff --git a/src/Logger.php b/src/Logger.php index 4f66f31..0250960 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -5,6 +5,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Request; +use InvalidArgumentException; use Sourcetoad\Logger\Enums\ActivityType; use Sourcetoad\Logger\Enums\HttpVerb; use Sourcetoad\Logger\Models\AuditActivity; @@ -124,7 +125,7 @@ public function logActivity(int $type, array $keys = []): AuditActivity $data[] = [ 'activity_id' => $activity->id, - 'entity_type' => $this->getNumericMorphMap($model), + 'entity_type' => static::getNumericMorphMap($model), 'entity_id' => $model->getKey(), 'key_id' => $keys->id, 'processed' => false, @@ -156,7 +157,7 @@ public function logActivity(int $type, array $keys = []): AuditActivity $data[] = [ 'activity_id' => $activity->id, - 'entity_type' => $this->getNumericMorphMap($model), + 'entity_type' => static::getNumericMorphMap($model), 'entity_id' => $model->getKey(), 'processed' => false, ]; @@ -171,7 +172,7 @@ public function logActivity(int $type, array $keys = []): AuditActivity // Private functions //-------------------------------------------------------------------------------------------------------------- - private function getNumericMorphMap(Model $model): int + public static function getNumericMorphMap(Model $model): int { $fcqn = get_class($model); $morphMap = LoggerServiceProvider::$morphs; @@ -181,11 +182,19 @@ private function getNumericMorphMap(Model $model): int $morphableTypeId = array_search($fcqn, $morphMap, true); } - if (is_numeric($morphableTypeId)) { - return $morphableTypeId; + if (!is_numeric($morphableTypeId)) { + throw new InvalidArgumentException( + sprintf('%s has no numeric model map. Check `morphs` in Logger.', get_class($model)), + ); } - throw new \InvalidArgumentException(get_class($model) . " has no numeric model map. Check `morphs` in Logger."); + if ($morphableTypeId === 0) { + throw new InvalidArgumentException( + sprintf('0 is not a valid morph map key for %s. Check `morphs` in Logger.', get_class($model)), + ); + } + + return $morphableTypeId; } private function getHttpVerb(string $verb): int