ldc-mesbr-app/app/Models/Brand.php
2023-12-01 15:27:30 +01:00

99 lines
2.3 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use App\Lib\Helpers\FileStorageHelper;
/**
* Brand
*
* @property integer $id Brand Id.
* @property string $name Brand name.
* @property string $contact_mail The contact mail of the brand.
* @property string $rcs The RCS of the brand.
* @property string $logo The path of the brand logo.
* @property string $crm_token CRM encrypted token.
* @property string $crm_password CRM encrypted password.
*
* * RELATIONS
* @property-read \App\Models\Campaign $campaigns Campaign that owned by the Brand.
*/
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'
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'crm_token' => 'encrypted',
'crm_password' => 'encrypted',
];
// * RELATIONS
/**
* Get campaign that owned by the Brand.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function campaigns(): HasMany
{
return $this->hasMany(Campaign::class);
}
// * 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
/**
* Helper to store the logo of the model.
*
* @param Model $model
* @return void
*/
private static function manageFields(Model $model)
{
$model->logo = FileStorageHelper::storeFile($model, $model->logo, true);
}
}