Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php namespace App\Messages\Helpers;
- use DateTimeZone;
- use Carbon\Carbon;
- class Scheduler
- {
- private $sendAt;
- private $sendAtNoDelay;
- private $countryCode;
- private $defaultTimezone;
- private $acceptedFrom;
- private $acceptedTo;
- private $timezone;
- public function __construct(Carbon $sendAtNoDelay, $countryCode, $defaultTimezone, $silentFrom, $silentTo)
- {
- $this->sendAtNoDelay = $sendAtNoDelay;
- $this->countryCode = $countryCode;
- $this->defaultTimezone = $defaultTimezone;
- if ($silentFrom > $silentTo) {
- $this->acceptedFrom = $silentTo;
- $this->acceptedTo = $silentFrom;
- } else {
- $this->acceptedFrom = $silentFrom;
- $this->acceptedTo = $silentTo;
- }
- $this->timezone = $this->getTimezone($this->countryCode, $this->defaultTimezone);
- $this->sendAt = $this->sendAtNoDelay->setTimezone($this->timezone);
- }
- public function addDelay($delay = null, $delayType = null, $test = false)
- {
- if ($test) {
- if ($delayType === 'minute') $this->sendAt = $this->sendAt->addMinutes(2);
- if ($delayType === 'hour') $this->sendAt = $this->sendAt->addMinutes(5);
- if ($delayType === 'day') $this->sendAt = $this->sendAt->addMinutes(10);
- } else {
- if ($delayType === 'minute') $this->sendAt->addMinutes($delay);
- if ($delayType === 'hour') $this->sendAt = $this->sendAt->addHours($delay);
- if ($delayType === 'day') $this->sendAt = $this->sendAt->addDays($delay);
- }
- return $this;
- }
- public function getSendAt(): Carbon
- {
- list($acceptedFrom, $acceptedTo) = $this->getAcceptedFromAndTo();
- if (!$this->inValidTimes($this->sendAt, $acceptedFrom, $acceptedTo)) {
- $this->sendAt = $acceptedFrom > $this->sendAt ?
- $acceptedFrom->copy()->timezone('UTC') : $acceptedFrom->copy()->addDay()->timezone('UTC');
- }
- return $this->applyRestrictions($this->sendAt);
- }
- private function inValidTimes(Carbon $sendAt, Carbon $acceptedFrom, Carbon $acceptedTo): bool
- {
- return $sendAt->toDateTimeString() >= $acceptedFrom->copy()->toDateTimeString() &&
- $sendAt->toDateTimeString() <= $acceptedTo->copy()->toDateTimeString();
- }
- private function getAcceptedFromAndTo(): array
- {
- list($hour, $minute) = explode(':', $this->acceptedFrom);
- $acceptedFrom = $this->sendAt->copy()->setTime($hour, $minute);
- list($hour, $minute) = explode(':', $this->acceptedTo);
- $acceptedTo = $this->sendAt->copy()->setTime($hour, $minute);
- return [$acceptedFrom, $acceptedTo];
- }
- private function getTimezone($countryCode, $defaultTimezone)
- {
- if (strlen($countryCode) != 2) return $defaultTimezone;
- $timezones = (DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, $countryCode));
- return count($timezones) > 0 ? $timezones[0] : $defaultTimezone;
- }
- private function applyRestrictions(Carbon $sendAt)
- {
- $countryCode = mb_strtoupper($this->countryCode);
- switch ($countryCode) {
- case 'FR':
- $acceptedFromFR = $sendAt->copy()->setTime(8, 0);
- $acceptedToFr = $sendAt->copy()->setTime(19, 55);
- if ($acceptedToFr < $sendAt) {
- if ($this->acceptedFrom >= '08:00') {
- list($hour, $minute) = explode(':', $this->acceptedFrom);
- $sendAt = $acceptedFromFR->setTime($hour, $minute)->addDay(1)->copy();
- } else $sendAt = $acceptedFromFR->copy()->addDay(1);
- }
- if ($sendAt->isSunday()) {
- list($hour, $minute) = explode(':', $this->acceptedFrom);
- $sendAt = $sendAt->addDay()->setTime($hour, $minute);
- }
- return $sendAt->setTimezone('UTC');
- break;
- case 'ID':
- $acceptedFromFR = $sendAt->copy()->setTime(9, 0);
- $acceptedToFr = $sendAt->copy()->setTime(20, 55);
- if ($acceptedToFr < $sendAt) {
- if ($this->acceptedFrom >= '09:00') {
- list($hour, $minute) = explode(':', $this->acceptedFrom);
- $sendAt = $acceptedFromFR->setTime($hour, $minute)->addDay(1)->copy();
- } else $sendAt = $acceptedFromFR->copy()->addDay(1);
- }
- return $sendAt->setTimezone('UTC');
- break;
- default:
- return $sendAt->setTimezone('UTC');
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement