Subscription   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A user() 0 3 1
A mailingList() 0 3 1
A getCampaigns() 0 3 1
A scopeMailingList() 0 3 1
A boot() 0 6 1
1
<?php
2
3
namespace App\Models;
4
5
use EloquentFilter\Filterable;
6
use Illuminate\Database\Eloquent\Model;
7
8
/**
9
 * @property mixed campaigns
10
 * @property mixed email
11
 * @property mixed name
12
 * @property mixed mailingList
13
 * @property mixed country
14
 * @property mixed unsubscribe
15
 * @property mixed user_id
16
 */
17
class Subscription extends Model
18
{
19
    use Filterable;
20
21
    /**
22
     * @var array
23
     */
24
    protected $fillable = [
25
        'email',
26
        'name',
27
        'country',
28
        'language',
29
        'mailing_list_id',
30
        'user_id',
31
    ];
32
33
    /**
34
     * @param $query
35
     * @param $type
36
     * @return mixed
37
     */
38
    public function scopeMailingList($query, $type)
39
    {
40
        return $query->where('mailing_list_id', $type);
41
    }
42
43
    /**
44
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
45
     */
46
    public function mailingList()
47
    {
48
        return $this->belongsTo(MailingList::class);
49
    }
50
51
    /**
52
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
53
     */
54
    public function user()
55
    {
56
        return $this->hasOne(User::class);
57
    }
58
59
    /**
60
     * @return mixed
61
     */
62
    public function getCampaigns()
63
    {
64
        return $this->mailingList->pluck('campaigns')->flatten();
65
    }
66
67
    public static function boot()
68
    {
69
        parent::boot();
70
71
        static::creating(function ($subscription) {
72
            $subscription->unsubscribe = str_random(25);
73
        });
74
    }
75
}
76