Subscription   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 4
dl 0
loc 46
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A emailList() 0 4 1
A subscriber() 0 4 1
A confirm() 0 6 1
A markAsUnsubscribed() 0 15 2
A unsubscribeUrl() 0 4 1
1
<?php
2
3
namespace Spatie\EmailCampaigns\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Spatie\EmailCampaigns\Support\Config;
7
use Spatie\EmailCampaigns\Events\Unsubscribed;
8
use Spatie\EmailCampaigns\Models\Concerns\HasUuid;
9
use Spatie\EmailCampaigns\Enums\SubscriptionStatus;
10
use Illuminate\Database\Eloquent\Relations\BelongsTo;
11
use Spatie\EmailCampaigns\Actions\ConfirmSubscriptionAction;
12
use Spatie\EmailCampaigns\Http\Controllers\UnsubscribeController;
13
14
class Subscription extends Model
15
{
16
    public $table = 'email_list_subscriptions';
17
18
    use HasUuid;
19
20
    public $guarded = [];
21
22
    public function emailList(): BelongsTo
23
    {
24
        return $this->belongsTo(EmailList::class);
25
    }
26
27
    public function subscriber(): BelongsTo
28
    {
29
        return $this->belongsTo(Subscriber::class, 'email_list_subscriber_id');
30
    }
31
32
    public function confirm()
33
    {
34
        $action = Config::getActionClass('confirm_subscription_action', ConfirmSubscriptionAction::class);
35
36
        return $action->execute($this);
37
    }
38
39
    public function markAsUnsubscribed(CampaignSend $campaignSend = null)
40
    {
41
        $this->update(['status' => SubscriptionStatus::UNSUBSCRIBED]);
42
43
        if ($campaignSend) {
44
            CampaignUnsubscribe::firstOrCreate([
45
                'email_campaign_id' => $campaignSend->campaign->id,
46
                'email_list_subscriber_id' => $campaignSend->subscription->subscriber->id,
47
            ]);
48
        }
49
50
        event(new Unsubscribed($this, $campaignSend));
51
52
        return $this;
53
    }
54
55
    public function unsubscribeUrl(CampaignSend $campaignSend = null): string
56
    {
57
        return url(action(UnsubscribeController::class, [$this->uuid, optional($campaignSend)->uuid]));
58
    }
59
}
60