Completed
Pull Request — master (#33)
by Karl
02:24
created

Search   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 50%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
lcom 0
cbo 3
dl 0
loc 102
rs 10
c 1
b 0
f 0
ccs 9
cts 18
cp 0.5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A user() 0 4 1
A scopeActive() 0 6 1
A boot() 0 8 1
A latestNotification() 0 5 1
A notifications() 0 4 1
A scopeWhereUserEmail() 0 6 1
A scopeWhereUserId() 0 6 1
1
<?php namespace JobApis\JobsToMail\Models;
2
3
use Illuminate\Database\Eloquent\Model;
4
use Illuminate\Database\Eloquent\SoftDeletes;
5
use Ramsey\Uuid\Uuid;
6
7
class Search extends Model
8
{
9
    use SoftDeletes;
10
11
    /**
12
     * Indicates that the IDs are not auto-incrementing.
13
     *
14
     * @var bool
15
     */
16
    public $incrementing = false;
17
18
    /**
19
     * The attributes that are mass assignable.
20
     *
21
     * @var array
22
     */
23
    protected $fillable = [
24
        'keyword',
25
        'location',
26
        'no_recruiters',
27
        'user_id',
28
    ];
29
30
    /**
31
     * Boot function from laravel.
32
     */
33 6
    protected static function boot()
34
    {
35 6
        parent::boot();
36
37
        static::creating(function ($model) {
38
            $model->{$model->getKeyName()} = Uuid::uuid4();
39 6
        });
40 6
    }
41
42
    /**
43
     * Defines the relationship to User model
44
     *
45
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
46
     */
47 1
    public function user()
48
    {
49 1
        return $this->belongsTo(User::class);
50
    }
51
52
    /**
53
     * Gets the latest Notification only
54
     *
55
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
56
     */
57
    public function latestNotification()
58
    {
59
        return $this->hasOne(CustomDatabaseNotification::class)
60
            ->orderBy('created_at', 'desc');
61
    }
62
63
    /**
64
     * Defines the relationship to Notification model
65
     *
66
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
67
     */
68
    public function notifications()
69
    {
70
        return $this->hasMany(CustomDatabaseNotification::class);
71
    }
72
73
    /**
74
     * Limits query to "active" searches
75
     *
76
     * @return \Illuminate\Database\Eloquent\Builder
77
     */
78 1
    public function scopeActive($query)
79
    {
80
        return $query->whereHas('user', function ($query) {
81 1
            return $query->confirmed();
82 1
        });
83
    }
84
85
    /**
86
     * Limits query to searches by user with specific email
87
     *
88
     * @return \Illuminate\Database\Eloquent\Builder
89
     */
90
    public function scopeWhereUserEmail($query, $email = null)
91
    {
92
        return $query->whereHas('user', function ($query) use ($email) {
93
            return $query->where('email', $email);
94
        });
95
    }
96
97
    /**
98
     * Limits query to searches by user with specific id
99
     *
100
     * @return \Illuminate\Database\Eloquent\Builder
101
     */
102
    public function scopeWhereUserId($query, $id = null)
103
    {
104
        return $query->whereHas('user', function ($query) use ($id) {
105
            return $query->where('id', $id);
106
        });
107
    }
108
}
109