UserProfile::getLocationAttribute()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 8
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 16
ccs 0
cts 9
cp 0
crap 20
rs 10
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Factories\HasFactory;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\SoftDeletes;
8
use Stevebauman\Location\Facades\Location;
9
10
class UserProfile extends Model
11
{
12
    use SoftDeletes;
13
    use HasFactory;
14
15
    protected $fillable = [
16
        'name',
17
        'surname',
18
        'user_id',
19
        'region_id',
20
        'work_type_id',
21
        'gender_id',
22
        'phone',
23
        'additional_information',
24
        'ip'
25
    ];
26
27
    /**
28
     * The attributes that should be cast to native types.
29
     *
30
     * @var array
31
     */
32
    protected $casts = [
33
        'validated_on' => 'datetime',
34
    ];
35
36
    /**
37
     * Return the user
38
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
39
     */
40 1
    public function user()
41
    {
42 1
        return $this->belongsTo(User::class);
43
    }
44
45
    /**
46
     * Returns the region where the user is based
47
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
48
     */
49
    public function country()
50
    {
51
        return $this->belongsTo(Country::class);
52
    }
53
54
    /**
55
     * Return the user that has validated the user
56
     *
57
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
58
     */
59
    public function validator()
60
    {
61
        return $this->belongsTo(User::class, 'validated_by', 'id');
62
    }
63
64
    /**
65
     * Get the location of the user
66
     *
67
     * @return string
68
     */
69
    public function getLocationAttribute(): string
70
    {
71
        if (empty($this->attributes['ip'])) {
72
            return "No IP saved";
73
        }
74
75
        if (strpos($this->attributes['ip'], '192.168') === 0) {
76
            return "User was created in local environment";
77
        }
78
79
        $location = Location::get($this->attributes['ip']);
80
        if ($location) {
81
            return $location->cityName . ", " . $location->regionName . ", " . $location->countryName;
82
        }
83
84
        return "No IP saved";
85
    }
86
87
    /**
88
     * Return true if the member has filled the profile with all the required data
89
     *
90
     * @return bool
91
     */
92
    public function completed(): bool
93
    {
94
        return $this->profile_completed_at != null;
95
    }
96
97
    /**
98
     * UserProfile full_name accessor.
99
     * $user->profile->full_name
100
     *
101
     * @return string
102
     */
103
    public function getFullNameAttribute(): string
104
    {
105
        return "{$this->name} {$this->surname}";
106
    }
107
}
108