Skip to content

Commit 2ad9e10

Browse files
authored
Merge pull request #55 from sourcetoad/fix-owner-morphs
Fix Owner Morph Class Resolution and Add Check for `0` in Morph Map Keys
2 parents 294cb39 + e990adb commit 2ad9e10

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,15 @@ Due to the large amount of records anticipated to be created, you must create an
5959
return [
6060

6161
'morphs' => [
62-
0 => App\Models\User::class
62+
1 => App\Models\User::class
6363
],
6464

6565
];
6666
```
6767

68+
> [!CAUTION]
69+
> `0` is not a valid key for the morph map, as it will get resolved to `false` in Laravel's morphing logic.
70+
6871
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.
6972

7073
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`.

src/Commands/AuditModelResolver.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Sourcetoad\Logger\Commands;
44

55
use Illuminate\Console\Command;
6+
use Sourcetoad\Logger\Logger;
67
use Sourcetoad\Logger\Helpers\AuditResolver;
78
use Sourcetoad\Logger\Models\AuditChange;
89
use Sourcetoad\Logger\Models\AuditModel;
@@ -21,7 +22,7 @@ public function handle(): void
2122

2223
$item->processed = true;
2324
$item->owner_id = $owner?->getKey();
24-
$item->owner_type = $owner?->getMorphClass();
25+
$item->owner_type = !is_null($owner) ? Logger::getNumericMorphMap($owner) : null;
2526
$item->saveOrFail();
2627
}
2728
});
@@ -33,7 +34,7 @@ public function handle(): void
3334

3435
$item->processed = true;
3536
$item->owner_id = $owner?->getKey();
36-
$item->owner_type = $owner?->getMorphClass();
37+
$item->owner_type = !is_null($owner) ? Logger::getNumericMorphMap($owner) : null;
3738
$item->saveOrFail();
3839
}
3940
});

src/Logger.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Support\Facades\Request;
8+
use InvalidArgumentException;
89
use Sourcetoad\Logger\Enums\ActivityType;
910
use Sourcetoad\Logger\Enums\HttpVerb;
1011
use Sourcetoad\Logger\Models\AuditActivity;
@@ -124,7 +125,7 @@ public function logActivity(int $type, array $keys = []): AuditActivity
124125

125126
$data[] = [
126127
'activity_id' => $activity->id,
127-
'entity_type' => $this->getNumericMorphMap($model),
128+
'entity_type' => static::getNumericMorphMap($model),
128129
'entity_id' => $model->getKey(),
129130
'key_id' => $keys->id,
130131
'processed' => false,
@@ -156,7 +157,7 @@ public function logActivity(int $type, array $keys = []): AuditActivity
156157

157158
$data[] = [
158159
'activity_id' => $activity->id,
159-
'entity_type' => $this->getNumericMorphMap($model),
160+
'entity_type' => static::getNumericMorphMap($model),
160161
'entity_id' => $model->getKey(),
161162
'processed' => false,
162163
];
@@ -171,7 +172,7 @@ public function logActivity(int $type, array $keys = []): AuditActivity
171172
// Private functions
172173
//--------------------------------------------------------------------------------------------------------------
173174

174-
private function getNumericMorphMap(Model $model): int
175+
public static function getNumericMorphMap(Model $model): int
175176
{
176177
$fcqn = get_class($model);
177178
$morphMap = LoggerServiceProvider::$morphs;
@@ -181,11 +182,19 @@ private function getNumericMorphMap(Model $model): int
181182
$morphableTypeId = array_search($fcqn, $morphMap, true);
182183
}
183184

184-
if (is_numeric($morphableTypeId)) {
185-
return $morphableTypeId;
185+
if (!is_numeric($morphableTypeId)) {
186+
throw new InvalidArgumentException(
187+
sprintf('%s has no numeric model map. Check `morphs` in Logger.', get_class($model)),
188+
);
186189
}
187190

188-
throw new \InvalidArgumentException(get_class($model) . " has no numeric model map. Check `morphs` in Logger.");
191+
if ($morphableTypeId === 0) {
192+
throw new InvalidArgumentException(
193+
sprintf('0 is not a valid morph map key for %s. Check `morphs` in Logger.', get_class($model)),
194+
);
195+
}
196+
197+
return $morphableTypeId;
189198
}
190199

191200
private function getHttpVerb(string $verb): int

0 commit comments

Comments
 (0)