Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
DateTimeZoneFactory
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
1 / 1
 __invoke
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare(strict_types=1);
4
5/**
6 * This file is part of php-fast-forward/clock.
7 *
8 * This source file is subject to the license bundled
9 * with this source code in the file LICENSE.
10 *
11 * @copyright Copyright (c) 2025-2026 Felipe SayĆ£o Lobato Abreu <github@mentordosnerds.com>
12 * @license   https://opensource.org/licenses/MIT MIT License
13 *
14 * @see       https://github.com/php-fast-forward/clock
15 * @see       https://github.com/php-fast-forward
16 * @see       https://datatracker.ietf.org/doc/html/rfc2119
17 */
18
19namespace FastForward\Clock\ServiceProvider\Factory;
20
21use DateTimeZone;
22use FastForward\Config\ConfigInterface;
23use Psr\Container\ContainerInterface;
24
25/**
26 * Factory for creating DateTimeZone instances for the Fast Forward Container.
27 *
28 * This factory SHALL return a DateTimeZone instance based on configuration, or fallback to the default timezone.
29 *
30 * @see https://github.com/php-fast-forward/clock
31 * @see https://github.com/php-fast-forward
32 * @see https://datatracker.ietf.org/doc/html/rfc2119
33 */
34 class DateTimeZoneFactory
35{
36    /**
37     * Creates a DateTimeZone instance using configuration from the container.
38     *
39     * If the container does not provide a ConfigInterface, or the configuration does not specify a timezone,
40     * this method SHALL return a DateTimeZone using the current default timezone.
41     *
42     * If the configuration value for DateTimeZone::class is already a DateTimeZone instance, it SHALL be returned as-is.
43     * Otherwise, the value SHALL be used as the timezone identifier string.
44     *
45     * @param ContainerInterface $container the container from which to retrieve configuration
46     *
47     * @return DateTimeZone the resolved DateTimeZone instance
48     */
49    public function __invoke(ContainerInterface $container): DateTimeZone
50    {
51        if (! $container->has(ConfigInterface::class)
52            || ! $container->get(ConfigInterface::class)->has(DateTimeZone::class)
53        ) {
54            return new DateTimeZone(date_default_timezone_get());
55        }
56
57        $timezone = $container->get(ConfigInterface::class)->get(DateTimeZone::class);
58
59        if ($timezone instanceof DateTimeZone) {
60            return $timezone;
61        }
62
63        return new DateTimeZone($timezone);
64    }
65}