99use JeckelLab \Clock \Clock \FakedClock ;
1010use JeckelLab \Clock \Clock \FrozenClock ;
1111use JeckelLab \Clock \Clock \RealClock ;
12+ use JeckelLab \Clock \Exception \InvalidFakeClockInitialValueException ;
1213use JeckelLab \Clock \Exception \RuntimeException ;
1314use JeckelLab \Contract \Infrastructure \System \Clock as ClockInterface ;
1415
@@ -37,14 +38,21 @@ public static function getClock(array $config = []): ClockInterface
3738 );
3839 }
3940
40- switch ($ config ['mode ' ] ?? '' ) {
41- case 'frozen ' :
42- return new FrozenClock (self ::getInitialTimeFromConfig ($ config ));
43- case 'faked ' :
44- return new FakedClock (self ::getInitialTimeFromConfig ($ config ), $ timezone );
45- case 'real ' :
46- default :
41+ try {
42+ switch ($ config ['mode ' ] ?? '' ) {
43+ case 'frozen ' :
44+ return new FrozenClock (self ::getInitialTimeFromConfig ($ config ));
45+ case 'faked ' :
46+ return new FakedClock (self ::getInitialTimeFromConfig ($ config ), $ timezone );
47+ case 'real ' :
48+ default :
49+ return new RealClock ($ timezone );
50+ }
51+ } catch (InvalidFakeClockInitialValueException $ e ) {
52+ if (isset ($ config ['fallback_to_current_date ' ]) && $ config ['fallback_to_current_date ' ] === true ) {
4753 return new RealClock ($ timezone );
54+ }
55+ throw $ e ;
4856 }
4957 }
5058
@@ -62,12 +70,12 @@ protected static function getInitialTimeFromConfig(array $config): DateTimeImmut
6270 if (isset ($ config ['fake_time_path ' ])) {
6371 $ filePath = (string ) $ config ['fake_time_path ' ];
6472 if (! is_readable ($ filePath )) {
65- throw new Exception ('Impossible to read fake time file: ' . $ filePath );
73+ throw new InvalidFakeClockInitialValueException ('Impossible to read fake time file: ' . $ filePath );
6674 }
6775 return new DateTimeImmutable (file_get_contents ($ filePath ));
6876 }
6977 } catch (Exception $ e ) {
70- throw new RuntimeException (
78+ throw new InvalidFakeClockInitialValueException (
7179 'Invalid fake time provided: ' . $ e ->getMessage (),
7280 (int ) $ e ->getCode (),
7381 $ e
0 commit comments