%PDF- %PDF-
Direktori : /var/www/html/diaspora/api_internal/app/Jobs/ |
Current File : /var/www/html/diaspora/api_internal/app/Jobs/NotifyUsersForNewPostCreatedJob.php |
<?php namespace App\Jobs; use App\Models\Device; use App\Models\Post; use App\Models\PushQueue; use App\Notifications\NotifyUsersForNewPostCreatedNotification; use Carbon\Carbon; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; class NotifyUsersForNewPostCreatedJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; /** * @var Post */ private $post; /** * Create a new job instance. * * @param Post $post */ public function __construct(Post $post) { $this->post = $post; } /** * Execute the job. * * @return void * @throws \Exception */ public function handle() { $devices = Device::query() ->whereHas('user') ->whereHas('configurations', function ($q) { $q->where('country_id', $this->post->country_id); }) ->orWhereHas('configurations', function ($q) { $q->where('category_id', $this->post->category_id); }) ->get(); foreach ($devices as $device) { /** @var Device $device */ $userTz = 'Europe/Rome'; try { $userTz = $device->user->timezone; } catch (\Exception $e) { } $sendDate = Carbon::now($userTz); $startingHours = 9; $endingHours = 22; $shouldQueue = false; if ($sendDate->isBefore(Carbon::now($userTz) ->startOfDay() ->addHours($startingHours))) { $diff = $sendDate->diffInMinutes(Carbon::now($userTz) ->startOfDay() ->addHours($startingHours)); $sendDate = Carbon::now() ->addMinutes($diff) ->startOfHour() ->addHour(); $shouldQueue = true; } else if ($sendDate->isAfter(Carbon::now($userTz) ->startOfDay() ->addHours($endingHours))) { $diff = $sendDate->diffInMinutes(Carbon::now($userTz) ->startOfDay() ->addDay() ->addHours($startingHours), true); $sendDate = Carbon::now() ->addMinutes($diff) ->startOfHour() ->addHour(); $shouldQueue = true; } else { $this->post->createNotification($device); $sendDate = Carbon::now(); if ($device->user) { $device->notify(new NotifyUsersForNewPostCreatedNotification($this->post, $device->user->getBadge())); } } if ($shouldQueue) { /** @var PushQueue $queue */ $queue = PushQueue::query() ->make([ 'send_at' => $sendDate, ]); $queue->post() ->associate($this->post); $queue->device() ->associate($device); $queue->save(); } } } }