setTime(static::INTERVAL_HOUR, static::INTERVAL_MINUTE); // Put the current Daily a day in the past when the current time the job runs is still before the new Daily if(Carbon::now()->isBefore($dailyToday)) $dailyToday->subDay(); $dailyTomorrow = $dailyToday->copy()->addDay(); $currentWeekly = Carbon::parse('last '.static::WEEKLY_INTERVAL_DAY); $currentWeekly->setTime(static::INTERVAL_HOUR, static::INTERVAL_MINUTE); $nextWeekly = $currentWeekly->copy()->addWeek(); $dailys = [$dailyToday, $dailyTomorrow]; $weeklys = [$currentWeekly, $nextWeekly]; $log = Log::channel('challengeCreation'); foreach ([Faction::Hunter, Faction::Runner] as $faction) { foreach ($dailys as $time) { $dailyExists = TimedChallenge::whereDate('start_time', $time) ->where('type', ChallengeType::Daily) ->where('faction', $faction) ->exists(); if(!$dailyExists) { try { $newDaily = TimedChallengeFactory::makeChallenge($time, $faction, ChallengeType::Daily); $newDaily->save(); } catch (TimedChallengeFactoryException $e) { $log->error($e->getMessage()); } } } foreach ($weeklys as $time) { $weeklyExists = TimedChallenge::whereDate('start_time', $time) ->where('type', ChallengeType::Weekly) ->where('faction', $faction) ->exists(); if(!$weeklyExists) { try { $newDaily = TimedChallengeFactory::makeChallenge($time, $faction, ChallengeType::Weekly); $newDaily->save(); } catch (TimedChallengeFactoryException $e) { $log->error($e->getMessage()); } } } } } }