Просмотр файла app/Console/Commands/AddSubscribers.php

Размер файла: 2.35Kb
  1. <?php
  2.  
  3. namespace App\Console\Commands;
  4.  
  5. use App\Models\Mailing;
  6. use App\Models\User;
  7. use Illuminate\Console\Command;
  8.  
  9. class AddSubscribers extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'add:subscribers';
  17.  
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Add subscribers';
  24.  
  25. /**
  26. * Create a new command instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. }
  34.  
  35. /**
  36. * Добавляет подписчиков в рассылку
  37. *
  38. * @return int
  39. */
  40. public function handle()
  41. {
  42. $deliveryUsers = User::query()
  43. ->where('sendprivatmail', 0)
  44. ->whereIn('level', User::USER_GROUPS)
  45. ->where('newprivat', '>', 0)
  46. ->where('updated_at', '<', strtotime('-' . setting('sendprivatmailday') . ' days', SITETIME))
  47. ->whereNotNull('subscribe')
  48. ->limit(100)
  49. ->get();
  50.  
  51. if ($deliveryUsers->isNotEmpty()) {
  52. foreach ($deliveryUsers as $user) {
  53. $subject = $user->newprivat . ' непрочитанных сообщений на ' . setting('title');
  54.  
  55. $text = 'Здравствуйте ' . e($user->getName()) . '!<br>У вас имеются непрочитанные сообщения (' . $user->newprivat . ' шт.) на сайте ' . setting('title') . '<br>Прочитать свои сообщения вы можете по адресу <a href="' . config('app.url') . '/messages">' . config('app.url') . '/messages</a><br><br><small>Если вы не хотите получать эти email, пожалуйста, <a href="' . config('app.url') . '/unsubscribe?key=' . $user->subscribe . '">откажитесь от подписки</a></small>';
  56.  
  57. Mailing::query()->create([
  58. 'user_id' => $user->id,
  59. 'type' => 'messages',
  60. 'subject' => $subject,
  61. 'text' => $text,
  62. 'created_at' => SITETIME,
  63. ]);
  64.  
  65. $user->update(['sendprivatmail' => 1]);
  66. }
  67. }
  68.  
  69. $this->info('Subscribers successfully added.');
  70.  
  71. return 0;
  72. }
  73. }