1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\EmailCampaigns\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
7
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
8
|
|
|
use Spatie\EmailCampaigns\Support\Config; |
9
|
|
|
use Spatie\EmailCampaigns\Actions\SubscribeAction; |
10
|
|
|
use Spatie\EmailCampaigns\Models\Concerns\HasUuid; |
11
|
|
|
use Spatie\EmailCampaigns\Enums\SubscriptionStatus; |
12
|
|
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough; |
13
|
|
|
use Spatie\EmailCampaigns\Models\Concerns\HasExtraAttributes; |
14
|
|
|
|
15
|
|
|
class Subscriber extends Model |
16
|
|
|
{ |
17
|
|
|
use HasUuid, |
18
|
|
|
HasExtraAttributes; |
19
|
|
|
|
20
|
|
|
public $table = 'email_list_subscribers'; |
21
|
|
|
|
22
|
|
|
public $casts = [ |
23
|
|
|
'extra_attributes' => 'array', |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
protected $guarded = []; |
27
|
|
|
|
28
|
|
|
public static function findForEmail(string $email): ?Subscriber |
29
|
|
|
{ |
30
|
|
|
return static::where('email', $email)->first(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function emailLists(): BelongsToMany |
34
|
|
|
{ |
35
|
|
|
return $this |
36
|
|
|
->belongsToMany(EmailList::class, 'email_list_subscriptions','email_list_subscriber_id', 'email_list_id') |
37
|
|
|
->where('email_list_subscriptions.status', SubscriptionStatus::SUBSCRIBED); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function subscribeTo(EmailList $emailList, bool $respectDoubleOptIn = true): Subscription |
41
|
|
|
{ |
42
|
|
|
$action = Config::getActionClass('subscribe_action', SubscribeAction::class); |
43
|
|
|
|
44
|
|
|
return $action->execute($this, $emailList, $respectDoubleOptIn); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function subscribeNowTo(EmailList $emailList) |
48
|
|
|
{ |
49
|
|
|
return $this->subscribeTo($emailList, false); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function unsubscribeFrom(EmailList $emailList): void |
53
|
|
|
{ |
54
|
|
|
$emailList->unsubscribe($this->email); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function isSubscribedTo(EmailList $emailList): bool |
58
|
|
|
{ |
59
|
|
|
return Subscription::query() |
|
|
|
|
60
|
|
|
->where('email_list_subscriber_id', $this->id) |
61
|
|
|
->where('email_list_id', $emailList->id) |
62
|
|
|
->where('status', SubscriptionStatus::SUBSCRIBED) |
63
|
|
|
->exists(); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.