Page MenuHomePhorge

D4863.1775252115.diff
No OneTemporary

Authored By
Unknown
Size
5 KB
Referenced Files
None
Subscribers
None

D4863.1775252115.diff

diff --git a/src/app/Console/Command.php b/src/app/Console/Command.php
--- a/src/app/Console/Command.php
+++ b/src/app/Console/Command.php
@@ -266,18 +266,6 @@
}
}
- /**
- * Checks that a model is soft-deletable
- *
- * @param string $class Model class name
- *
- * @return bool
- */
- protected function isSoftDeletable($class)
- {
- return class_exists($class) && method_exists($class, 'forceDelete');
- }
-
/**
* Return a string for output, with any additional attributes specified as well.
*
diff --git a/src/app/Console/ObjectDeleteCommand.php b/src/app/Console/ObjectDeleteCommand.php
--- a/src/app/Console/ObjectDeleteCommand.php
+++ b/src/app/Console/ObjectDeleteCommand.php
@@ -17,7 +17,7 @@
$this->objectName
);
- if ($this->isSoftDeletable($this->objectClass)) {
+ if (\App\Utils::isSoftDeletable($this->objectClass)) {
$this->signature .= " {--with-deleted : Consider deleted {$this->objectName}s}";
}
diff --git a/src/app/Console/ObjectListCommand.php b/src/app/Console/ObjectListCommand.php
--- a/src/app/Console/ObjectListCommand.php
+++ b/src/app/Console/ObjectListCommand.php
@@ -24,7 +24,7 @@
$this->signature .= " {--tenant= : Limit results to the specified tenant}";
}
- if ($this->isSoftDeletable($this->objectClass)) {
+ if (\App\Utils::isSoftDeletable($this->objectClass)) {
$this->signature .= " {--with-deleted : Include deleted {$this->objectName}s}";
}
@@ -41,7 +41,8 @@
*/
public function handle()
{
- if ($this->isSoftDeletable($this->objectClass) && $this->option('with-deleted')) {
+ // @phpstan-ignore-next-line
+ if (\App\Utils::isSoftDeletable($this->objectClass) && $this->option('with-deleted')) {
$objects = $this->objectClass::withTrashed();
} else {
$objects = new $this->objectClass();
diff --git a/src/app/Console/ObjectRelationListCommand.php b/src/app/Console/ObjectRelationListCommand.php
--- a/src/app/Console/ObjectRelationListCommand.php
+++ b/src/app/Console/ObjectRelationListCommand.php
@@ -54,7 +54,7 @@
$this->objectRelationClass = "App\\" . rtrim(ucfirst($this->objectRelation), 's');
}
- if ($this->isSoftDeletable($this->objectRelationClass)) {
+ if (\App\Utils::isSoftDeletable($this->objectRelationClass)) {
$this->signature .= " {--with-deleted : Include deleted objects}";
}
@@ -99,7 +99,7 @@
|| ($result instanceof \Illuminate\Database\Eloquent\Builder)
) {
// @phpstan-ignore-next-line
- if ($this->isSoftDeletable($this->objectRelationClass) && $this->option('with-deleted')) {
+ if (\App\Utils::isSoftDeletable($this->objectRelationClass) && $this->option('with-deleted')) {
$result->withoutGlobalScope(SoftDeletingScope::class);
}
diff --git a/src/app/Traits/UuidStrKeyTrait.php b/src/app/Traits/UuidStrKeyTrait.php
--- a/src/app/Traits/UuidStrKeyTrait.php
+++ b/src/app/Traits/UuidStrKeyTrait.php
@@ -14,14 +14,13 @@
$allegedly_unique = \App\Utils::uuidStr();
// Verify if unique
- if (in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($model))) {
- while ($model->withTrashed()->find($allegedly_unique)) {
- $allegedly_unique = \App\Utils::uuidStr();
- }
- } else {
- while ($model->find($allegedly_unique)) {
- $allegedly_unique = \App\Utils::uuidStr();
- }
+ $finder = $model;
+ if (\App\Utils::isSoftDeletable($model)) {
+ $finder = $finder->withTrashed();
+ }
+
+ while ($finder->find($allegedly_unique)) {
+ $allegedly_unique = \App\Utils::uuidStr();
}
$model->{$model->getKeyName()} = $allegedly_unique;
diff --git a/src/app/Utils.php b/src/app/Utils.php
--- a/src/app/Utils.php
+++ b/src/app/Utils.php
@@ -346,6 +346,20 @@
return $lastaddrstr;
}
+ /**
+ * Checks that a model is soft-deletable
+ *
+ * @param mixed $model Model object or a class name
+ */
+ public static function isSoftDeletable($model): bool
+ {
+ if (is_string($model) && !class_exists($model)) {
+ return false;
+ }
+
+ return method_exists($model, 'restore');
+ }
+
/**
* Normalize an email address.
*
diff --git a/src/tests/Unit/UtilsTest.php b/src/tests/Unit/UtilsTest.php
--- a/src/tests/Unit/UtilsTest.php
+++ b/src/tests/Unit/UtilsTest.php
@@ -76,6 +76,18 @@
$this->assertSame(['anyone, lrswitednp'], Utils::ensureAclPostPermission($acl));
}
+ /**
+ * Test for Utils::isSoftDeletable()
+ */
+ public function testIsSoftDeletable(): void
+ {
+ $this->assertTrue(Utils::isSoftDeletable(\App\User::class));
+ $this->assertFalse(Utils::isSoftDeletable(\App\Wallet::class));
+
+ $this->assertTrue(Utils::isSoftDeletable(new \App\User()));
+ $this->assertFalse(Utils::isSoftDeletable(new \App\Wallet()));
+ }
+
/**
* Test for Utils::money()
*/

File Metadata

Mime Type
text/plain
Expires
Fri, Apr 3, 9:35 PM (21 h, 26 m ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18797793
Default Alt Text
D4863.1775252115.diff (5 KB)

Event Timeline