Ajoute la notification journalière

Il manque l'intégration du mail
Pour #13
This commit is contained in:
Clement Desmidt 2020-03-20 16:37:36 +01:00
parent 3ab7e495e9
commit bd12cf6dfb
7 changed files with 208 additions and 10 deletions

View File

@ -0,0 +1,66 @@
<?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));
}
}
}

View File

@ -2,12 +2,11 @@
namespace App\Console;
use App\Console\Commands\SendDailyNotification;
use App\Jobs\SendReminderEmail;
use App\Jobs\SendWelcomeEmail;
use App\User;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Carbon;
class Kernel extends ConsoleKernel
{
@ -17,7 +16,7 @@ class Kernel extends ConsoleKernel
* @var array
*/
protected $commands = [
//
SendDailyNotification::class,
];
/**
@ -28,17 +27,17 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
$schedule
->command('send:daily')
->cron('0 */6 * * *');
$schedule->call(function() {
$users = User::where('email_verified_at', '!=', null)
->where('notification_hour', '!=', null)
$users = User::where('email_verified_at', '=', null)
->get();
var_dump(count($users));
foreach ($users as $user) {
dispatch((new SendReminderEmail($user)));
}
})->everyMinute();
// $schedule->command('inspire')
// ->hourly();
})->daily();
}
/**

View File

@ -0,0 +1,10 @@
<?php
namespace App\Exceptions;
class WrongHourException extends \Exception
{
}

View File

@ -4,6 +4,7 @@ namespace App\Jobs;
use App\Mail\ReminderMail;
use App\Mail\WelcomeMail;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
@ -15,8 +16,9 @@ class SendReminderEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/** @var User */
private $user;
/**
* Create a new job instance.
*

View File

@ -0,0 +1,38 @@
<?php
namespace App\Mail;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class DailyNotification extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
/**
* @var User
*/
public $user;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('emails.daily');
}
}

View File

@ -0,0 +1,82 @@
<?php
namespace App\Services;
use App\Exceptions\WrongHourException;
use App\User;
class HourService
{
const NO_NOTIFICATION = 0;
const MIDNIGHT_NOTIFICATION = 1;
const MORNING_NOTIFICATION = 2;
const NOON_NOTIFICATION = 3;
const EVENING_NOTIFICATION = 4;
public static $notifications_classes = [
self::NO_NOTIFICATION => 'slash',
self::MIDNIGHT_NOTIFICATION => 'moon',
self::MORNING_NOTIFICATION => 'sunrise',
self::NOON_NOTIFICATION => 'sun',
self::EVENING_NOTIFICATION => 'sunset',
];
/**
* @param User $user
* @return array
*/
public function getHoursBracket(User $user)
{
switch ((int)$user->notification_hour) {
case self::MIDNIGHT_NOTIFICATION:
return ['2100-2359', '0000-0600'];
break;
case self::MORNING_NOTIFICATION:
return ['0601-1100'];
break;
case self::NOON_NOTIFICATION:
return ['1101-1530'];
break;
case self::EVENING_NOTIFICATION:
return ['1531-2059'];
break;
case self::NO_NOTIFICATION:
default:
return [];
}
}
/**
* @param null|int|string $current_hour the hour and minute we want to find the id to. Formatted like `date('Hi')`
* @return int
* @throws WrongHourException
*/
public function getCurrentNotificationId($current_hour = null)
{
if (null === $current_hour) {
$current_hour = date('Hi');
}
$current_hour = (int) $current_hour;
if (
($current_hour >= 2100 && $current_hour < 2359) ||
($current_hour >= 0 && $current_hour < 600)
) {
return self::MIDNIGHT_NOTIFICATION;
}
if ($current_hour >= 600 && $current_hour < 1100) {
return self::MORNING_NOTIFICATION;
}
if ($current_hour >= 1100 && $current_hour < 1530) {
return self::NOON_NOTIFICATION;
}
if ($current_hour >= 1530 && $current_hour < 2100) {
return self::EVENING_NOTIFICATION;
}
throw new WrongHourException(sprintf('Given Time « %s » is not compatible', $current_hour));
}
}

View File

@ -0,0 +1 @@
Merci d'écrire !