1 | <?php |
||
2 | |||
3 | namespace App\Jobs; |
||
4 | |||
5 | use Mail; |
||
6 | use App\Models\User; |
||
7 | use App\Models\Campaign; |
||
8 | use App\Models\Template; |
||
9 | use App\Mail\CampaignMail; |
||
10 | use Illuminate\Bus\Queueable; |
||
11 | use App\Mail\CampaignSendMail; |
||
12 | use Illuminate\Queue\SerializesModels; |
||
13 | use Illuminate\Queue\InteractsWithQueue; |
||
14 | use Illuminate\Contracts\Queue\ShouldQueue; |
||
15 | use Illuminate\Database\Eloquent\Collection; |
||
16 | |||
17 | /** |
||
18 | * @property Campaign campaign |
||
19 | * @property Template template |
||
20 | * @property Collection subscriptions |
||
21 | * @property User user |
||
22 | */ |
||
23 | class SendCampaign implements ShouldQueue |
||
24 | { |
||
25 | use InteractsWithQueue, Queueable, SerializesModels; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
26 | |||
27 | protected $campaign; |
||
28 | protected $user; |
||
29 | protected $template; |
||
30 | |||
31 | /** |
||
32 | * SendCampaign constructor. |
||
33 | * |
||
34 | * @param User $user |
||
35 | * @param Campaign $campaign |
||
36 | * @param Template $template |
||
37 | */ |
||
38 | public function __construct(User $user, Campaign $campaign, Template $template) |
||
39 | { |
||
40 | $this->user = $user; |
||
41 | $this->campaign = $campaign; |
||
42 | $this->template = $template; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Execute the job. |
||
47 | * |
||
48 | * @return void |
||
49 | */ |
||
50 | public function handle() |
||
51 | { |
||
52 | $lists = $this->campaign |
||
53 | ->mailingLists() |
||
54 | ->with('subscriptions') |
||
55 | ->get(); |
||
56 | |||
57 | $chunk = ceil($lists->count() / 4); |
||
58 | |||
59 | $lists->each(function ($list) use ($chunk) { |
||
60 | $list->subscriptions()->chunk($chunk, function ($subscriptions) { |
||
61 | $subscriptions->each(function ($subscription) { |
||
62 | Mail::to($subscription)->queue(new CampaignMail($subscription, $this->campaign, $this->template)); |
||
63 | }); |
||
64 | }); |
||
65 | }); |
||
66 | |||
67 | $this->campaign->update([ |
||
68 | 'send' => 1 |
||
69 | ]); |
||
70 | |||
71 | if ($this->user->preferences['notifications']) { |
||
72 | Mail::to($this->user)->queue(new CampaignSendMail($this->campaign->getSubscriptions(), $this->campaign)); |
||
73 | } |
||
74 | } |
||
75 | } |
||
76 |