Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (danger)
50.00%
1 / 2
CRAP
79.31% covered (warning)
79.31%
23 / 29
SingleOptin
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (danger)
50.00%
1 / 2
4.14
79.31% covered (warning)
79.31%
23 / 29
 __construct
0.00% covered (danger)
0.00%
0 / 1
3.09
78.57% covered (warning)
78.57%
22 / 28
 brand
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
1<?php
2
3namespace App\Models;
4
5use Illuminate\Database\Eloquent\Factories\HasFactory;
6use Illuminate\Database\Eloquent\Model;
7use Illuminate\Support\Str;
8use Kwaadpepper\CrudPolicies\Enums\CrudAction;
9use Kwaadpepper\CrudPolicies\Enums\CrudType;
10use Kwaadpepper\CrudPolicies\Traits\IsCrudModel;
11
12class 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}