Skip to content

Commit

Permalink
feat: Added holiday greetings
Browse files Browse the repository at this point in the history
  • Loading branch information
lewislarsen committed Dec 19, 2024
1 parent b546efe commit 37f7ea1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
32 changes: 29 additions & 3 deletions app/Services/GreetingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,27 @@
use Carbon\Carbon;

/**
* Service for generating time-appropriate greetings.
* Service for generating time-appropriate and holiday greetings.
*
* Provides methods to automatically determine the appropriate
* greeting based on the time of day in a given timezone.
* greeting based on the time of day or a holiday in a given timezone.
*/
class GreetingService
{
/**
* Get an automatic greeting based on the current time.
* Get an automatic greeting based on the current time or holiday.
*/
public function auto(string $timezone = 'UTC'): string
{
$currentDate = Carbon::now($timezone)->format('Y-m-d');

// Check if today is a holiday
$holidayGreeting = $this->holidayGreeting($currentDate);
if ($holidayGreeting !== null) {
return $holidayGreeting;
}

// Otherwise, return a time-based greeting
$hour = Carbon::now($timezone)->hour;

if ($hour < 12) {
Expand Down Expand Up @@ -55,4 +64,21 @@ private function evening(): string
{
return __('Good evening');
}

/**
* Get the holiday greeting for a specific date.
*/
private function holidayGreeting(string $date): ?string
{
$currentYear = Carbon::now()->year;
$holidays = [
"{$currentYear}-01-01" => (string) __('Happy New Year'),
"{$currentYear}-04-01" => (string) __("Happy April Fools' Day"),
"{$currentYear}-12-25" => (string) __('Merry Christmas'),
"{$currentYear}-12-26" => (string) __('Happy Boxing Day'),
"{$currentYear}-10-31" => (string) __('Happy Halloween'),
];

return $holidays[$date] ?? null;
}
}
45 changes: 45 additions & 0 deletions tests/Unit/Services/GreetingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,48 @@

Carbon::setTestNow();
});

it('returns holiday greeting on Christmas', function (): void {
$currentYear = Carbon::now()->year;
Carbon::setTestNow(Carbon::createMidnightDate($currentYear, 12, 25, 'UTC'));

expect(Greeting::auto('UTC'))->toBe('Merry Christmas');

Carbon::setTestNow();
});

it('returns holiday greeting on New Year', function (): void {
$currentYear = Carbon::now()->year;
Carbon::setTestNow(Carbon::createMidnightDate($currentYear, 1, 1, 'UTC'));

expect(Greeting::auto('UTC'))->toBe('Happy New Year');

Carbon::setTestNow();
});

it('returns holiday greeting on Halloween', function (): void {
$currentYear = Carbon::now()->year;
Carbon::setTestNow(Carbon::createMidnightDate($currentYear, 10, 31, 'UTC'));

expect(Greeting::auto('UTC'))->toBe('Happy Halloween');

Carbon::setTestNow();
});

it('returns holiday greeting on Boxing Day', function (): void {
$currentYear = Carbon::now()->year;
Carbon::setTestNow(Carbon::createMidnightDate($currentYear, 12, 26, 'UTC'));

expect(Greeting::auto('UTC'))->toBe('Happy Boxing Day');

Carbon::setTestNow();
});

it("returns holiday greeting on April Fools' Day", function (): void {
$currentYear = Carbon::now()->year;
Carbon::setTestNow(Carbon::createMidnightDate($currentYear, 4, 1, 'UTC'));

expect(Greeting::auto('UTC'))->toBe("Happy April Fools' Day");

Carbon::setTestNow();
});

0 comments on commit 37f7ea1

Please sign in to comment.