Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
80.00% |
20 / 25 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| SingleOptin | |
80.00% |
20 / 25 |
|
50.00% |
1 / 2 |
4.13 | |
0.00% |
0 / 1 |
| __construct | |
79.17% |
19 / 24 |
|
0.00% |
0 / 1 |
3.08 | |||
| brand | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Models; |
| 4 | |
| 5 | use Illuminate\Database\Eloquent\Factories\HasFactory; |
| 6 | use Illuminate\Database\Eloquent\Model; |
| 7 | use Illuminate\Support\Str; |
| 8 | use Kwaadpepper\CrudPolicies\Enums\CrudAction; |
| 9 | use Kwaadpepper\CrudPolicies\Enums\CrudType; |
| 10 | use Kwaadpepper\CrudPolicies\Traits\IsCrudModel; |
| 11 | |
| 12 | class SingleOptin extends Model |
| 13 | { |
| 14 | use HasFactory; |
| 15 | use IsCrudModel; |
| 16 | |
| 17 | /** @var string */ |
| 18 | protected $crudLabelColumn = 'email'; |
| 19 | |
| 20 | /** @var string */ |
| 21 | protected $crudValueColumn = 'id'; |
| 22 | |
| 23 | /** |
| 24 | * The attributes that are mass assignable. |
| 25 | * |
| 26 | * @var string[] |
| 27 | */ |
| 28 | protected $fillable = [ |
| 29 | 'email' |
| 30 | ]; |
| 31 | |
| 32 | /** |
| 33 | * SingleOptin |
| 34 | * |
| 35 | * @param array $attributes |
| 36 | */ |
| 37 | public function __construct(array $attributes = []) |
| 38 | { |
| 39 | self::$editableProperties = [ |
| 40 | 'email' => [ |
| 41 | 'label' => Str::ucfirst(trans('models.common.email')), |
| 42 | 'type' => CrudType::email(), |
| 43 | 'validate' => [ |
| 44 | 'required', |
| 45 | 'email:rfc,dns', |
| 46 | // @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed |
| 47 | function ($attribute, $value, $fail) { |
| 48 | $q = static::query(); |
| 49 | if (request()->user) { |
| 50 | $q = $q->where('id', '!=', $this->id); |
| 51 | } |
| 52 | if ($q->where('email', $value)->exists()) { |
| 53 | $fail(__(':attribute est déjà utilisé', [ |
| 54 | 'attribute' => $attribute |
| 55 | ])); |
| 56 | } |
| 57 | } |
| 58 | ], |
| 59 | 'actions' => [ |
| 60 | CrudAction::viewAny(), |
| 61 | CrudAction::view(), |
| 62 | CrudAction::create(), |
| 63 | CrudAction::update() |
| 64 | ] |
| 65 | ], |
| 66 | 'brand_id' => [ |
| 67 | 'label' => Str::ucfirst(trans('models.classes.brand')), |
| 68 | 'type' => CrudType::belongsTo(), |
| 69 | 'belongsTo' => Brand::class, |
| 70 | 'actions' => [ |
| 71 | CrudAction::viewAny(), |
| 72 | CrudAction::view() |
| 73 | ] |
| 74 | ], |
| 75 | 'created_at' => [ |
| 76 | 'label' => Str::ucfirst(trans('models.common.optinDate')), |
| 77 | 'type' => CrudType::string(), |
| 78 | 'readonly' => true, |
| 79 | 'actions' => [ |
| 80 | CrudAction::viewAny(), |
| 81 | CrudAction::view(), |
| 82 | CrudAction::update() |
| 83 | ] |
| 84 | ], |
| 85 | ]; |
| 86 | parent::__construct($attributes); |
| 87 | } |
| 88 | |
| 89 | // * RELATIONS |
| 90 | |
| 91 | /** |
| 92 | * Brand BelongsTo relation |
| 93 | * |
| 94 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
| 95 | */ |
| 96 | public function brand() |
| 97 | { |
| 98 | return $this->belongsTo(Brand::class); |
| 99 | } |
| 100 | } |