diff --git a/src/app/AuthAttempt.php b/src/app/AuthAttempt.php --- a/src/app/AuthAttempt.php +++ b/src/app/AuthAttempt.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Model; use Iatstuti\Database\Support\NullableFields; +use App\Traits\UuidKeyTrait; use Carbon\Carbon; /** @@ -14,6 +15,7 @@ class AuthAttempt extends Model { use NullableFields; + use UuidKeyTrait; // No specific reason public const REASON_NONE = ''; @@ -43,9 +45,6 @@ 'last_seen' => 'datetime' ]; - public $incrementing = false; - protected $keyType = 'string'; - /** * Prepare a date for array / JSON serialization. * diff --git a/src/app/Discount.php b/src/app/Discount.php --- a/src/app/Discount.php +++ b/src/app/Discount.php @@ -2,6 +2,7 @@ namespace App; +use App\Traits\UuidKeyTrait; use Illuminate\Database\Eloquent\Model; use Spatie\Translatable\HasTranslations; @@ -17,9 +18,7 @@ class Discount extends Model { use HasTranslations; - - public $incrementing = false; - protected $keyType = 'string'; + use UuidKeyTrait; protected $casts = [ 'discount' => 'integer', diff --git a/src/app/Entitlement.php b/src/app/Entitlement.php --- a/src/app/Entitlement.php +++ b/src/app/Entitlement.php @@ -5,6 +5,7 @@ use Carbon\Carbon; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; +use App\Traits\UuidKeyTrait; /** * The eloquent definition of an Entitlement. @@ -27,20 +28,7 @@ class Entitlement extends Model { use SoftDeletes; - - /** - * This table does not use auto-increment. - * - * @var boolean - */ - public $incrementing = false; - - /** - * The key type is actually a string. - * - * @var string - */ - protected $keyType = 'string'; + use UuidKeyTrait; /** * The fillable columns for this Entitlement diff --git a/src/app/Observers/AuthAttemptObserver.php b/src/app/Observers/AuthAttemptObserver.php deleted file mode 100644 --- a/src/app/Observers/AuthAttemptObserver.php +++ /dev/null @@ -1,31 +0,0 @@ -{$authAttempt->getKeyName()} = $allegedly_unique; - break; - } - } - } -} diff --git a/src/app/Observers/DiscountObserver.php b/src/app/Observers/DiscountObserver.php --- a/src/app/Observers/DiscountObserver.php +++ b/src/app/Observers/DiscountObserver.php @@ -18,14 +18,6 @@ */ public function creating(Discount $discount): void { - while (true) { - $allegedly_unique = \App\Utils::uuidStr(); - if (!Discount::find($allegedly_unique)) { - $discount->{$discount->getKeyName()} = $allegedly_unique; - break; - } - } - $discount->tenant_id = \config('app.tenant_id'); } } diff --git a/src/app/Observers/EntitlementObserver.php b/src/app/Observers/EntitlementObserver.php --- a/src/app/Observers/EntitlementObserver.php +++ b/src/app/Observers/EntitlementObserver.php @@ -45,14 +45,6 @@ return false; } - while (true) { - $allegedly_unique = \App\Utils::uuidStr(); - if (!Entitlement::withTrashed()->find($allegedly_unique)) { - $entitlement->{$entitlement->getKeyName()} = $allegedly_unique; - break; - } - } - return true; } diff --git a/src/app/Observers/PackageObserver.php b/src/app/Observers/PackageObserver.php --- a/src/app/Observers/PackageObserver.php +++ b/src/app/Observers/PackageObserver.php @@ -12,22 +12,12 @@ /** * Handle the "creating" event on an Package. * - * Ensures that the entry uses a custom ID (uuid). - * * @param Package $package The Package being created. * * @return void */ public function creating(Package $package) { - while (true) { - $allegedly_unique = \App\Utils::uuidStr(); - if (!Package::find($allegedly_unique)) { - $package->{$package->getKeyName()} = $allegedly_unique; - break; - } - } - $package->tenant_id = \config('app.tenant_id'); } } diff --git a/src/app/Observers/PlanObserver.php b/src/app/Observers/PlanObserver.php --- a/src/app/Observers/PlanObserver.php +++ b/src/app/Observers/PlanObserver.php @@ -20,14 +20,6 @@ */ public function creating(Plan $plan) { - while (true) { - $allegedly_unique = \App\Utils::uuidStr(); - if (!Plan::find($allegedly_unique)) { - $plan->{$plan->getKeyName()} = $allegedly_unique; - break; - } - } - $plan->tenant_id = \config('app.tenant_id'); } } diff --git a/src/app/Observers/SignupInvitationObserver.php b/src/app/Observers/SignupInvitationObserver.php --- a/src/app/Observers/SignupInvitationObserver.php +++ b/src/app/Observers/SignupInvitationObserver.php @@ -18,14 +18,6 @@ */ public function creating(SI $invitation) { - while (true) { - $allegedly_unique = \App\Utils::uuidStr(); - if (!SI::find($allegedly_unique)) { - $invitation->{$invitation->getKeyName()} = $allegedly_unique; - break; - } - } - $invitation->status = SI::STATUS_NEW; $invitation->tenant_id = \config('app.tenant_id'); diff --git a/src/app/Observers/SkuObserver.php b/src/app/Observers/SkuObserver.php --- a/src/app/Observers/SkuObserver.php +++ b/src/app/Observers/SkuObserver.php @@ -15,14 +15,6 @@ */ public function creating(Sku $sku) { - while (true) { - $allegedly_unique = \App\Utils::uuidStr(); - if (!Sku::find($allegedly_unique)) { - $sku->{$sku->getKeyName()} = $allegedly_unique; - break; - } - } - $sku->tenant_id = \config('app.tenant_id'); } } diff --git a/src/app/Observers/TransactionObserver.php b/src/app/Observers/TransactionObserver.php --- a/src/app/Observers/TransactionObserver.php +++ b/src/app/Observers/TransactionObserver.php @@ -15,14 +15,6 @@ */ public function creating(Transaction $transaction): void { - while (true) { - $allegedly_unique = \App\Utils::uuidStr(); - if (!Transaction::find($allegedly_unique)) { - $transaction->{$transaction->getKeyName()} = $allegedly_unique; - break; - } - } - if (!isset($transaction->user_email)) { $transaction->user_email = \App\Utils::userEmailOrNull(); } diff --git a/src/app/Observers/WalletObserver.php b/src/app/Observers/WalletObserver.php --- a/src/app/Observers/WalletObserver.php +++ b/src/app/Observers/WalletObserver.php @@ -18,14 +18,6 @@ */ public function creating(Wallet $wallet) { - while (true) { - $allegedly_unique = \App\Utils::uuidStr(); - if (!Wallet::find($allegedly_unique)) { - $wallet->{$wallet->getKeyName()} = $allegedly_unique; - break; - } - } - $wallet->currency = \config('app.currency'); } diff --git a/src/app/Package.php b/src/app/Package.php --- a/src/app/Package.php +++ b/src/app/Package.php @@ -2,6 +2,7 @@ namespace App; +use App\Traits\UuidKeyTrait; use Illuminate\Database\Eloquent\Model; use Spatie\Translatable\HasTranslations; @@ -32,9 +33,7 @@ class Package extends Model { use HasTranslations; - - public $incrementing = false; - protected $keyType = 'string'; + use UuidKeyTrait; public $timestamps = false; diff --git a/src/app/Plan.php b/src/app/Plan.php --- a/src/app/Plan.php +++ b/src/app/Plan.php @@ -2,6 +2,7 @@ namespace App; +use App\Traits\UuidKeyTrait; use Illuminate\Database\Eloquent\Model; use Spatie\Translatable\HasTranslations; @@ -27,9 +28,8 @@ class Plan extends Model { use HasTranslations; + use UuidKeyTrait; - public $incrementing = false; - protected $keyType = 'string'; public $timestamps = false; protected $fillable = [ diff --git a/src/app/Providers/AppServiceProvider.php b/src/app/Providers/AppServiceProvider.php --- a/src/app/Providers/AppServiceProvider.php +++ b/src/app/Providers/AppServiceProvider.php @@ -44,7 +44,6 @@ */ public function boot() { - \App\AuthAttempt::observe(\App\Observers\AuthAttemptObserver::class); \App\Discount::observe(\App\Observers\DiscountObserver::class); \App\Domain::observe(\App\Observers\DomainObserver::class); \App\Entitlement::observe(\App\Observers\EntitlementObserver::class); diff --git a/src/app/SignupInvitation.php b/src/app/SignupInvitation.php --- a/src/app/SignupInvitation.php +++ b/src/app/SignupInvitation.php @@ -4,6 +4,7 @@ use Carbon\Carbon; use Illuminate\Database\Eloquent\Model; +use App\Traits\UuidKeyTrait; /** * The eloquent definition of a signup invitation. @@ -16,6 +17,8 @@ */ class SignupInvitation extends Model { + use UuidKeyTrait; + // just created public const STATUS_NEW = 1 << 0; // it's been sent successfully @@ -26,20 +29,6 @@ public const STATUS_COMPLETED = 1 << 3; - /** - * Indicates if the IDs are auto-incrementing. - * - * @var bool - */ - public $incrementing = false; - - /** - * The "type" of the auto-incrementing ID. - * - * @var string - */ - protected $keyType = 'string'; - /** * The attributes that are mass assignable. * diff --git a/src/app/Sku.php b/src/app/Sku.php --- a/src/app/Sku.php +++ b/src/app/Sku.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Model; use Spatie\Translatable\HasTranslations; +use App\Traits\UuidKeyTrait; /** * The eloquent definition of a Stock Keeping Unit (SKU). @@ -23,9 +24,7 @@ class Sku extends Model { use HasTranslations; - - public $incrementing = false; - protected $keyType = 'string'; + use UuidKeyTrait; protected $casts = [ 'units_free' => 'integer' diff --git a/src/app/Traits/UuidKeyTrait.php b/src/app/Traits/UuidKeyTrait.php new file mode 100644 --- /dev/null +++ b/src/app/Traits/UuidKeyTrait.php @@ -0,0 +1,46 @@ +{$model->getKeyName()})) { + while (true) { + $allegedly_unique = \App\Utils::uuidStr(); + //FIXME Entitlement::withTrashed()->find (it shouldn't really matter because uuidv4, but we can solve it anyways) + if (!$model->find($allegedly_unique)) { + $model->{$model->getKeyName()} = $allegedly_unique; + break; + } + } + } + }); + } + + /** + * Get if the key is incrementing. + * + * @return bool + */ + public function getIncrementing() + { + return false; + } + + /** + * Get the key type. + * + * @return string + */ + public function getKeyType() + { + return 'string'; + } +} diff --git a/src/app/Transaction.php b/src/app/Transaction.php --- a/src/app/Transaction.php +++ b/src/app/Transaction.php @@ -4,6 +4,7 @@ use App\Entitlement; use App\Wallet; +use App\Traits\UuidKeyTrait; use Illuminate\Database\Eloquent\Model; /** @@ -20,6 +21,8 @@ */ class Transaction extends Model { + use UuidKeyTrait; + public const ENTITLEMENT_BILLED = 'billed'; public const ENTITLEMENT_CREATED = 'created'; public const ENTITLEMENT_DELETED = 'deleted'; @@ -56,12 +59,6 @@ 'amount' => 'integer', ]; - /** @var boolean This model uses an automatically incrementing integer primary key? */ - public $incrementing = false; - - /** @var string The type of the primary key */ - protected $keyType = 'string'; - /** * Returns the entitlement to which the transaction is assigned (if any) diff --git a/src/app/Wallet.php b/src/app/Wallet.php --- a/src/app/Wallet.php +++ b/src/app/Wallet.php @@ -4,6 +4,7 @@ use App\User; use App\Traits\SettingsTrait; +use App\Traits\UuidKeyTrait; use Carbon\Carbon; use Iatstuti\Database\Support\NullableFields; use Illuminate\Database\Eloquent\Model; @@ -25,9 +26,7 @@ { use NullableFields; use SettingsTrait; - - public $incrementing = false; - protected $keyType = 'string'; + use UuidKeyTrait; public $timestamps = false;