parent
3ab7e495e9
commit
bd12cf6dfb
7 changed files with 208 additions and 10 deletions
@ -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)); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,10 @@ |
||||
<?php |
||||
|
||||
|
||||
namespace App\Exceptions; |
||||
|
||||
|
||||
class WrongHourException extends \Exception |
||||
{ |
||||
|
||||
} |
@ -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'); |
||||
} |
||||
} |
@ -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)); |
||||
} |
||||
} |
@ -0,0 +1 @@ |
||||
Merci d'écrire ! |
Loading…
Reference in new issue