|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\EmailCampaigns\Jobs; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Illuminate\Bus\Queueable; |
|
7
|
|
|
use Illuminate\Queue\SerializesModels; |
|
8
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
|
9
|
|
|
use Spatie\EmailCampaigns\Enums\SubscriptionStatus; |
|
10
|
|
|
use Spatie\EmailCampaigns\Models\EmailList; |
|
11
|
|
|
use Spatie\EmailCampaigns\Support\Config; |
|
12
|
|
|
use Spatie\EmailCampaigns\Models\Campaign; |
|
13
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
|
14
|
|
|
use Illuminate\Foundation\Bus\Dispatchable; |
|
15
|
|
|
use Spatie\EmailCampaigns\Events\CampaignSent; |
|
16
|
|
|
use Spatie\EmailCampaigns\Models\Subscription; |
|
17
|
|
|
use Spatie\EmailCampaigns\Actions\PrepareEmailHtmlAction; |
|
18
|
|
|
use Spatie\EmailCampaigns\Actions\PrepareWebviewHtmlAction; |
|
19
|
|
|
|
|
20
|
|
|
class SendCampaignJob implements ShouldQueue |
|
21
|
|
|
{ |
|
22
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
|
23
|
|
|
|
|
24
|
|
|
/** @var \Spatie\EmailCampaigns\Models\Campaign */ |
|
25
|
|
|
public $campaign; |
|
26
|
|
|
|
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
public $queue; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(Campaign $campaign) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->campaign = $campaign; |
|
33
|
|
|
|
|
34
|
|
|
$this->queue = config('email-campaigns.perform_on_queue.send_campaign_job'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function handle() |
|
38
|
|
|
{ |
|
39
|
|
|
if ($this->campaign->wasAlreadySent()) { |
|
40
|
|
|
return; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$this |
|
44
|
|
|
->prepareEmailHtml() |
|
45
|
|
|
->prepareWebviewHtml() |
|
46
|
|
|
->send(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected function prepareEmailHtml() |
|
50
|
|
|
{ |
|
51
|
|
|
$action = Config::getActionClass('prepare_email_html_action', PrepareEmailHtmlAction::class); |
|
52
|
|
|
$action->execute($this->campaign); |
|
53
|
|
|
|
|
54
|
|
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
private function prepareWebviewHtml() |
|
58
|
|
|
{ |
|
59
|
|
|
$action = Config::getActionClass('prepare_webview_html_action', PrepareWebviewHtmlAction::class); |
|
60
|
|
|
$action->execute($this->campaign); |
|
61
|
|
|
|
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function send() |
|
66
|
|
|
{ |
|
67
|
|
|
$segment = $this->campaign->getSegment(); |
|
68
|
|
|
|
|
69
|
|
|
$subscriptionsQuery = $segment |
|
70
|
|
|
->getSubscriptionsQuery($this->campaign) |
|
71
|
|
|
// ->where('status', SubscriptionStatus::SUBSCRIBED) |
|
72
|
|
|
// ->where('email_list_id', $this->campaign->emailList->id) |
|
73
|
|
|
; |
|
74
|
|
|
|
|
75
|
|
|
$subscriptionsQuery->each(function (Subscription $subscription) { |
|
76
|
|
|
if (! $this->campaign->getSegment()->shouldSend($subscription, $this->campaign)) { |
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (! $this->isValidSubscriptionForEmailList($subscription, $this->campaign->emailList)) { |
|
81
|
|
|
return; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$pendingSend = $this->campaign->sends()->create([ |
|
85
|
|
|
'email_list_subscription_id' => $subscription->id, |
|
86
|
|
|
'uuid' => (string) Str::uuid(), |
|
87
|
|
|
]); |
|
88
|
|
|
|
|
89
|
|
|
dispatch(new SendMailJob($pendingSend)); |
|
90
|
|
|
}); |
|
91
|
|
|
|
|
92
|
|
|
$this->campaign->markAsSent($this->campaign->emailList->subscriptions->count()); |
|
93
|
|
|
|
|
94
|
|
|
event(new CampaignSent($this->campaign)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function isValidSubscriptionForEmailList(Subscription $subscription, EmailList $emailList): bool |
|
98
|
|
|
{ |
|
99
|
|
|
if (! $subscription->status === SubscriptionStatus::SUBSCRIBED) { |
|
100
|
|
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if ((int)$subscription->email_list_id !== (int)$emailList->id) { |
|
104
|
|
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return true; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|