User_body_mass   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
c 0
b 0
f 0
dl 0
loc 73
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B createOrUpdate() 0 29 6
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class User_body_mass extends Base_Model  {
8
9
    /**
10
     * The database table used by the model.
11
     *
12
     * @var string
13
     */
14
15
16
    Public const CREATED_AT = 'created';
17
    Public const UPDATED_AT = 'modified';
18
    protected $table = 'user_body_masses';
19
20
    public $timestamps = false;
21
22
    /**
23
     * Attributes that should be mass-assignable.
24
     *
25
     * @var array
26
     */
27
    protected $fillable = ['date', 'height', 'weight', 'body_mass_index', 'comment', 'academic_period_id', 'security_user_id', 'modified_user_id', 'modified', 'created_user_id', 'created'];
28
29
    /**
30
     * The attributes excluded from the model's JSON form.
31
     *
32
     * @var array
33
     */
34
    protected $hidden = [];
35
36
    /**
37
     * The attributes that should be casted to native types.
38
     *
39
     * @var array
40
     */
41
    protected $casts = [];
42
43
    /**
44
     * The attributes that should be mutated to dates.
45
     *
46
     * @var array
47
     */
48
    protected $dates = ['date', 'modified', 'created'];
49
50
51
    public static function createOrUpdate($studentId,$row,$file){
52
        if (!empty($row['bmi_weight']) && !empty($row['bmi_weight']) && !empty($row['bmi_date_yyyy_mm_dd'])) {
53
            try {
54
                // convert Meeter to CM
55
                $hight = $row['bmi_height'] / 100;
56
                //calculate BMI
57
                $bodyMass = ($row['bmi_weight']) / pow($hight, 2);
58
                $bmiAcademic = Academic_period::where('name', '=', $row['bmi_academic_period'])->first();
59
                
60
                $count = User_body_mass::where('academic_period_id' ,'=',$bmiAcademic->id)
61
                            ->where('security_user_id',$studentId)->count();
62
                $data = [
63
                    'height' => $row['bmi_height'],
64
                    'weight' => $row['bmi_weight'],
65
                    'date' => $row['bmi_date_yyyy_mm_dd'],
66
                    'body_mass_index' => $bodyMass,
67
                    'academic_period_id' => $bmiAcademic->id,
68
                    'security_user_id' => $studentId,
69
                    'created_user_id' => $file['security_user_id']
70
                ];
71
                // dd($data);
72
                if($count == 0){
73
                    self::create($data);
74
                }else{
75
                    self::where('security_user_id',$studentId)
76
                    ->update($data);
77
                }         
78
            } catch (\Throwable $th) {
79
                \Log::error('User_body_mass:' . $th->getMessage());
80
            }
81
        }
82
    }
83
}