DateTimeStrategyFactory::__invoke()   A
last analyzed

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
final class DateTimeStrategyFactory extends AbstractFactory
15
{
16
    /**
17
     * @throws ServiceNotCreatedException
18
     * @throws ServiceNotFoundException
19
     * @throws ContainerExceptionInterface
20
     */
21
    public function __invoke(
22
        ContainerInterface $container,
23
        string $requestedName,
24
        array $options = null
25
    ): DateTimeFormatterStrategy {
26
        $options = $options ?? $this->getServiceOptions($container, $requestedName);
27
28
        $format = $options['format'] ?? null;
29
        if (null === $format) {
30
            throw new ServiceNotCreatedException(
31
                sprintf(
32
                    'The required \'format\' configuration option is missing for service \'%s\'',
33
                    $requestedName
34
                )
35
            );
36
        }
37
38
        return new DateTimeFormatterStrategy($format, $options['timezone'] ?? null, $options['fallback'] ?? false);
39
    }
40
}
41