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

Subscriber   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 5
dl 0
loc 39
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A findForEmail() 0 4 1
A emailLists() 0 4 1
A subscribeTo() 0 6 1
A subscribeNowTo() 0 4 1
A isSubscribedTo() 0 8 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