111 lines
2.7 KiB
PHP
111 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Contracts\Encryption\DecryptException;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Support\Facades\Crypt;
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
|
|
class Brand extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'contact_mail',
|
|
'rcs',
|
|
'logo',
|
|
'crm_token',
|
|
'crm_password'
|
|
];
|
|
|
|
|
|
public function campaigns(): HasMany
|
|
{
|
|
return $this->hasMany(Campaign::class);
|
|
}
|
|
|
|
/**
|
|
* Interact with the brand's crm_token.
|
|
*/
|
|
protected function crmToken(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: function (string $value) {
|
|
try {
|
|
$decrypted = Crypt::decryptString($value);
|
|
} catch (DecryptException $e) {
|
|
report($e);
|
|
$decrypted = null;
|
|
}
|
|
return $decrypted;
|
|
},
|
|
set: fn (string $value) => Crypt::encryptString($value),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Interact with the brand's crm_password.
|
|
*/
|
|
protected function crmPassword(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: function (string $value) {
|
|
try {
|
|
$decrypted = Crypt::decryptString($value);
|
|
} catch (DecryptException $e) {
|
|
report($e);
|
|
$decrypted = null;
|
|
}
|
|
return $decrypted;
|
|
},
|
|
set: fn (string $value) => Crypt::encryptString($value),
|
|
);
|
|
}
|
|
|
|
// * EVENTS
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted()
|
|
{
|
|
static::creating(function (self $model) {
|
|
self::manageFields($model);
|
|
});
|
|
static::updating(function (self $model) {
|
|
self::manageFields($model);
|
|
});
|
|
static::updated(function (self $model) {
|
|
FileStorageHelper::removeOldFile($model, 'logo');
|
|
});
|
|
static::deleted(function (self $model) {
|
|
FileStorageHelper::removeFile($model, 'logo');
|
|
});
|
|
}
|
|
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Undocumented function
|
|
*
|
|
* @param Model $model
|
|
* @return void
|
|
*/
|
|
private static function manageFields(Model $model)
|
|
{
|
|
$model->logo = FileStorageHelper::storeFile($model, $model->logo, true);
|
|
}
|
|
}
|