Status::afterSave()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php namespace Bedard\Shop\Models;
2
3
use Flash;
4
use Lang;
5
use Model;
6
use October\Rain\Database\ModelException;
7
8
/**
9
 * Status Model.
10
 */
11
class Status extends Model
12
{
13
    use \October\Rain\Database\Traits\SoftDelete,
14
        \October\Rain\Database\Traits\Validation;
15
16
    /**
17
     * @var string The database table used by the model.
18
     */
19
    public $table = 'bedard_shop_statuses';
20
21
    /**
22
     * @var array Attribute casting
23
     */
24
    protected $casts = [
25
        'is_abandoned' => 'boolean',
26
        'is_default' => 'boolean',
27
        'is_reducing' => 'boolean',
28
    ];
29
30
    /**
31
     * @var array Dates
32
     */
33
    protected $dates = [
34
        'deleted_at',
35
    ];
36
37
    /**
38
     * @var array Guarded fields
39
     */
40
    protected $guarded = ['*'];
41
42
    /**
43
     * @var array Fillable fields
44
     */
45
    protected $fillable = [
46
        'color',
47
        'icon',
48
        'is_abandoned',
49
        'is_default',
50
        'is_reducing',
51
        'name',
52
    ];
53
54
    /**
55
     * @var array Relations
56
     */
57
    public $belongsToMany = [
58
        'carts' => [
59
            'Bedard\Shop\Models\Cart',
60
            'pivot' => [
61
                'created_at',
62
                'driver',
63
            ],
64
            'table' => 'bedard_shop_cart_status',
65
        ],
66
    ];
67
68
    /**
69
     * @var array Validation
70
     */
71
    public $rules = [
72
        'name' => 'required',
73
    ];
74
75
    /**
76
     * After save.
77
     *
78
     * @return void
79
     */
80
    public function afterSave()
81
    {
82
        $this->preventDuplicateAbandoned();
83
        $this->preventDuplicateDefaults();
84
    }
85
86
    /**
87
     * Before delete.
88
     *
89
     * @return void
90
     */
91
    public function beforeDelete()
92
    {
93
        if (! $this->isDeleteable()) {
94
            return false;
95
        }
96
    }
97
98
    /**
99
     * Before save.
100
     *
101
     * @return void
102
     */
103
    public function beforeSave()
104
    {
105
        $this->preventDefaultRemoval();
106
        $this->validateFlags();
107
    }
108
109
    /**
110
     * Determine if the status can be deleted.
111
     *
112
     * @return bool
113
     */
114
    public function isDeleteable()
115
    {
116
        return ! $this->is_default && ! $this->is_abandoned;
117
    }
118
119
    /**
120
     * Prevent the default status from being removed.
121
     *
122
     * @throws \October\Rain\Database\ModelException
123
     * @return void
124
     */
125
    protected function preventDefaultRemoval()
126
    {
127
        if ($this->getOriginal('is_default') && ! $this->is_default) {
128
            $message = Lang::get('bedard.shop::lang.statuses.form.is_default_removed');
129
130
            Flash::error($message);
131
            throw new ModelException($this, $message);
132
        }
133
    }
134
135
    /**
136
     * Prevent multiple statuses from having an abandoned status.
137
     *
138
     * @return void
139
     */
140
    protected function preventDuplicateAbandoned()
141
    {
142
        if ($this->is_abandoned) {
143
            self::isAbandoned()
144
                ->where('id', '<>', $this->id)
145
                ->update(['is_abandoned' => false]);
146
        }
147
    }
148
149
    /**
150
     * Prevent multiple statuses from having a default status.
151
     *
152
     * @return void
153
     */
154
    protected function preventDuplicateDefaults()
155
    {
156
        if ($this->is_default) {
157
            self::isDefault()
158
                ->where('id', '<>', $this->id)
159
                ->update(['is_default' => false]);
160
        }
161
    }
162
163
    /**
164
     * Select the abandoned status.
165
     *
166
     * @param  \October\Rain\Database\Builder $query
167
     * @return \October\Rain\Database\Builder
168
     */
169
    public function scopeIsAbandoned($query)
170
    {
171
        return $query->where('is_abandoned', 1);
172
    }
173
174
    /**
175
     * Select the default status.
176
     *
177
     * @param  \October\Rain\Database\Builder $query
178
     * @return \October\Rain\Database\Builder
179
     */
180
    public function scopeIsDefault($query)
181
    {
182
        return $query->where('is_default', 1);
183
    }
184
185
    /**
186
     * Validate status flags.
187
     *
188
     * @throws \Exception
189
     * @return void
190
     */
191
    protected function validateFlags()
192
    {
193
        if ($this->is_abandoned && $this->is_default) {
194
            $message = Lang::get('bedard.shop::lang.statuses.form.is_default_abandoned_exception');
195
196
            Flash::error($message);
197
            throw new ModelException($this, $message);
198
        }
199
    }
200
}
201