journal-intime/app/Console/Commands/SendDailyNotification.php

67 lines
1.7 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Exceptions\WrongHourException;
use App\Mail\DailyNotification;
use App\Services\HourService;
use App\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
class SendDailyNotification extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'send:daily';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Send daily notification to write a post';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @param HourService $hour_service
* @return mixed
*/
public function handle(HourService $hour_service)
{
try {
$notification_id = $hour_service->getCurrentNotificationId();
$this->info(sprintf('Notification id = %s', $notification_id));
} catch (WrongHourException $e) {
$this->error($e->getMessage());
exit;
}
$users = User::where('notification_hour', '=', $notification_id)
->where('email_verified_at', '!=', null)
->whereNotExists(function($query) {
$query->select()
->from('posts')
->whereRaw('posts.user_id = users.id')
->whereDate('date_post', date('Y-m-d'));
})
->get();
foreach ($users as $user) {
Mail::to($user)->queue(new DailyNotification($user));
}
}
}