Passed
Push — master ( b3d1e7...30dac1 )
by Alex
49s queued 13s
created

DateTimeStrategyFactory::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 8
c 3
b 1
f 0
dl 0
loc 18
rs 10
cc 2
nc 2
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDateTime\Factory\Hydrator\Strategy;
6
7
use Arp\LaminasFactory\AbstractFactory;
8
use Laminas\Hydrator\Strategy\DateTimeFormatterStrategy;
9
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
10
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
11
use Psr\Container\ContainerExceptionInterface;
12
use Psr\Container\ContainerInterface;
13
14
/**
15
 * @author  Alex Patterson <[email protected]>
16
 * @package Arp\LaminasDateTime\Factory\Hydrator\Strategy
17
 */
18
final class DateTimeStrategyFactory extends AbstractFactory
19
{
20
    /**
21
     * @param ContainerInterface        $container
22
     * @param string                    $requestedName
23
     * @param array<string, mixed>|null $options
24
     *
25
     * @return DateTimeFormatterStrategy
26
     *
27
     * @throws ServiceNotCreatedException
28
     * @throws ServiceNotFoundException
29
     * @throws ContainerExceptionInterface
30
     */
31
    public function __invoke(
32
        ContainerInterface $container,
33
        string $requestedName,
34
        array $options = null
35
    ): DateTimeFormatterStrategy {
36
        $options = $options ?? $this->getServiceOptions($container, $requestedName);
37
38
        $format = $options['format'] ?? null;
39
        if (null === $format) {
40
            throw new ServiceNotCreatedException(
41
                sprintf(
42
                    'The required \'format\' configuration option is missing for service \'%s\'',
43
                    $requestedName
44
                )
45
            );
46
        }
47
48
        return new DateTimeFormatterStrategy($format, $options['timezone'] ?? null, $options['fallback'] ?? false);
49
    }
50
}
51