Form   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 4
dl 0
loc 164
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A boot() 0 8 1
A getSlugOptions() 0 7 1
A responses() 0 4 1
A entity() 0 4 1
A activate() 0 6 1
A deactivate() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Forms\Models;
6
7
use Spatie\Sluggable\SlugOptions;
8
use Rinvex\Support\Traits\HasSlug;
9
use Illuminate\Database\Eloquent\Model;
10
use Rinvex\Support\Traits\HasTranslations;
11
use Rinvex\Support\Traits\ValidatingTrait;
12
use Illuminate\Database\Eloquent\Relations\HasMany;
13
use Illuminate\Database\Eloquent\Relations\MorphTo;
14
15
/**
16
 * Rinvex\Forms\Models\Form.
17
 *
18
 * @property int                                                                             $id
19
 * @property int                                                                             $entity_id
20
 * @property string                                                                          $entity_type
21
 * @property string                                                                          $slug
22
 * @property string                                                                          $name
23
 * @property string                                                                          $description
24
 * @property array                                                                           $content
25
 * @property array                                                                           $actions
26
 * @property array                                                                           $submission
27
 * @property bool                                                                         $is_active
28
 * @property bool                                                                         $is_public
29
 * @property \Carbon\Carbon|null                                                             $created_at
30
 * @property \Carbon\Carbon|null                                                             $updated_at
31
 * @property \Carbon\Carbon|null                                                             $deleted_at
32
 * @property-read \Illuminate\Database\Eloquent\Model|\Eloquent                              $entity
33
 *
34
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\Form whereActions($value)
35
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\Form whereContent($value)
36
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\Form whereCreatedAt($value)
37
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\Form whereDescription($value)
38
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\Form whereEntityId($value)
39
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\Form whereEntityType($value)
40
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\Form whereIsActive($value)
41
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\Form whereIsPublic($value)
42
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\Form whereName($value)
43
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\Form whereSlug($value)
44
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\Form whereSubmission($value)
45
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Forms\Models\Form whereUpdatedAt($value)
46
 * @mixin \Eloquent
47
 */
48
class Form extends Model
49
{
50
    use HasSlug;
51
    use ValidatingTrait;
52
    use HasTranslations;
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    protected $fillable = [
58
        'entity_id',
59
        'entity_type',
60
        'slug',
61
        'name',
62
        'description',
63
        'content',
64
        'actions',
65
        'submission',
66
        'is_active',
67
        'is_public',
68
    ];
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    protected $casts = [
74
        'entity_id' => 'integer',
75
        'entity_type' => 'string',
76
        'slug' => 'string',
77
        'name' => 'string',
78
        'description' => 'string',
79
        'content' => 'json',
80
        'actions' => 'json',
81
        'submission' => 'json',
82
        'is_active' => 'boolean',
83
        'is_public' => 'boolean',
84
        'deleted_at' => 'datetime',
85
    ];
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    protected $observables = [
91
        'validating',
92
        'validated',
93
    ];
94
95
    /**
96
     * The attributes that are translatable.
97
     *
98
     * @var array
99
     */
100
    public $translatable = [
101
        'name',
102
        'description',
103
    ];
104
105
    /**
106
     * The default rules that the model will validate against.
107
     *
108
     * @var array
109
     */
110
    protected $rules = [
111
        'entity_id' => 'nullable|integer',
112
        'entity_type' => 'nullable|string|strip_tags|max:150',
113
        'slug' => 'required|alpha_dash|max:150',
114
        'name' => 'required|string|strip_tags|max:150',
115
        'description' => 'nullable|string|max:32768',
116
        'content' => 'required|array',
117
        'actions' => 'required|array',
118
        'submission' => 'required|array',
119
        'is_active' => 'sometimes|boolean',
120
        'is_public' => 'sometimes|boolean',
121
    ];
122
123
    /**
124
     * Whether the model should throw a
125
     * ValidationException if it fails validation.
126
     *
127
     * @var bool
128
     */
129
    protected $throwValidationExceptions = true;
130
131
    /**
132
     * Create a new Eloquent model instance.
133
     *
134
     * @param array $attributes
135
     */
136
    public function __construct(array $attributes = [])
137
    {
138
        parent::__construct($attributes);
139
140
        $this->setTable(config('rinvex.forms.tables.forms'));
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    protected static function boot()
147
    {
148
        parent::boot();
149
150
        static::deleted(function (self $model) {
151
            $model->responses()->delete();
152
        });
153
    }
154
155
    /**
156
     * Get the options for generating the slug.
157
     *
158
     * @return \Spatie\Sluggable\SlugOptions
159
     */
160
    public function getSlugOptions(): SlugOptions
161
    {
162
        return SlugOptions::create()
163
                          ->doNotGenerateSlugsOnUpdate()
164
                          ->generateSlugsFrom('name')
165
                          ->saveSlugsTo('slug');
166
    }
167
168
    /**
169
     * The form may have many responses.
170
     *
171
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
172
     */
173
    public function responses(): HasMany
174
    {
175
        return $this->hasMany(config('rinvex.forms.models.form_response'), 'form_id', 'id');
176
    }
177
178
    /**
179
     * Get the owner model of the form.
180
     *
181
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
182
     */
183
    public function entity(): MorphTo
184
    {
185
        return $this->morphTo('entity', 'entity_type', 'entity_id', 'id');
186
    }
187
188
    /**
189
     * Activate the form.
190
     *
191
     * @return $this
192
     */
193
    public function activate()
194
    {
195
        $this->update(['is_active' => true]);
196
197
        return $this;
198
    }
199
200
    /**
201
     * Deactivate the form.
202
     *
203
     * @return $this
204
     */
205
    public function deactivate()
206
    {
207
        $this->update(['is_active' => false]);
208
209
        return $this;
210
    }
211
}
212