| 1 | <?php |
||
| 8 | class Period |
||
| 9 | { |
||
| 10 | public Carbon $startDateTime; |
||
|
|
|||
| 11 | |||
| 12 | public Carbon $endDateTime; |
||
| 13 | |||
| 14 | public function __construct(Carbon $startDateTime, Carbon $endDateTime) |
||
| 15 | { |
||
| 16 | if ($startDateTime->gt($endDateTime)) { |
||
| 17 | throw InvalidPeriod::startDateMustComeBeforeEndDate($startDateTime, $endDateTime); |
||
| 18 | } |
||
| 19 | |||
| 20 | $this->startDateTime = $startDateTime; |
||
| 21 | |||
| 22 | $this->endDateTime = $endDateTime; |
||
| 23 | } |
||
| 24 | |||
| 25 | public function duration(): string |
||
| 26 | { |
||
| 27 | $interval = $this->startDateTime->diff($this->endDateTime); |
||
| 28 | |||
| 29 | if (! $this->startDateTime->diffInHours($this->endDateTime)) { |
||
| 30 | return $interval->format('%im'); |
||
| 31 | } |
||
| 32 | |||
| 33 | if (! $this->startDateTime->diffInDays($this->endDateTime)) { |
||
| 34 | return $interval->format('%hh %im'); |
||
| 35 | } |
||
| 36 | |||
| 37 | return $interval->format('%dd %hh %im'); |
||
| 38 | } |
||
| 39 | |||
| 40 | public function toText(): string |
||
| 41 | { |
||
| 42 | $configuredDateFormat = config('uptime-monitor.notifications.date_format'); |
||
| 43 | |||
| 44 | return |
||
| 45 | $this->startDateTime->format('H:i').' ' |
||
| 46 | .($this->startDateTime->isToday() ? '' : "on {$this->startDateTime->format($configuredDateFormat)} ") |
||
| 47 | .'➡️ ' |
||
| 48 | .$this->endDateTime->format('H:i') |
||
| 49 | .($this->endDateTime->isToday() ? '' : " on {$this->endDateTime->format($configuredDateFormat)}"); |
||
| 50 | } |
||
| 51 | } |
||
| 52 |