Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.73 KB | None | 0 0
  1. <?php namespace App\Messages\Helpers;
  2.  
  3. use DateTimeZone;
  4. use Carbon\Carbon;
  5.  
  6. class Scheduler
  7. {
  8.     private $sendAt;
  9.     private $sendAtNoDelay;
  10.     private $countryCode;
  11.     private $defaultTimezone;
  12.     private $acceptedFrom;
  13.     private $acceptedTo;
  14.     private $timezone;
  15.  
  16.     public function __construct(Carbon $sendAtNoDelay, $countryCode, $defaultTimezone, $silentFrom, $silentTo)
  17.     {
  18.  
  19.         $this->sendAtNoDelay = $sendAtNoDelay;
  20.         $this->countryCode = $countryCode;
  21.         $this->defaultTimezone = $defaultTimezone;
  22.  
  23.         if ($silentFrom > $silentTo) {
  24.             $this->acceptedFrom = $silentTo;
  25.             $this->acceptedTo = $silentFrom;
  26.         } else {
  27.             $this->acceptedFrom = $silentFrom;
  28.             $this->acceptedTo = $silentTo;
  29.         }
  30.  
  31.         $this->timezone = $this->getTimezone($this->countryCode, $this->defaultTimezone);
  32.         $this->sendAt = $this->sendAtNoDelay->setTimezone($this->timezone);
  33.     }
  34.  
  35.     public function addDelay($delay = null, $delayType = null, $test = false)
  36.     {
  37.         if ($test) {
  38.             if ($delayType === 'minute') $this->sendAt = $this->sendAt->addMinutes(2);
  39.             if ($delayType === 'hour') $this->sendAt = $this->sendAt->addMinutes(5);
  40.             if ($delayType === 'day') $this->sendAt = $this->sendAt->addMinutes(10);
  41.         } else {
  42.             if ($delayType === 'minute') $this->sendAt->addMinutes($delay);
  43.             if ($delayType === 'hour') $this->sendAt = $this->sendAt->addHours($delay);
  44.             if ($delayType === 'day') $this->sendAt = $this->sendAt->addDays($delay);
  45.         }
  46.  
  47.         return $this;
  48.     }
  49.  
  50.     public function getSendAt(): Carbon
  51.     {
  52.         list($acceptedFrom, $acceptedTo) = $this->getAcceptedFromAndTo();
  53.  
  54.         if (!$this->inValidTimes($this->sendAt, $acceptedFrom, $acceptedTo)) {
  55.             $this->sendAt = $acceptedFrom > $this->sendAt ?
  56.                 $acceptedFrom->copy()->timezone('UTC') : $acceptedFrom->copy()->addDay()->timezone('UTC');
  57.         }
  58.  
  59.         return $this->applyRestrictions($this->sendAt);
  60.     }
  61.  
  62.     private function inValidTimes(Carbon $sendAt, Carbon $acceptedFrom, Carbon $acceptedTo): bool
  63.     {
  64.         return $sendAt->toDateTimeString() >= $acceptedFrom->copy()->toDateTimeString() &&
  65.             $sendAt->toDateTimeString() <= $acceptedTo->copy()->toDateTimeString();
  66.     }
  67.  
  68.     private function getAcceptedFromAndTo(): array
  69.     {
  70.         list($hour, $minute) = explode(':', $this->acceptedFrom);
  71.         $acceptedFrom = $this->sendAt->copy()->setTime($hour, $minute);
  72.  
  73.         list($hour, $minute) = explode(':', $this->acceptedTo);
  74.         $acceptedTo = $this->sendAt->copy()->setTime($hour, $minute);
  75.  
  76.         return [$acceptedFrom, $acceptedTo];
  77.     }
  78.  
  79.     private function getTimezone($countryCode, $defaultTimezone)
  80.     {
  81.         if (strlen($countryCode) != 2) return $defaultTimezone;
  82.  
  83.         $timezones = (DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, $countryCode));
  84.  
  85.         return count($timezones) > 0 ? $timezones[0] : $defaultTimezone;
  86.     }
  87.  
  88.     private function applyRestrictions(Carbon $sendAt)
  89.     {
  90.         $countryCode = mb_strtoupper($this->countryCode);
  91.  
  92.         switch ($countryCode) {
  93.             case 'FR':
  94.                 $acceptedFromFR = $sendAt->copy()->setTime(8, 0);
  95.                 $acceptedToFr = $sendAt->copy()->setTime(19, 55);
  96.  
  97.                 if ($acceptedToFr < $sendAt) {
  98.                     if ($this->acceptedFrom >= '08:00') {
  99.                         list($hour, $minute) = explode(':', $this->acceptedFrom);
  100.                         $sendAt = $acceptedFromFR->setTime($hour, $minute)->addDay(1)->copy();
  101.                     } else $sendAt = $acceptedFromFR->copy()->addDay(1);
  102.                 }
  103.  
  104.                 if ($sendAt->isSunday()) {
  105.                     list($hour, $minute) = explode(':', $this->acceptedFrom);
  106.  
  107.                     $sendAt = $sendAt->addDay()->setTime($hour, $minute);
  108.                 }
  109.  
  110.                 return $sendAt->setTimezone('UTC');
  111.                 break;
  112.             case 'ID':
  113.                 $acceptedFromFR = $sendAt->copy()->setTime(9, 0);
  114.                 $acceptedToFr = $sendAt->copy()->setTime(20, 55);
  115.  
  116.                 if ($acceptedToFr < $sendAt) {
  117.                     if ($this->acceptedFrom >= '09:00') {
  118.                         list($hour, $minute) = explode(':', $this->acceptedFrom);
  119.                         $sendAt = $acceptedFromFR->setTime($hour, $minute)->addDay(1)->copy();
  120.                     } else $sendAt = $acceptedFromFR->copy()->addDay(1);
  121.                 }
  122.  
  123.                 return $sendAt->setTimezone('UTC');
  124.                 break;
  125.             default:
  126.                 return $sendAt->setTimezone('UTC');
  127.         }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement