Completed
Push — master ( 6ffab0...c9db29 )
by Freek
01:20
created

CampaignSend::markAsBounced()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.9332
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Spatie\EmailCampaigns\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Relations\HasMany;
7
use Illuminate\Database\Eloquent\Relations\HasOne;
8
use Spatie\EmailCampaigns\Enums\CampaignSendBounceSeverity;
9
use Spatie\EmailCampaigns\Models\Concerns\HasUuid;
10
use Illuminate\Database\Eloquent\Relations\BelongsTo;
11
12
class CampaignSend extends Model
13
{
14
    use HasUuid;
15
16
    public $table = 'email_campaign_sends';
17
18
    public $guarded = [];
19
20
    public $dates = ['sent_at'];
21
22
    public static function findByTransportMessageId(string $transportMessageId): ?Model
23
    {
24
        return static::where('transport_message_id', $transportMessageId)->first();
25
    }
26
27
    public function subscription(): BelongsTo
28
    {
29
        return $this->belongsTo(Subscription::class, 'email_list_subscription_id');
30
    }
31
32
    public function campaign(): BelongsTo
33
    {
34
        return $this->belongsTo(Campaign::class, 'email_campaign_id');
35
    }
36
37
    public function bounces(): HasMany
38
    {
39
        return $this->hasMany(CampaignSendBounce::class);
40
    }
41
42
    public function markAsBounced(string $severity)
43
    {
44
        $this->bounces()->create(compact('severity'));
45
46
        if ($severity === CampaignSendBounceSeverity::PERMANENT) {
47
            $this->subscription->markAsUnsubscribed();
48
        }
49
50
        return $this;
51
    }
52
53
    public function markAsSent()
54
    {
55
        $this->sent_at = now();
56
57
        $this->save();
58
59
        return $this;
60
    }
61
62
    public function wasAlreadySent(): bool
63
    {
64
        return !is_null($this->sent_at);
65
    }
66
}
67