Skip to content

Commit

Permalink
feat: added localised welcoming
Browse files Browse the repository at this point in the history
  • Loading branch information
lewislarsen committed Jun 16, 2024
1 parent 50fe510 commit cdc3cd1
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
14 changes: 14 additions & 0 deletions app/Facades/Greeting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Facades;

use App\Services\GreetingService;
use Illuminate\Support\Facades\Facade;

class Greeting extends Facade
{
protected static function getFacadeAccessor(): string
{
return GreetingService::class;
}
}
7 changes: 6 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
namespace App\Providers;

use App\Models\User;
use App\Services\GreetingService;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
public function register(): void
{
//
$this->app->singleton(GreetingService::class, function ($app) {
return new GreetingService;
});

$this->app->alias(GreetingService::class, 'Greeting');
}

public function boot(): void
Expand Down
24 changes: 24 additions & 0 deletions app/Services/GreetingService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Services;

use Carbon\Carbon;

class GreetingService
{
public function auto($timezone = 'UTC'): string
{
$currentTime = Carbon::now($timezone);
$hour = $currentTime->hour;

if ($hour < 12) {
return __('Good morning');
}

if ($hour < 18) {
return __('Good afternoon');
}

return __('Good evening');
}
}
2 changes: 1 addition & 1 deletion resources/views/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<img class="h-14 w-14 rounded-full border border-gray-200 dark:border-gray-700" src="{{ Auth::user()->gravatar() }}" alt="{{ Auth::user()->name }}" />
<div>
<div class="font-semibold text-2xl ml-3 mt-0 dark:text-gray-100">
{{ __('Hey, :name!', ['name' => Auth::user()->first_name]) }}
{{ \App\Facades\Greeting::auto(Auth::user()->timezone) }}, {{ Auth::user()->first_name }}!
</div>
<p class="text-gray-700 dark:text-gray-300 mt-0.5 ml-3">
{{ trans_choice(':count backup task has|:count backup tasks have', Auth::user()->backupTasklogCountToday(), ['count' => Auth::user()->backupTasklogCountToday()]) }} {{ __('been run today.') }}
Expand Down
38 changes: 38 additions & 0 deletions tests/Unit/Services/GreetingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use App\Facades\Greeting;
use Carbon\Carbon;

it('returns Good morning', function () {
Carbon::setTestNow(Carbon::createFromTime(8, 0, 0, 'UTC'));

expect(Greeting::auto('UTC'))->toBe('Good morning');

Carbon::setTestNow();
});

it('returns Good afternoon', function () {
Carbon::setTestNow(Carbon::createFromTime(14, 0, 0, 'UTC'));

expect(Greeting::auto('UTC'))->toBe('Good afternoon');

Carbon::setTestNow();
});

it('returns Good evening', function () {
Carbon::setTestNow(Carbon::createFromTime(20, 0, 0, 'UTC'));

expect(Greeting::auto('UTC'))->toBe('Good evening');

Carbon::setTestNow();
});

it('handles different timezones correctly', function () {
Carbon::setTestNow(Carbon::createFromTime(8, 0, 0, 'UTC'));

expect(Greeting::auto('Europe/Berlin'))->toBe('Good morning')
->and(Greeting::auto('Asia/Shanghai'))->toBe('Good afternoon')
->and(Greeting::auto('America/New_York'))->toBe('Good morning');

Carbon::setTestNow();
});

0 comments on commit cdc3cd1

Please sign in to comment.