Passed
Pull Request — master (#788)
by John
06:12
created

ProblemSolution::boot()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 33
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 26
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 33
rs 9.504
1
<?php
2
3
namespace App\Models\Eloquent;
4
5
use Illuminate\Database\Eloquent\Model;
6
use App\Models\Eloquent\Problem;
7
8
class ProblemSolution extends Model
9
{
10
    protected $table='problem_solution';
11
    protected $primaryKey='psoid';
12
13
    public function problem() {
14
        return $this->belongTo('App\Models\Eloquent\Problem', 'pid', 'pid');
15
    }
16
17
    public static function boot()
18
    {
19
        parent::boot();
20
        static::updating(function($model) {
21
            $problem = Problem::findOrFail($model->pid);
22
            if($model->original['audit'] != $model->audit) {
23
                if($model->audit == 1) {
24
                    // passed
25
                    sendMessage([
26
                        'sender'   => config('app.official_sender'),
27
                        'receiver' => $model->uid,
28
                        'title'    => __('message.solution.accepted.title'),
29
                        'type'     => 3,
30
                        'level'    => 5,
31
                        'data'     => [
32
                            'problem' => [[
33
                                'pcode' => $problem->pcode,
34
                                'title' => $problem->title
35
                            ]]
36
                        ]
37
                    ]);
38
                } elseif($model->audit == 2) {
39
                    // declined
40
                    sendMessage([
41
                        'sender'   => config('app.official_sender'),
42
                        'receiver' => $model->uid,
43
                        'title'    => __('message.solution.declined.title'),
44
                        'type'     => 4,
45
                        'level'    => 2,
46
                        'data'     => [
47
                            'problem' => [[
48
                                'pcode' => $problem->pcode,
49
                                'title' => $problem->title
50
                            ]]
51
                        ]
52
                    ]);
53
                }
54
            }
55
        });
56
    }
57
}
58