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
|
|
|
|