Subscription::confirm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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