Skip to content

Commit

Permalink
chore: update ClockConfig (replace string property with DateTimezone)
Browse files Browse the repository at this point in the history
  • Loading branch information
bautrukevich committed Dec 7, 2023
1 parent 3c49a6f commit 847034d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
22 changes: 14 additions & 8 deletions packages/clock/src/ClockConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,40 @@
namespace A50\Clock;

use DateTimeZone;
use Exception;
use Webmozart\Assert\Assert;

final class ClockConfig
{
private string $timezone;
private DateTimeZone $timezone;

/**
* @throws Exception
*/
private function __construct(string $timezone)
{
Assert::inArray($timezone, DateTimeZone::listIdentifiers(DateTimeZone::ALL));
$this->timezone = $timezone;
$this->timezone = new DateTimeZone($timezone);
}

/**
* @throws Exception
*/
public static function withDefaults(
string $timezone = 'UTC',
): self {
return new self($timezone);
}

/**
* @throws Exception
*/
public function withTimezoneChanged(string $timezone): self
{
Assert::inArray($timezone, DateTimeZone::listIdentifiers(DateTimeZone::ALL));
$new = clone $this;
$new->timezone = $timezone;

return $new;
return new self($timezone);
}

public function timezone(): string
public function timezone(): DateTimeZone
{
return $this->timezone;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/clock/src/ClockProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static function getDefinitions(): array
/** @var ClockConfig $config */
$config = $container->get(ClockConfig::class);

return new TimeZoneAwareClock(new DateTimeZone($config->timezone()));
return new TimeZoneAwareClock($config->timezone());
},
];
}
Expand Down

0 comments on commit 847034d

Please sign in to comment.