Completed
Push — master ( f2cb96...f968aa )
by Freek
01:28
created

Subscriber::subscribeNowTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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\Actions\SubscribeAction;
8
use Spatie\EmailCampaigns\Models\Concerns\HasUuid;
9
use Spatie\EmailCampaigns\Enums\SubscriptionStatus;
10
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
11
12
class Subscriber extends Model
13
{
14
    use HasUuid;
15
16
    public $table = 'email_list_subscribers';
17
18
    protected $guarded = [];
19
20
    public static function findForEmail(string $email): ?Subscriber
21
    {
22
        return static::where('email', $email)->first();
23
    }
24
25
    public function emailLists(): HasManyThrough
26
    {
27
        return $this->hasManyThrough(EmailList::class, Subscription::class);
28
    }
29
30
    public function subscribeTo(EmailList $emailList, bool $respectDoubleOptIn = true): Subscription
31
    {
32
        $action = Config::getActionClass('subscribe_action', SubscribeAction::class);
33
34
        return $action->execute($this, $emailList, $respectDoubleOptIn);
35
    }
36
37
    public function subscribeNowTo(EmailList $emailList)
38
    {
39
        return $this->subscribeTo($emailList, false);
40
    }
41
42
    public function isSubscribedTo(EmailList $emailList): bool
43
    {
44
        return Subscription::query()
0 ignored issues
show
Bug introduced by
The method exists() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean canUseExistsForExistenceCheck()?

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.

Loading history...
45
            ->where('email_list_subscriber_id', $this->id)
46
            ->where('email_list_id', $emailList->id)
47
            ->where('status', SubscriptionStatus::SUBSCRIBED)
48
            ->exists();
49
    }
50
}
51